diff --git a/React/Fabric/Mounting/ComponentViews/Slider/RCTSliderComponentView.h b/React/Fabric/Mounting/ComponentViews/Slider/RCTSliderComponentView.h new file mode 100644 index 00000000000000..75ca052d915003 --- /dev/null +++ b/React/Fabric/Mounting/ComponentViews/Slider/RCTSliderComponentView.h @@ -0,0 +1,21 @@ +/** + * 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 + +#import + +NS_ASSUME_NONNULL_BEGIN + +/** + * UIView class for root component. + */ +@interface RCTSliderComponentView : RCTViewComponentView + +@end + +NS_ASSUME_NONNULL_END diff --git a/React/Fabric/Mounting/ComponentViews/Slider/RCTSliderComponentView.mm b/React/Fabric/Mounting/ComponentViews/Slider/RCTSliderComponentView.mm new file mode 100644 index 00000000000000..abe5bae5143d1e --- /dev/null +++ b/React/Fabric/Mounting/ComponentViews/Slider/RCTSliderComponentView.mm @@ -0,0 +1,92 @@ +/** + * 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 "RCTSliderComponentView.h" + +#import +#import +#import + +using namespace facebook::react; + +@implementation RCTSliderComponentView { + UISlider *_sliderView; + float _prevValue; +} + +- (instancetype)initWithFrame:(CGRect)frame +{ + if (self = [super initWithFrame:frame]) { + static const auto defaultProps = std::make_shared(); + _props = defaultProps; + + _sliderView = [[UISlider alloc] initWithFrame:self.bounds]; + + [_sliderView addTarget:self + action:@selector(onChange:) + forControlEvents:UIControlEventValueChanged]; + + _sliderView.value = defaultProps->value; + + self.contentView = _sliderView; + } + + return self; +} + +#pragma mark - RCTComponentViewProtocol + ++ (ComponentHandle)componentHandle +{ + return SliderShadowNode::Handle(); +} + +- (void)updateProps:(SharedProps)props oldProps:(SharedProps)oldProps +{ + const auto &oldSliderProps = *std::static_pointer_cast(oldProps ?: _props); + const auto &newSliderProps = *std::static_pointer_cast(props); + + [super updateProps:props oldProps:oldProps]; + + // `value` + if (oldSliderProps.value != newSliderProps.value) { + _sliderView.value = newSliderProps.value; + _prevValue = newSliderProps.value; + } + + // `disabled` + if (oldSliderProps.disabled != newSliderProps.disabled) { + _sliderView.enabled = !newSliderProps.disabled; + } + + // `thumbTintColor` + if (oldSliderProps.thumbTintColor != newSliderProps.thumbTintColor) { + _sliderView.thumbTintColor = [UIColor colorWithCGColor:newSliderProps.thumbTintColor.get()]; + } + + // `minimumTrackTintColor` + if (oldSliderProps.minimumTrackTintColor != newSliderProps.minimumTrackTintColor) { + _sliderView.minimumTrackTintColor = [UIColor colorWithCGColor:newSliderProps.minimumTrackTintColor.get()]; + } + + // `maximumTrackTintColor` + if (oldSliderProps.maximumTrackTintColor != newSliderProps.maximumTrackTintColor) { + _sliderView.maximumTrackTintColor = [UIColor colorWithCGColor:newSliderProps.maximumTrackTintColor.get()]; + } +} + +- (void)onChange:(UISlider *)sender +{ + if (_prevValue == sender.value) { + return; + } + _prevValue = sender.value; + + std::dynamic_pointer_cast(_eventEmitter)->onValueChange(sender.value); +} + +@end diff --git a/React/Fabric/Mounting/RCTComponentViewFactory.mm b/React/Fabric/Mounting/RCTComponentViewFactory.mm index 817f65657fc9c9..6b160f713f9511 100644 --- a/React/Fabric/Mounting/RCTComponentViewFactory.mm +++ b/React/Fabric/Mounting/RCTComponentViewFactory.mm @@ -16,6 +16,7 @@ #import "RCTParagraphComponentView.h" #import "RCTRootComponentView.h" #import "RCTActivityIndicatorViewComponentView.h" +#import "RCTSliderComponentView.h" #import "RCTSwitchComponentView.h" using namespace facebook::react; @@ -37,6 +38,7 @@ + (RCTComponentViewFactory *)standardComponentViewFactory [componentViewFactory registerComponentViewClass:[RCTImageComponentView class]]; [componentViewFactory registerComponentViewClass:[RCTParagraphComponentView class]]; [componentViewFactory registerComponentViewClass:[RCTActivityIndicatorViewComponentView class]]; + [componentViewFactory registerComponentViewClass:[RCTSliderComponentView class]]; [componentViewFactory registerComponentViewClass:[RCTSwitchComponentView class]]; return componentViewFactory;