forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
e941284
commit 6cbd842
Showing
3 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
React/Fabric/Mounting/ComponentViews/Slider/RCTSliderComponentView.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
92 changes: 92 additions & 0 deletions
92
React/Fabric/Mounting/ComponentViews/Slider/RCTSliderComponentView.mm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters