Fabric: Add Fabric-compatible Slider component to iOS (ObjC code)

Summary: Objective-C side of the Fabric-compatible slider component for iOS.

Reviewed By: mdvacca

Differential Revision: D13745263

fbshipit-source-id: 647631d6fc86f81a5d4f735c507636ed9c468093
This commit is contained in:
Joshua Gross
2019-01-22 17:03:08 -08:00
committed by Facebook Github Bot
parent c40a782b3a
commit 62395d09eb
3 changed files with 115 additions and 0 deletions
@@ -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 <UIKit/UIKit.h>
#import <React/RCTViewComponentView.h>
NS_ASSUME_NONNULL_BEGIN
/**
* UIView class for root <Slider> component.
*/
@interface RCTSliderComponentView : RCTViewComponentView
@end
NS_ASSUME_NONNULL_END
@@ -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 <react/components/slider/SliderEventEmitter.h>
#import <react/components/slider/SliderProps.h>
#import <react/components/slider/SliderShadowNode.h>
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<const SliderProps>();
_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<const SliderProps>(oldProps ?: _props);
const auto &newSliderProps = *std::static_pointer_cast<const SliderProps>(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<const SliderEventEmitter>(_eventEmitter)->onValueChange(sender.value);
}
@end
@@ -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 @@ using namespace facebook::react;
[componentViewFactory registerComponentViewClass:[RCTImageComponentView class]];
[componentViewFactory registerComponentViewClass:[RCTParagraphComponentView class]];
[componentViewFactory registerComponentViewClass:[RCTActivityIndicatorViewComponentView class]];
[componentViewFactory registerComponentViewClass:[RCTSliderComponentView class]];
[componentViewFactory registerComponentViewClass:[RCTSwitchComponentView class]];
return componentViewFactory;