Files
react-native/React/Fabric/Mounting/ComponentViews/Switch/RCTSwitchComponentView.mm
Valentin Shergin 00eab3d6fb Fabric: Introducing ComponentDescriptorProvider
Summary:
ComponentDescriptorProvider represents unified way to create a particular descriptor.
Now all ComponentViews (which support RCTComponentViewProtocol) expose a `ComponentDescriptorProvider` which will allow creating and registering ComponentDescriptor instances for all visual components automatically as a part of ComponentView registration process.
Don't panic, everything is still being as explicit as it always was, no magic involved; we just will have only one registration step instead of two parallel.

That also opens a way to register components on the fly.

Reviewed By: JoshuaGross

Differential Revision: D14963488

fbshipit-source-id: 9e9d9166fabaf7b30b35b8647faa6e3a19cd2435
2019-04-17 22:44:20 -07:00

92 lines
2.5 KiB
Plaintext

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "RCTSwitchComponentView.h"
#import <react/components/rncore/ComponentDescriptors.h>
#import <react/components/rncore/EventEmitters.h>
#import <react/components/rncore/Props.h>
using namespace facebook::react;
@implementation RCTSwitchComponentView {
UISwitch *_switchView;
BOOL _wasOn;
}
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
static const auto defaultProps = std::make_shared<const SwitchProps>();
_props = defaultProps;
_switchView = [[UISwitch alloc] initWithFrame:self.bounds];
[_switchView addTarget:self action:@selector(onChange:) forControlEvents:UIControlEventValueChanged];
_switchView.on = defaultProps->value;
self.contentView = _switchView;
}
return self;
}
#pragma mark - RCTComponentViewProtocol
+ (ComponentDescriptorProvider)componentDescriptorProvider
{
return concreteComponentDescriptorProvider<SwitchComponentDescriptor>();
}
- (void)updateProps:(SharedProps)props oldProps:(SharedProps)oldProps
{
const auto &oldSwitchProps = *std::static_pointer_cast<const SwitchProps>(oldProps ?: _props);
const auto &newSwitchProps = *std::static_pointer_cast<const SwitchProps>(props);
[super updateProps:props oldProps:oldProps];
// `value`
if (oldSwitchProps.value != newSwitchProps.value) {
_switchView.on = newSwitchProps.value;
_wasOn = newSwitchProps.value;
}
// `disabled`
if (oldSwitchProps.disabled != newSwitchProps.disabled) {
_switchView.enabled = !newSwitchProps.disabled;
}
// `tintColor`
if (oldSwitchProps.tintColor != newSwitchProps.tintColor) {
_switchView.tintColor = [UIColor colorWithCGColor:newSwitchProps.tintColor.get()];
}
// `onTintColor
if (oldSwitchProps.onTintColor != newSwitchProps.onTintColor) {
_switchView.onTintColor = [UIColor colorWithCGColor:newSwitchProps.onTintColor.get()];
}
// `thumbTintColor`
if (oldSwitchProps.thumbTintColor != newSwitchProps.thumbTintColor) {
_switchView.thumbTintColor = [UIColor colorWithCGColor:newSwitchProps.thumbTintColor.get()];
}
}
- (void)onChange:(UISwitch *)sender
{
if (_wasOn == sender.on) {
return;
}
_wasOn = sender.on;
std::dynamic_pointer_cast<const SwitchEventEmitter>(_eventEmitter)
->onChange(SwitchOnChangeStruct{.value = static_cast<bool>(sender.on)});
}
@end