2016-04-06 11:49:47 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-04-06 11:49:47 +00:00
|
|
|
*
|
2018-05-11 02:06:46 +00:00
|
|
|
* @format
|
2016-04-06 11:49:47 +00:00
|
|
|
* @flow
|
|
|
|
*/
|
2018-05-11 02:06:46 +00:00
|
|
|
|
2016-04-06 11:49:47 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-05-14 07:09:16 +00:00
|
|
|
const ReactNative = require('ReactNative');
|
2018-03-03 23:04:46 +00:00
|
|
|
const Platform = require('Platform');
|
|
|
|
const React = require('React');
|
|
|
|
const StyleSheet = require('StyleSheet');
|
2016-04-06 11:49:47 +00:00
|
|
|
|
2018-03-03 23:04:46 +00:00
|
|
|
const requireNativeComponent = require('requireNativeComponent');
|
2016-04-06 11:49:47 +00:00
|
|
|
|
2018-05-14 07:09:16 +00:00
|
|
|
import type {ImageSource} from 'ImageSource';
|
|
|
|
import type {ViewStyleProp} from 'StyleSheet';
|
|
|
|
import type {ColorValue} from 'StyleSheetTypes';
|
|
|
|
import type {ViewProps} from 'ViewPropTypes';
|
|
|
|
|
2018-06-01 19:37:22 +00:00
|
|
|
const RCTSlider = requireNativeComponent('RCTSlider');
|
|
|
|
|
2016-04-06 11:49:47 +00:00
|
|
|
type Event = Object;
|
|
|
|
|
2018-05-14 07:09:16 +00:00
|
|
|
type IOSProps = $ReadOnly<{|
|
2018-06-03 05:35:45 +00:00
|
|
|
/**
|
|
|
|
* Assigns a single image for the track. Only static images are supported.
|
|
|
|
* The center pixel of the image will be stretched to fill the track.
|
|
|
|
*/
|
2018-05-14 07:09:16 +00:00
|
|
|
trackImage?: ?ImageSource,
|
2018-06-03 05:35:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Assigns a minimum track image. Only static images are supported. The
|
|
|
|
* rightmost pixel of the image will be stretched to fill the track.
|
|
|
|
*/
|
2018-05-14 07:09:16 +00:00
|
|
|
minimumTrackImage?: ?ImageSource,
|
2018-06-03 05:35:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Assigns a maximum track image. Only static images are supported. The
|
|
|
|
* leftmost pixel of the image will be stretched to fill the track.
|
|
|
|
*/
|
2018-05-14 07:09:16 +00:00
|
|
|
maximumTrackImage?: ?ImageSource,
|
2018-06-03 05:35:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets an image for the thumb. Only static images are supported.
|
|
|
|
*/
|
2018-05-14 07:09:16 +00:00
|
|
|
thumbImage?: ?ImageSource,
|
|
|
|
|}>;
|
|
|
|
|
|
|
|
type AndroidProps = $ReadOnly<{|
|
2018-06-03 05:35:45 +00:00
|
|
|
/**
|
|
|
|
* Color of the foreground switch grip.
|
|
|
|
* @platform android
|
|
|
|
*/
|
2018-05-14 07:09:16 +00:00
|
|
|
thumbTintColor?: ?ColorValue,
|
|
|
|
|}>;
|
|
|
|
|
|
|
|
type Props = $ReadOnly<{|
|
|
|
|
...ViewProps,
|
|
|
|
...IOSProps,
|
|
|
|
...AndroidProps,
|
2018-06-03 05:35:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to style and layout the `Slider`. See `StyleSheet.js` and
|
|
|
|
* `ViewStylePropTypes.js` for more info.
|
|
|
|
*/
|
2018-05-14 07:09:16 +00:00
|
|
|
style?: ?ViewStyleProp,
|
2018-06-03 05:35:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initial value of the slider. The value should be between minimumValue
|
|
|
|
* and maximumValue, which default to 0 and 1 respectively.
|
|
|
|
* Default value is 0.
|
|
|
|
*
|
|
|
|
* *This is not a controlled component*, you don't need to update the
|
|
|
|
* value during dragging.
|
|
|
|
*/
|
2018-05-14 07:09:16 +00:00
|
|
|
value?: ?number,
|
2018-06-03 05:35:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Step value of the slider. The value should be
|
|
|
|
* between 0 and (maximumValue - minimumValue).
|
|
|
|
* Default value is 0.
|
|
|
|
*/
|
2018-05-14 07:09:16 +00:00
|
|
|
step?: ?number,
|
2018-06-03 05:35:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initial minimum value of the slider. Default value is 0.
|
|
|
|
*/
|
2018-05-14 07:09:16 +00:00
|
|
|
minimumValue?: ?number,
|
2018-06-03 05:35:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initial maximum value of the slider. Default value is 1.
|
|
|
|
*/
|
2018-05-14 07:09:16 +00:00
|
|
|
maximumValue?: ?number,
|
2018-06-03 05:35:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The color used for the track to the left of the button.
|
|
|
|
* Overrides the default blue gradient image on iOS.
|
|
|
|
*/
|
2018-05-14 07:09:16 +00:00
|
|
|
minimumTrackTintColor?: ?ColorValue,
|
2018-06-03 05:35:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The color used for the track to the right of the button.
|
|
|
|
* Overrides the default blue gradient image on iOS.
|
|
|
|
*/
|
2018-05-14 07:09:16 +00:00
|
|
|
maximumTrackTintColor?: ?ColorValue,
|
2018-06-03 05:35:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If true the user won't be able to move the slider.
|
|
|
|
* Default value is false.
|
|
|
|
*/
|
2018-05-14 07:09:16 +00:00
|
|
|
disabled?: ?boolean,
|
2018-06-03 05:35:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback continuously called while the user is dragging the slider.
|
|
|
|
*/
|
2018-05-14 07:09:16 +00:00
|
|
|
onValueChange?: ?Function,
|
2018-06-03 05:35:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback that is called when the user releases the slider,
|
|
|
|
* regardless if the value has changed. The current value is passed
|
|
|
|
* as an argument to the callback handler.
|
|
|
|
*/
|
2018-05-14 07:09:16 +00:00
|
|
|
onSlidingComplete?: ?Function,
|
2018-06-03 05:35:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to locate this view in UI automation tests.
|
|
|
|
*/
|
2018-05-14 07:09:16 +00:00
|
|
|
testID?: ?string,
|
|
|
|
|}>;
|
|
|
|
|
2016-04-06 15:50:33 +00:00
|
|
|
/**
|
|
|
|
* A component used to select a single value from a range of values.
|
2017-10-31 16:21:23 +00:00
|
|
|
*
|
|
|
|
* ### Usage
|
|
|
|
*
|
|
|
|
* The example below shows how to use `Slider` to change
|
|
|
|
* a value used by `Text`. The value is stored using
|
|
|
|
* the state of the root component (`App`). The same component
|
|
|
|
* subscribes to the `onValueChange` of `Slider` and changes
|
|
|
|
* the value using `setState`.
|
|
|
|
*
|
|
|
|
*```
|
|
|
|
* import React from 'react';
|
|
|
|
* import { StyleSheet, Text, View, Slider } from 'react-native';
|
|
|
|
*
|
|
|
|
* export default class App extends React.Component {
|
|
|
|
* constructor(props) {
|
|
|
|
* super(props);
|
|
|
|
* this.state = {
|
|
|
|
* value: 50
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* change(value) {
|
|
|
|
* this.setState(() => {
|
|
|
|
* return {
|
|
|
|
* value: parseFloat(value)
|
|
|
|
* };
|
|
|
|
* });
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* render() {
|
|
|
|
* const {value} = this.state;
|
|
|
|
* return (
|
|
|
|
* <View style={styles.container}>
|
|
|
|
* <Text style={styles.text}>{String(value)}</Text>
|
|
|
|
* <Slider
|
|
|
|
* step={1}
|
|
|
|
* maximumValue={100}
|
|
|
|
* onValueChange={this.change.bind(this)}
|
|
|
|
* value={value} />
|
|
|
|
* </View>
|
|
|
|
* );
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* const styles = StyleSheet.create({
|
|
|
|
* container: {
|
|
|
|
* flex: 1,
|
|
|
|
* flexDirection: 'column',
|
|
|
|
* justifyContent: 'center'
|
|
|
|
* },
|
|
|
|
* text: {
|
|
|
|
* fontSize: 50,
|
|
|
|
* textAlign: 'center'
|
|
|
|
* }
|
|
|
|
* });
|
|
|
|
*```
|
|
|
|
*
|
2016-04-06 15:50:33 +00:00
|
|
|
*/
|
2018-06-03 05:35:48 +00:00
|
|
|
const Slider = (
|
|
|
|
props: $ReadOnly<{|
|
|
|
|
...Props,
|
|
|
|
forwardedRef?: ?React.Ref<'RCTActivityIndicatorView'>,
|
|
|
|
|}>,
|
|
|
|
) => {
|
|
|
|
const style = StyleSheet.compose(styles.slider, props.style);
|
|
|
|
|
|
|
|
const onValueChange =
|
|
|
|
props.onValueChange &&
|
|
|
|
((event: Event) => {
|
|
|
|
let userEvent = true;
|
|
|
|
if (Platform.OS === 'android') {
|
|
|
|
// On Android there's a special flag telling us the user is
|
|
|
|
// dragging the slider.
|
|
|
|
userEvent = event.nativeEvent.fromUser;
|
|
|
|
}
|
|
|
|
props.onValueChange &&
|
|
|
|
userEvent &&
|
|
|
|
props.onValueChange(event.nativeEvent.value);
|
|
|
|
});
|
|
|
|
|
|
|
|
const onChange = onValueChange;
|
|
|
|
|
|
|
|
const onSlidingComplete =
|
|
|
|
props.onSlidingComplete &&
|
|
|
|
((event: Event) => {
|
|
|
|
props.onSlidingComplete &&
|
|
|
|
props.onSlidingComplete(event.nativeEvent.value);
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<RCTSlider
|
|
|
|
{...props}
|
|
|
|
style={style}
|
|
|
|
onChange={onChange}
|
|
|
|
onSlidingComplete={onSlidingComplete}
|
|
|
|
onValueChange={onValueChange}
|
|
|
|
enabled={!props.disabled}
|
|
|
|
onStartShouldSetResponder={() => true}
|
|
|
|
onResponderTerminationRequest={() => false}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
// $FlowFixMe - TODO T29156721 `React.forwardRef` is not defined in Flow, yet.
|
|
|
|
const SliderWithRef = React.forwardRef((props: Props, ref) => {
|
|
|
|
return <Slider {...props} forwardedRef={ref} />;
|
2016-04-06 11:49:47 +00:00
|
|
|
});
|
|
|
|
|
2018-06-03 05:35:48 +00:00
|
|
|
SliderWithRef.defaultProps = {
|
|
|
|
disabled: false,
|
|
|
|
value: 0,
|
|
|
|
minimumValue: 0,
|
|
|
|
maximumValue: 1,
|
|
|
|
step: 0,
|
|
|
|
};
|
|
|
|
SliderWithRef.displayName = 'Slider';
|
|
|
|
|
2016-04-06 11:49:47 +00:00
|
|
|
let styles;
|
|
|
|
if (Platform.OS === 'ios') {
|
|
|
|
styles = StyleSheet.create({
|
|
|
|
slider: {
|
|
|
|
height: 40,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
styles = StyleSheet.create({
|
|
|
|
slider: {},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-03 05:35:48 +00:00
|
|
|
module.exports = (SliderWithRef: Class<ReactNative.NativeComponent<Props>>);
|