mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Summary: Even though we have wonderful helper functions converting SharedColor to UIColor and CGColorRef, in many places we still use some hardcoded things. This diff fixes that; it will be important for the next diff. Changelog: [Internal] Fabric-specific internal change. Reviewed By: JoshuaGross Differential Revision: D23753508 fbshipit-source-id: 09d280b132266252753526c2735ab3e41b96c8d5
125 lines
3.2 KiB
Plaintext
125 lines
3.2 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/RCTConversions.h>
|
|
|
|
#import <react/renderer/components/rncore/ComponentDescriptors.h>
|
|
#import <react/renderer/components/rncore/EventEmitters.h>
|
|
#import <react/renderer/components/rncore/Props.h>
|
|
#import <react/renderer/components/rncore/RCTComponentViewHelpers.h>
|
|
|
|
#import "FBRCTFabricComponentsPlugins.h"
|
|
|
|
using namespace facebook::react;
|
|
|
|
@interface RCTSwitchComponentView () <RCTSwitchViewProtocol>
|
|
@end
|
|
|
|
@implementation RCTSwitchComponentView {
|
|
UISwitch *_switchView;
|
|
}
|
|
|
|
- (instancetype)initWithFrame:(CGRect)frame
|
|
{
|
|
if (self = [super initWithFrame:frame]) {
|
|
_switchView = [[UISwitch alloc] initWithFrame:self.bounds];
|
|
|
|
[_switchView addTarget:self action:@selector(onChange:) forControlEvents:UIControlEventValueChanged];
|
|
|
|
self.contentView = _switchView;
|
|
|
|
[self setPropsToDefault];
|
|
}
|
|
|
|
return self;
|
|
}
|
|
|
|
- (void)setPropsToDefault
|
|
{
|
|
static const auto defaultProps = std::make_shared<const SwitchProps>();
|
|
_props = defaultProps;
|
|
_switchView.on = defaultProps->value;
|
|
}
|
|
|
|
#pragma mark - RCTComponentViewProtocol
|
|
|
|
- (void)prepareForRecycle
|
|
{
|
|
[super prepareForRecycle];
|
|
[self setPropsToDefault];
|
|
}
|
|
|
|
+ (ComponentDescriptorProvider)componentDescriptorProvider
|
|
{
|
|
return concreteComponentDescriptorProvider<SwitchComponentDescriptor>();
|
|
}
|
|
|
|
- (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &)oldProps
|
|
{
|
|
const auto &oldSwitchProps = *std::static_pointer_cast<const SwitchProps>(_props);
|
|
const auto &newSwitchProps = *std::static_pointer_cast<const SwitchProps>(props);
|
|
|
|
// `value`
|
|
if (oldSwitchProps.value != newSwitchProps.value) {
|
|
_switchView.on = newSwitchProps.value;
|
|
}
|
|
|
|
// `disabled`
|
|
if (oldSwitchProps.disabled != newSwitchProps.disabled) {
|
|
_switchView.enabled = !newSwitchProps.disabled;
|
|
}
|
|
|
|
// `tintColor`
|
|
if (oldSwitchProps.tintColor != newSwitchProps.tintColor) {
|
|
_switchView.tintColor = RCTUIColorFromSharedColor(newSwitchProps.tintColor);
|
|
}
|
|
|
|
// `onTintColor
|
|
if (oldSwitchProps.onTintColor != newSwitchProps.onTintColor) {
|
|
_switchView.onTintColor = RCTUIColorFromSharedColor(newSwitchProps.onTintColor);
|
|
}
|
|
|
|
// `thumbTintColor`
|
|
if (oldSwitchProps.thumbTintColor != newSwitchProps.thumbTintColor) {
|
|
_switchView.thumbTintColor = RCTUIColorFromSharedColor(newSwitchProps.thumbTintColor);
|
|
}
|
|
|
|
[super updateProps:props oldProps:oldProps];
|
|
}
|
|
|
|
- (void)onChange:(UISwitch *)sender
|
|
{
|
|
const auto &props = *std::static_pointer_cast<const SwitchProps>(_props);
|
|
if (props.value == sender.on) {
|
|
return;
|
|
}
|
|
|
|
std::dynamic_pointer_cast<const SwitchEventEmitter>(_eventEmitter)
|
|
->onChange(SwitchEventEmitter::OnChange{.value = static_cast<bool>(sender.on)});
|
|
}
|
|
|
|
#pragma mark - Native Commands
|
|
|
|
- (void)handleCommand:(const NSString *)commandName args:(const NSArray *)args
|
|
{
|
|
RCTSwitchHandleCommand(self, commandName, args);
|
|
}
|
|
|
|
- (void)setValue:(BOOL)value
|
|
{
|
|
[_switchView setOn:value animated:YES];
|
|
}
|
|
|
|
@end
|
|
|
|
Class<RCTComponentViewProtocol> RCTSwitchCls(void)
|
|
{
|
|
return RCTSwitchComponentView.class;
|
|
}
|