2016-01-29 11:58:35 +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-01-29 11:58:35 +00:00
|
|
|
*
|
2018-05-11 02:06:46 +00:00
|
|
|
* @format
|
2018-08-08 17:39:16 +00:00
|
|
|
* @flow
|
2016-01-29 11:58:35 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2018-03-03 23:04:46 +00:00
|
|
|
const ColorPropType = require('ColorPropType');
|
|
|
|
const React = require('React');
|
|
|
|
const ReactPropTypes = require('prop-types');
|
|
|
|
const StyleSheet = require('StyleSheet');
|
|
|
|
const StyleSheetPropType = require('StyleSheetPropType');
|
2017-03-24 07:22:57 +00:00
|
|
|
const ViewPropTypes = require('ViewPropTypes');
|
2018-03-03 23:04:46 +00:00
|
|
|
const ViewStylePropTypes = require('ViewStylePropTypes');
|
2016-01-29 11:58:35 +00:00
|
|
|
|
2018-03-03 23:04:46 +00:00
|
|
|
const processColor = require('processColor');
|
|
|
|
const requireNativeComponent = require('requireNativeComponent');
|
2016-01-29 11:58:35 +00:00
|
|
|
|
2018-06-10 22:34:37 +00:00
|
|
|
const DropdownPicker = requireNativeComponent('AndroidDropdownPicker');
|
|
|
|
const DialogPicker = requireNativeComponent('AndroidDialogPicker');
|
|
|
|
|
2018-03-03 23:04:46 +00:00
|
|
|
const REF_PICKER = 'picker';
|
|
|
|
const MODE_DROPDOWN = 'dropdown';
|
2016-01-29 11:58:35 +00:00
|
|
|
|
2018-03-03 23:04:46 +00:00
|
|
|
const pickerStyleType = StyleSheetPropType({
|
2016-01-29 11:58:35 +00:00
|
|
|
...ViewStylePropTypes,
|
|
|
|
color: ColorPropType,
|
|
|
|
});
|
|
|
|
|
|
|
|
type Event = Object;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Not exposed as a public API - use <Picker> instead.
|
|
|
|
*/
|
2018-05-11 02:06:46 +00:00
|
|
|
class PickerAndroid extends React.Component<
|
|
|
|
{
|
|
|
|
style?: $FlowFixMe,
|
|
|
|
selectedValue?: any,
|
|
|
|
enabled?: boolean,
|
|
|
|
mode?: 'dialog' | 'dropdown',
|
|
|
|
onValueChange?: Function,
|
|
|
|
prompt?: string,
|
|
|
|
testID?: string,
|
|
|
|
},
|
|
|
|
*,
|
|
|
|
> {
|
2018-08-13 18:14:35 +00:00
|
|
|
/* $FlowFixMe(>=0.78.0 site=react_native_android_fb) This issue was found
|
|
|
|
* when making Flow check .android.js files. */
|
2016-07-26 08:00:02 +00:00
|
|
|
static propTypes = {
|
2017-03-24 07:22:57 +00:00
|
|
|
...ViewPropTypes,
|
2016-01-29 11:58:35 +00:00
|
|
|
style: pickerStyleType,
|
2017-06-22 20:31:50 +00:00
|
|
|
selectedValue: ReactPropTypes.any,
|
2016-01-29 11:58:35 +00:00
|
|
|
enabled: ReactPropTypes.bool,
|
|
|
|
mode: ReactPropTypes.oneOf(['dialog', 'dropdown']),
|
|
|
|
onValueChange: ReactPropTypes.func,
|
|
|
|
prompt: ReactPropTypes.string,
|
|
|
|
testID: ReactPropTypes.string,
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
2016-01-29 11:58:35 +00:00
|
|
|
|
2018-08-13 18:14:35 +00:00
|
|
|
/* $FlowFixMe(>=0.78.0 site=react_native_android_fb) This issue was found
|
|
|
|
* when making Flow check .android.js files. */
|
2016-07-26 08:00:02 +00:00
|
|
|
constructor(props, context) {
|
|
|
|
super(props, context);
|
2018-03-03 23:04:46 +00:00
|
|
|
const state = this._stateFromProps(props);
|
2016-07-26 08:00:02 +00:00
|
|
|
|
|
|
|
this.state = {
|
2016-05-07 22:51:22 +00:00
|
|
|
...state,
|
|
|
|
initialSelectedIndex: state.selectedIndex,
|
|
|
|
};
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2016-01-29 11:58:35 +00:00
|
|
|
|
2018-08-13 18:14:35 +00:00
|
|
|
/* $FlowFixMe(>=0.78.0 site=react_native_android_fb) This issue was found
|
|
|
|
* when making Flow check .android.js files. */
|
2018-02-08 18:26:45 +00:00
|
|
|
UNSAFE_componentWillReceiveProps(nextProps) {
|
2016-01-29 11:58:35 +00:00
|
|
|
this.setState(this._stateFromProps(nextProps));
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2016-01-29 11:58:35 +00:00
|
|
|
|
|
|
|
// Translate prop and children into stuff that the native picker understands.
|
2018-05-11 02:06:46 +00:00
|
|
|
_stateFromProps = props => {
|
2018-03-03 23:04:46 +00:00
|
|
|
let selectedIndex = 0;
|
2016-10-15 01:50:19 +00:00
|
|
|
const items = React.Children.map(props.children, (child, index) => {
|
2016-01-29 11:58:35 +00:00
|
|
|
if (child.props.value === props.selectedValue) {
|
|
|
|
selectedIndex = index;
|
|
|
|
}
|
2016-08-09 13:32:41 +00:00
|
|
|
const childProps = {
|
2016-01-29 11:58:35 +00:00
|
|
|
value: child.props.value,
|
|
|
|
label: child.props.label,
|
|
|
|
};
|
|
|
|
if (child.props.color) {
|
2018-08-13 18:14:35 +00:00
|
|
|
/* $FlowFixMe(>=0.78.0 site=react_native_android_fb) This issue was
|
|
|
|
* found when making Flow check .android.js files. */
|
2016-01-29 11:58:35 +00:00
|
|
|
childProps.color = processColor(child.props.color);
|
|
|
|
}
|
|
|
|
return childProps;
|
|
|
|
});
|
|
|
|
return {selectedIndex, items};
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
2016-01-29 11:58:35 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
render() {
|
2018-05-11 02:06:46 +00:00
|
|
|
const Picker =
|
|
|
|
this.props.mode === MODE_DROPDOWN ? DropdownPicker : DialogPicker;
|
2016-01-29 11:58:35 +00:00
|
|
|
|
2018-03-03 23:04:46 +00:00
|
|
|
const nativeProps = {
|
2016-01-29 11:58:35 +00:00
|
|
|
enabled: this.props.enabled,
|
|
|
|
items: this.state.items,
|
|
|
|
mode: this.props.mode,
|
|
|
|
onSelect: this._onChange,
|
|
|
|
prompt: this.props.prompt,
|
2016-05-07 22:51:22 +00:00
|
|
|
selected: this.state.initialSelectedIndex,
|
2016-01-29 11:58:35 +00:00
|
|
|
testID: this.props.testID,
|
|
|
|
style: [styles.pickerAndroid, this.props.style],
|
2018-08-13 18:14:35 +00:00
|
|
|
/* $FlowFixMe(>=0.78.0 site=react_native_android_fb) This issue was found
|
|
|
|
* when making Flow check .android.js files. */
|
2016-09-07 23:49:47 +00:00
|
|
|
accessibilityLabel: this.props.accessibilityLabel,
|
2016-01-29 11:58:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return <Picker ref={REF_PICKER} {...nativeProps} />;
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2016-01-29 11:58:35 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
_onChange = (event: Event) => {
|
2016-01-29 11:58:35 +00:00
|
|
|
if (this.props.onValueChange) {
|
2018-03-03 23:04:46 +00:00
|
|
|
const position = event.nativeEvent.position;
|
2016-01-29 11:58:35 +00:00
|
|
|
if (position >= 0) {
|
2018-08-13 18:14:35 +00:00
|
|
|
/* $FlowFixMe(>=0.78.0 site=react_native_android_fb) This issue was
|
|
|
|
* found when making Flow check .android.js files. */
|
2018-03-03 23:04:46 +00:00
|
|
|
const children = React.Children.toArray(this.props.children);
|
|
|
|
const value = children[position].props.value;
|
2018-08-13 18:14:35 +00:00
|
|
|
/* $FlowFixMe(>=0.78.0 site=react_native_android_fb) This issue was
|
|
|
|
* found when making Flow check .android.js files. */
|
2016-02-19 14:49:41 +00:00
|
|
|
this.props.onValueChange(value, position);
|
2016-01-29 11:58:35 +00:00
|
|
|
} else {
|
2016-02-19 14:49:41 +00:00
|
|
|
this.props.onValueChange(null, position);
|
2016-01-29 11:58:35 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-13 18:14:35 +00:00
|
|
|
/* $FlowFixMe(>=0.78.0 site=react_native_android_fb) This issue was found
|
|
|
|
* when making Flow check .android.js files. */
|
2016-05-07 22:51:22 +00:00
|
|
|
this._lastNativePosition = event.nativeEvent.position;
|
|
|
|
this.forceUpdate();
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
2016-05-07 22:51:22 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
componentDidMount() {
|
2018-08-13 18:14:35 +00:00
|
|
|
/* $FlowFixMe(>=0.78.0 site=react_native_android_fb) This issue was found
|
|
|
|
* when making Flow check .android.js files. */
|
2016-05-07 22:51:22 +00:00
|
|
|
this._lastNativePosition = this.state.initialSelectedIndex;
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2016-01-29 11:58:35 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
componentDidUpdate() {
|
2016-01-29 11:58:35 +00:00
|
|
|
// The picker is a controlled component. This means we expect the
|
|
|
|
// on*Change handlers to be in charge of updating our
|
|
|
|
// `selectedValue` prop. That way they can also
|
|
|
|
// disallow/undo/mutate the selection of certain values. In other
|
|
|
|
// words, the embedder of this component should be the source of
|
|
|
|
// truth, not the native component.
|
2018-05-11 02:06:46 +00:00
|
|
|
if (
|
|
|
|
this.refs[REF_PICKER] &&
|
2018-08-13 18:14:35 +00:00
|
|
|
/* $FlowFixMe(>=0.78.0 site=react_native_android_fb) This issue was found
|
|
|
|
* when making Flow check .android.js files. */
|
2018-05-11 02:06:46 +00:00
|
|
|
this.state.selectedIndex !== this._lastNativePosition
|
|
|
|
) {
|
|
|
|
this.refs[REF_PICKER].setNativeProps({
|
|
|
|
selected: this.state.selectedIndex,
|
|
|
|
});
|
2018-08-13 18:14:35 +00:00
|
|
|
/* $FlowFixMe(>=0.78.0 site=react_native_android_fb) This issue was found
|
|
|
|
* when making Flow check .android.js files. */
|
2016-05-07 22:51:22 +00:00
|
|
|
this._lastNativePosition = this.state.selectedIndex;
|
2016-01-29 11:58:35 +00:00
|
|
|
}
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-29 11:58:35 +00:00
|
|
|
|
2018-03-03 23:04:46 +00:00
|
|
|
const styles = StyleSheet.create({
|
2016-01-29 11:58:35 +00:00
|
|
|
pickerAndroid: {
|
|
|
|
// The picker will conform to whatever width is given, but we do
|
|
|
|
// have to set the component's height explicitly on the
|
|
|
|
// surrounding view to ensure it gets rendered.
|
|
|
|
// TODO would be better to export a native constant for this,
|
|
|
|
// like in iOS the RCTDatePickerManager.m
|
|
|
|
height: 50,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = PickerAndroid;
|