Step support for SliderIOS

Summary: Add step support to SliderIOS
Closes https://github.com/facebook/react-native/pull/3746

Reviewed By: svcscm

Differential Revision: D2595360

Pulled By: nicklockwood

fb-gh-sync-id: 4adf8bcdf46c709776d779244ba3de2b40eb27d6
This commit is contained in:
Ivan Sorokin 2015-10-30 07:12:24 -07:00 committed by facebook-github-bot-4
parent c16ffd3162
commit e409e20d2b
3 changed files with 31 additions and 2 deletions

View File

@ -41,6 +41,13 @@ var SliderIOS = React.createClass({
*/
value: PropTypes.number,
/**
* Step value of the slider. The value should be
* between 0 and (maximumValue - minimumValue).
* Default value is 0.
*/
step: PropTypes.number,
/**
* Initial minimum value of the slider. Default value is 0.
*/
@ -103,6 +110,7 @@ var SliderIOS = React.createClass({
<RCTSlider
style={[styles.slider, this.props.style]}
value={this.props.value}
step={this.props.step}
maximumValue={this.props.maximumValue}
minimumValue={this.props.minimumValue}
minimumTrackTintColor={this.props.minimumTrackTintColor}

View File

@ -14,5 +14,7 @@
@interface RCTSlider : UISlider
@property (nonatomic, copy) RCTBubblingEventBlock onChange;
@property (nonatomic, assign) float step;
@property (nonatomic, assign) float lastValue;
@end

View File

@ -30,12 +30,30 @@ RCT_EXPORT_MODULE()
static void RCTSendSliderEvent(RCTSlider *sender, BOOL continuous)
{
if (sender.onChange) {
float value = sender.value;
if (sender.step > 0 &&
sender.step <= (sender.maximumValue - sender.minimumValue)) {
value =
MAX(sender.minimumValue,
MIN(sender.maximumValue,
sender.minimumValue + round((sender.value - sender.minimumValue) / sender.step) * sender.step
)
);
}
if (!continuous) {
sender.value = value;
}
if (sender.onChange && (sender.lastValue != value || !continuous)) {
sender.onChange(@{
@"value": @(sender.value),
@"value": @(value),
@"continuous": @(continuous),
});
}
sender.lastValue = value;
}
- (void)sliderValueChanged:(RCTSlider *)sender
@ -49,6 +67,7 @@ static void RCTSendSliderEvent(RCTSlider *sender, BOOL continuous)
}
RCT_EXPORT_VIEW_PROPERTY(value, float);
RCT_EXPORT_VIEW_PROPERTY(step, float);
RCT_EXPORT_VIEW_PROPERTY(minimumValue, float);
RCT_EXPORT_VIEW_PROPERTY(maximumValue, float);
RCT_EXPORT_VIEW_PROPERTY(minimumTrackTintColor, UIColor);