|
| 1 | +/** |
| 2 | + * Copyright (c) 2015-present, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + * |
| 9 | + * @providesModule PickerAndroid |
| 10 | + * @flow |
| 11 | + */ |
| 12 | + |
| 13 | +'use strict'; |
| 14 | + |
| 15 | +var ColorPropType = require('ColorPropType'); |
| 16 | +var React = require('React'); |
| 17 | +var ReactChildren = require('ReactChildren'); |
| 18 | +var ReactPropTypes = require('ReactPropTypes'); |
| 19 | +var StyleSheetPropType = require('StyleSheetPropType'); |
| 20 | +var View = require('View'); |
| 21 | +var ViewStylePropTypes = require('ViewStylePropTypes'); |
| 22 | + |
| 23 | +var processColor = require('processColor'); |
| 24 | +var requireNativeComponent = require('requireNativeComponent'); |
| 25 | + |
| 26 | +var MODE_DIALOG = 'dialog'; |
| 27 | +var MODE_DROPDOWN = 'dropdown'; |
| 28 | +var REF_PICKER = 'picker'; |
| 29 | + |
| 30 | +var pickerStyleType = StyleSheetPropType({ |
| 31 | + ...ViewStylePropTypes, |
| 32 | + color: ColorPropType, |
| 33 | +}); |
| 34 | + |
| 35 | +type Items = { |
| 36 | + selected: number; |
| 37 | + items: any[]; |
| 38 | +}; |
| 39 | + |
| 40 | +type Event = Object; |
| 41 | + |
| 42 | +/** |
| 43 | + * Individual selectable item in a Picker. |
| 44 | + */ |
| 45 | +var Item = React.createClass({ |
| 46 | + |
| 47 | + propTypes: { |
| 48 | + /** |
| 49 | + * Color of this item's text. |
| 50 | + */ |
| 51 | + color: ColorPropType, |
| 52 | + /** |
| 53 | + * Text to display for this item. |
| 54 | + */ |
| 55 | + text: ReactPropTypes.string.isRequired, |
| 56 | + /** |
| 57 | + * The value to be passed to picker's `onSelect` callback when this item is selected. |
| 58 | + */ |
| 59 | + value: ReactPropTypes.string, |
| 60 | + /** |
| 61 | + * If `true`, this item is selected and shown in the picker. |
| 62 | + * Usually this is set based on state. |
| 63 | + */ |
| 64 | + selected: ReactPropTypes.bool, |
| 65 | + /** |
| 66 | + * Used to locate this view in end-to-end tests. |
| 67 | + */ |
| 68 | + testID: ReactPropTypes.string, |
| 69 | + }, |
| 70 | + |
| 71 | + render: function() { |
| 72 | + throw new Error('Picker items should never be rendered'); |
| 73 | + }, |
| 74 | + |
| 75 | +}); |
| 76 | + |
| 77 | +/** |
| 78 | + * <PickerAndroid> - A React component that renders the native Picker widget on Android. The items |
| 79 | + * that can be selected are specified as children views of type Item. Example usage: |
| 80 | + * |
| 81 | + * <PickerAndroid> |
| 82 | + * <PickerAndroid.Item text="Java" value="js" /> |
| 83 | + * <PickerAndroid.Item text="JavaScript" value="java" selected={true} /> |
| 84 | + * </PickerAndroid> |
| 85 | + */ |
| 86 | +var PickerAndroid = React.createClass({ |
| 87 | + |
| 88 | + propTypes: { |
| 89 | + ...View.propTypes, |
| 90 | + style: pickerStyleType, |
| 91 | + /** |
| 92 | + * If set to false, the picker will be disabled, i.e. the user will not be able to make a |
| 93 | + * selection. |
| 94 | + */ |
| 95 | + enabled: ReactPropTypes.bool, |
| 96 | + /** |
| 97 | + * Specifies how to display the selection items when the user taps on the picker: |
| 98 | + * |
| 99 | + * - dialog: Show a modal dialog |
| 100 | + * - dropdown: Shows a dropdown anchored to the picker view |
| 101 | + */ |
| 102 | + mode: ReactPropTypes.oneOf([MODE_DIALOG, MODE_DROPDOWN]), |
| 103 | + /** |
| 104 | + * Callback for when an item is selected. This is called with the following parameters: |
| 105 | + * |
| 106 | + * - `itemValue`: the `value` prop of the item that was selected |
| 107 | + * - `itemPosition`: the index of the selected item in this picker |
| 108 | + */ |
| 109 | + onSelect: ReactPropTypes.func, |
| 110 | + /** |
| 111 | + * Prompt string for this picker, currently only used in `dialog` mode as the title of the |
| 112 | + * dialog. |
| 113 | + */ |
| 114 | + prompt: ReactPropTypes.string, |
| 115 | + /** |
| 116 | + * Used to locate this view in end-to-end tests. |
| 117 | + */ |
| 118 | + testID: ReactPropTypes.string, |
| 119 | + }, |
| 120 | + |
| 121 | + statics: { |
| 122 | + Item: Item, |
| 123 | + MODE_DIALOG: MODE_DIALOG, |
| 124 | + MODE_DROPDOWN: MODE_DROPDOWN, |
| 125 | + }, |
| 126 | + |
| 127 | + getDefaultProps: function() { |
| 128 | + return { |
| 129 | + mode: MODE_DIALOG, |
| 130 | + }; |
| 131 | + }, |
| 132 | + |
| 133 | + render: function() { |
| 134 | + var Picker = this.props.mode === MODE_DROPDOWN ? DropdownPicker : DialogPicker; |
| 135 | + |
| 136 | + var { selected, items } = this._getItems(); |
| 137 | + |
| 138 | + var nativeProps = { |
| 139 | + enabled: this.props.enabled, |
| 140 | + items: items, |
| 141 | + mode: this.props.mode, |
| 142 | + onSelect: this._onSelect, |
| 143 | + prompt: this.props.prompt, |
| 144 | + selected: selected, |
| 145 | + style: this.props.style, |
| 146 | + testID: this.props.testID, |
| 147 | + }; |
| 148 | + |
| 149 | + return <Picker ref={REF_PICKER} {...nativeProps} />; |
| 150 | + }, |
| 151 | + |
| 152 | + /** |
| 153 | + * Transform this view's children into an array of items to be passed to the native component. |
| 154 | + * Since we're traversing the children, also determine the selected position. |
| 155 | + * |
| 156 | + * @returns an object with two keys: |
| 157 | + * |
| 158 | + * - `selected` (number) - the index of the selected item |
| 159 | + * - `items` (array) - the items of this picker, as an array of strings |
| 160 | + */ |
| 161 | + _getItems: function(): Items { |
| 162 | + var items = []; |
| 163 | + var selected = 0; |
| 164 | + ReactChildren.forEach(this.props.children, function(child, index) { |
| 165 | + var childProps = Object.assign({}, child.props); |
| 166 | + if (childProps.color) { |
| 167 | + childProps.color = processColor(childProps.color); |
| 168 | + } |
| 169 | + items.push(childProps); |
| 170 | + if (childProps.selected) { |
| 171 | + selected = index; |
| 172 | + } |
| 173 | + }); |
| 174 | + return { |
| 175 | + selected: selected, |
| 176 | + items: items, |
| 177 | + }; |
| 178 | + }, |
| 179 | + |
| 180 | + _onSelect: function(event: Event) { |
| 181 | + if (this.props.onSelect) { |
| 182 | + var position = event.nativeEvent.position; |
| 183 | + if (position >= 0) { |
| 184 | + var value = this.props.children[position].props.value; |
| 185 | + this.props.onSelect(value, position); |
| 186 | + } else { |
| 187 | + this.props.onSelect(null, position); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + // The native Picker has changed, but the props haven't (yet). If |
| 192 | + // the handler decides to not accept the new value or do something |
| 193 | + // else with it we might end up in a bad state, so we reset the |
| 194 | + // selection on the native component. |
| 195 | + // tl;dr: PickerAndroid is a controlled component. |
| 196 | + var { selected } = this._getItems(); |
| 197 | + if (this.refs[REF_PICKER]) { |
| 198 | + this.refs[REF_PICKER].setNativeProps({selected: selected}); |
| 199 | + } |
| 200 | + }, |
| 201 | + |
| 202 | +}); |
| 203 | + |
| 204 | +var cfg = { |
| 205 | + nativeOnly: { |
| 206 | + items: true, |
| 207 | + selected: true, |
| 208 | + } |
| 209 | +} |
| 210 | +var DropdownPicker = requireNativeComponent('AndroidDropdownPicker', PickerAndroid, cfg); |
| 211 | +var DialogPicker = requireNativeComponent('AndroidDialogPicker', PickerAndroid, cfg); |
| 212 | + |
| 213 | +module.exports = PickerAndroid; |
0 commit comments