2016-01-29 03:58:35 -08:00
|
|
|
/**
|
2018-09-11 15:27:47 -07:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2016-01-29 03:58:35 -08:00
|
|
|
*
|
2018-02-16 18:24:55 -08: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 03:58:35 -08:00
|
|
|
*
|
2018-05-10 19:06:46 -07:00
|
|
|
* @format
|
2016-01-29 03:58:35 -08:00
|
|
|
* @flow
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2018-03-03 15:04:46 -08:00
|
|
|
const PickerAndroid = require('PickerAndroid');
|
2018-08-22 18:22:00 -07:00
|
|
|
const PickerIOS = require('PickerIOS');
|
2018-03-03 15:04:46 -08:00
|
|
|
const Platform = require('Platform');
|
2018-08-22 18:22:00 -07:00
|
|
|
const React = require('React');
|
2018-03-03 15:04:46 -08:00
|
|
|
const UnimplementedView = require('UnimplementedView');
|
2016-01-29 03:58:35 -08:00
|
|
|
|
2018-09-23 22:33:25 -07:00
|
|
|
import type {TextStyleProp} from 'StyleSheet';
|
|
|
|
import type {ColorValue} from 'StyleSheetTypes';
|
2016-01-29 03:58:35 -08:00
|
|
|
|
2018-03-03 15:04:46 -08:00
|
|
|
const MODE_DIALOG = 'dialog';
|
|
|
|
const MODE_DROPDOWN = 'dropdown';
|
2016-01-29 03:58:35 -08:00
|
|
|
|
2018-09-23 22:33:25 -07:00
|
|
|
type PickerItemProps = $ReadOnly<{|
|
|
|
|
/**
|
|
|
|
* Text to display for this item.
|
|
|
|
*/
|
2018-05-10 19:06:46 -07:00
|
|
|
label: string,
|
2018-09-23 22:33:25 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The value to be passed to picker's `onValueChange` callback when
|
|
|
|
* this item is selected. Can be a string or an integer.
|
|
|
|
*/
|
2018-05-10 19:06:46 -07:00
|
|
|
value?: any,
|
2018-09-23 22:33:25 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Color of this item's text.
|
|
|
|
* @platform android
|
|
|
|
*/
|
|
|
|
color?: ColorValue,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to locate the item in end-to-end tests.
|
|
|
|
*/
|
2018-05-10 19:06:46 -07:00
|
|
|
testID?: string,
|
2018-09-23 22:33:25 -07:00
|
|
|
|}>;
|
2017-04-05 10:16:30 -07:00
|
|
|
|
2018-09-23 22:33:25 -07:00
|
|
|
/**
|
|
|
|
* Individual selectable item in a Picker.
|
|
|
|
*/
|
|
|
|
class PickerItem extends React.Component<PickerItemProps> {
|
2018-05-10 19:06:46 -07:00
|
|
|
render() {
|
|
|
|
// The items are not rendered directly
|
|
|
|
throw null;
|
|
|
|
}
|
2017-04-05 10:16:30 -07:00
|
|
|
}
|
|
|
|
|
2018-09-23 22:33:25 -07:00
|
|
|
type PickerProps = $ReadOnly<{|
|
|
|
|
children?: React.Node,
|
|
|
|
style?: ?TextStyleProp,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Value matching value of one of the items. Can be a string or an integer.
|
|
|
|
*/
|
|
|
|
selectedValue?: any,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback for when an item is selected. This is called with the following parameters:
|
|
|
|
* - `itemValue`: the `value` prop of the item that was selected
|
|
|
|
* - `itemPosition`: the index of the selected item in this picker
|
|
|
|
*/
|
|
|
|
onValueChange?: ?(newValue: any, newIndex: number) => mixed,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If set to false, the picker will be disabled, i.e. the user will not be able to make a
|
|
|
|
* selection.
|
|
|
|
* @platform android
|
|
|
|
*/
|
|
|
|
enabled?: ?boolean,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* On Android, specifies how to display the selection items when the user taps on the picker:
|
|
|
|
*
|
|
|
|
* - 'dialog': Show a modal dialog. This is the default.
|
|
|
|
* - 'dropdown': Shows a dropdown anchored to the picker view
|
|
|
|
*
|
|
|
|
* @platform android
|
|
|
|
*/
|
|
|
|
mode?: ?('dialog' | 'dropdown'),
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Style to apply to each of the item labels.
|
|
|
|
* @platform ios
|
|
|
|
*/
|
|
|
|
itemStyle?: ?TextStyleProp,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prompt string for this picker, used on Android in dialog mode as the title of the dialog.
|
|
|
|
* @platform android
|
|
|
|
*/
|
|
|
|
prompt?: ?string,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to locate this view in end-to-end tests.
|
|
|
|
*/
|
|
|
|
testID?: ?string,
|
|
|
|
|}>;
|
|
|
|
|
2016-01-29 03:58:35 -08:00
|
|
|
/**
|
|
|
|
* Renders the native picker component on iOS and Android. Example:
|
|
|
|
*
|
|
|
|
* <Picker
|
|
|
|
* selectedValue={this.state.language}
|
2017-04-28 03:59:07 -07:00
|
|
|
* onValueChange={(itemValue, itemIndex) => this.setState({language: itemValue})}>
|
2016-01-29 03:58:35 -08:00
|
|
|
* <Picker.Item label="Java" value="java" />
|
|
|
|
* <Picker.Item label="JavaScript" value="js" />
|
|
|
|
* </Picker>
|
|
|
|
*/
|
2018-09-23 22:33:25 -07:00
|
|
|
class Picker extends React.Component<PickerProps> {
|
2018-05-10 19:06:46 -07:00
|
|
|
/**
|
|
|
|
* On Android, display the options in a dialog.
|
|
|
|
*/
|
|
|
|
static MODE_DIALOG = MODE_DIALOG;
|
2016-01-29 03:58:35 -08:00
|
|
|
|
2018-05-10 19:06:46 -07:00
|
|
|
/**
|
|
|
|
* On Android, display the options in a dropdown (this is the default).
|
|
|
|
*/
|
|
|
|
static MODE_DROPDOWN = MODE_DROPDOWN;
|
2016-01-29 03:58:35 -08:00
|
|
|
|
2018-05-10 19:06:46 -07:00
|
|
|
static Item = PickerItem;
|
2017-04-05 10:16:30 -07:00
|
|
|
|
2018-05-10 19:06:46 -07:00
|
|
|
static defaultProps = {
|
|
|
|
mode: MODE_DIALOG,
|
|
|
|
};
|
2016-01-29 03:58:35 -08:00
|
|
|
|
2018-05-10 19:06:46 -07:00
|
|
|
render() {
|
|
|
|
if (Platform.OS === 'ios') {
|
2018-09-27 11:39:47 -07:00
|
|
|
/* $FlowFixMe(>=0.81.0 site=react_native_ios_fb) This suppression was
|
|
|
|
* added when renaming suppression sites. */
|
2018-05-10 19:06:46 -07:00
|
|
|
return <PickerIOS {...this.props}>{this.props.children}</PickerIOS>;
|
|
|
|
} else if (Platform.OS === 'android') {
|
|
|
|
return (
|
2018-09-27 11:39:47 -07:00
|
|
|
/* $FlowFixMe(>=0.81.0 site=react_native_android_fb) This suppression
|
|
|
|
* was added when renaming suppression sites. */
|
2018-05-10 19:06:46 -07:00
|
|
|
<PickerAndroid {...this.props}>{this.props.children}</PickerAndroid>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return <UnimplementedView />;
|
|
|
|
}
|
|
|
|
}
|
2016-07-26 01:00:02 -07:00
|
|
|
}
|
2016-01-29 03:58:35 -08:00
|
|
|
|
|
|
|
module.exports = Picker;
|