Skip to content

Commit 5259450

Browse files
elicwhitefacebook-github-bot
authored andcommitted
Slider to ES6 Class
Reviewed By: yungsters Differential Revision: D8246422 fbshipit-source-id: 1955ae87abe077115ac8f8ea105be85db8ea66b4
1 parent 615daeb commit 5259450

File tree

1 file changed

+58
-89
lines changed

1 file changed

+58
-89
lines changed

Libraries/Components/Slider/Slider.js

+58-89
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,11 @@
1010

1111
'use strict';
1212

13-
const Image = require('Image');
14-
const ColorPropType = require('ColorPropType');
15-
const NativeMethodsMixin = require('NativeMethodsMixin');
1613
const ReactNative = require('ReactNative');
17-
const ReactNativeViewAttributes = require('ReactNativeViewAttributes');
1814
const Platform = require('Platform');
1915
const React = require('React');
20-
const PropTypes = require('prop-types');
2116
const StyleSheet = require('StyleSheet');
22-
const ViewPropTypes = require('ViewPropTypes');
2317

24-
const createReactClass = require('create-react-class');
2518
const requireNativeComponent = require('requireNativeComponent');
2619

2720
import type {ImageSource} from 'ImageSource';
@@ -200,89 +193,65 @@ type Props = $ReadOnly<{|
200193
*```
201194
*
202195
*/
203-
const Slider = createReactClass({
204-
displayName: 'Slider',
205-
mixins: [NativeMethodsMixin],
206-
207-
propTypes: {
208-
...ViewPropTypes,
209-
style: ViewPropTypes.style,
210-
value: PropTypes.number,
211-
step: PropTypes.number,
212-
minimumValue: PropTypes.number,
213-
maximumValue: PropTypes.number,
214-
minimumTrackTintColor: ColorPropType,
215-
maximumTrackTintColor: ColorPropType,
216-
disabled: PropTypes.bool,
217-
trackImage: Image.propTypes.source,
218-
minimumTrackImage: Image.propTypes.source,
219-
maximumTrackImage: Image.propTypes.source,
220-
thumbImage: Image.propTypes.source,
221-
thumbTintColor: ColorPropType,
222-
onValueChange: PropTypes.func,
223-
onSlidingComplete: PropTypes.func,
224-
testID: PropTypes.string,
225-
},
226-
227-
getDefaultProps: function(): any {
228-
return {
229-
disabled: false,
230-
value: 0,
231-
minimumValue: 0,
232-
maximumValue: 1,
233-
step: 0,
234-
};
235-
},
236-
237-
viewConfig: {
238-
uiViewClassName: 'RCTSlider',
239-
validAttributes: {
240-
...ReactNativeViewAttributes.RCTView,
241-
value: true,
242-
},
243-
},
244-
245-
render: function() {
246-
const style = StyleSheet.compose(styles.slider, this.props.style);
247-
248-
const onValueChange =
249-
this.props.onValueChange &&
250-
((event: Event) => {
251-
let userEvent = true;
252-
if (Platform.OS === 'android') {
253-
// On Android there's a special flag telling us the user is
254-
// dragging the slider.
255-
userEvent = event.nativeEvent.fromUser;
256-
}
257-
this.props.onValueChange &&
258-
userEvent &&
259-
this.props.onValueChange(event.nativeEvent.value);
260-
});
261-
262-
const onChange = onValueChange;
263-
264-
const onSlidingComplete =
265-
this.props.onSlidingComplete &&
266-
((event: Event) => {
267-
this.props.onSlidingComplete &&
268-
this.props.onSlidingComplete(event.nativeEvent.value);
269-
});
270-
271-
return (
272-
<RCTSlider
273-
{...this.props}
274-
style={style}
275-
onChange={onChange}
276-
onSlidingComplete={onSlidingComplete}
277-
onValueChange={onValueChange}
278-
enabled={!this.props.disabled}
279-
onStartShouldSetResponder={() => true}
280-
onResponderTerminationRequest={() => false}
281-
/>
282-
);
283-
},
196+
const Slider = (
197+
props: $ReadOnly<{|
198+
...Props,
199+
forwardedRef?: ?React.Ref<'RCTActivityIndicatorView'>,
200+
|}>,
201+
) => {
202+
const style = StyleSheet.compose(styles.slider, props.style);
203+
204+
const onValueChange =
205+
props.onValueChange &&
206+
((event: Event) => {
207+
let userEvent = true;
208+
if (Platform.OS === 'android') {
209+
// On Android there's a special flag telling us the user is
210+
// dragging the slider.
211+
userEvent = event.nativeEvent.fromUser;
212+
}
213+
props.onValueChange &&
214+
userEvent &&
215+
props.onValueChange(event.nativeEvent.value);
216+
});
217+
218+
const onChange = onValueChange;
219+
220+
const onSlidingComplete =
221+
props.onSlidingComplete &&
222+
((event: Event) => {
223+
props.onSlidingComplete &&
224+
props.onSlidingComplete(event.nativeEvent.value);
225+
});
226+
227+
return (
228+
<RCTSlider
229+
{...props}
230+
style={style}
231+
onChange={onChange}
232+
onSlidingComplete={onSlidingComplete}
233+
onValueChange={onValueChange}
234+
enabled={!props.disabled}
235+
onStartShouldSetResponder={() => true}
236+
onResponderTerminationRequest={() => false}
237+
/>
238+
);
239+
};
240+
241+
// $FlowFixMe - TODO T29156721 `React.forwardRef` is not defined in Flow, yet.
242+
const SliderWithRef = React.forwardRef((props: Props, ref) => {
243+
return <Slider {...props} forwardedRef={ref} />;
284244
});
285245

246+
SliderWithRef.defaultProps = {
247+
disabled: false,
248+
value: 0,
249+
minimumValue: 0,
250+
maximumValue: 1,
251+
step: 0,
252+
};
253+
SliderWithRef.displayName = 'Slider';
254+
286255
let styles;
287256
if (Platform.OS === 'ios') {
288257
styles = StyleSheet.create({
@@ -296,4 +265,4 @@ if (Platform.OS === 'ios') {
296265
});
297266
}
298267

299-
module.exports = ((Slider: any): Class<ReactNative.NativeComponent<Props>>);
268+
module.exports = (SliderWithRef: Class<ReactNative.NativeComponent<Props>>);

0 commit comments

Comments
 (0)