Skip to content

Commit

Permalink
Fabric: Add Fabric-compatible Slider component to iOS (ObjC code)
Browse files Browse the repository at this point in the history
Summary: Objective-C side of the Fabric-compatible slider component for iOS.

Reviewed By: mdvacca

Differential Revision: D13745263

fbshipit-source-id: 647631d6fc86f81a5d4f735c507636ed9c468093
  • Loading branch information
JoshuaGross authored and facebook-github-bot committed Jan 23, 2019
1 parent e941284 commit 6cbd842
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions React/Fabric/Mounting/RCTComponentViewFactory.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#import "RCTParagraphComponentView.h"
#import "RCTRootComponentView.h"
#import "RCTActivityIndicatorViewComponentView.h"
#import "RCTSliderComponentView.h"
#import "RCTSwitchComponentView.h"

using namespace facebook::react;
Expand All @@ -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;
Expand Down

0 comments on commit 6cbd842

Please sign in to comment.