2016-01-15 14:23:15 +00:00
|
|
|
/**
|
2017-05-06 03:50:47 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
2016-07-12 12:51:57 +00:00
|
|
|
*
|
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-07-12 12:51:57 +00:00
|
|
|
*
|
2016-01-15 14:23:15 +00:00
|
|
|
* @flow
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2016-04-09 03:36:40 +00:00
|
|
|
const React = require('react');
|
|
|
|
const ReactNative = require('react-native');
|
2016-01-29 11:58:35 +00:00
|
|
|
const StyleSheet = require('StyleSheet');
|
2017-05-06 03:50:47 +00:00
|
|
|
const RNTesterBlock = require('RNTesterBlock');
|
|
|
|
const RNTesterPage = require('RNTesterPage');
|
2016-01-15 14:23:15 +00:00
|
|
|
|
|
|
|
const {
|
2016-01-29 11:58:35 +00:00
|
|
|
Picker,
|
2016-01-15 14:23:15 +00:00
|
|
|
Text,
|
2016-04-09 03:36:40 +00:00
|
|
|
} = ReactNative;
|
2016-09-14 09:55:24 +00:00
|
|
|
|
2016-01-29 11:58:35 +00:00
|
|
|
const Item = Picker.Item;
|
2016-01-15 14:23:15 +00:00
|
|
|
|
2017-08-18 01:36:54 +00:00
|
|
|
class PickerExample extends React.Component<{}, $FlowFixMeState> {
|
2016-07-26 08:00:02 +00:00
|
|
|
static title = '<Picker>';
|
|
|
|
static description = 'Provides multiple options to choose from, using either a dropdown menu or a dialog.';
|
2016-01-26 18:29:47 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
state = {
|
|
|
|
selected1: 'key1',
|
|
|
|
selected2: 'key1',
|
|
|
|
selected3: 'key1',
|
|
|
|
color: 'red',
|
|
|
|
mode: Picker.MODE_DIALOG,
|
|
|
|
};
|
2016-01-15 14:23:15 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
render() {
|
2016-01-15 14:23:15 +00:00
|
|
|
return (
|
2017-05-06 03:50:47 +00:00
|
|
|
<RNTesterPage title="<Picker>">
|
|
|
|
<RNTesterBlock title="Basic Picker">
|
2016-01-29 11:58:35 +00:00
|
|
|
<Picker
|
|
|
|
style={styles.picker}
|
|
|
|
selectedValue={this.state.selected1}
|
|
|
|
onValueChange={this.onValueChange.bind(this, 'selected1')}>
|
|
|
|
<Item label="hello" value="key0" />
|
|
|
|
<Item label="world" value="key1" />
|
|
|
|
</Picker>
|
2017-05-06 03:50:47 +00:00
|
|
|
</RNTesterBlock>
|
|
|
|
<RNTesterBlock title="Disabled picker">
|
2016-01-29 11:58:35 +00:00
|
|
|
<Picker style={styles.picker} enabled={false} selectedValue={this.state.selected1}>
|
|
|
|
<Item label="hello" value="key0" />
|
|
|
|
<Item label="world" value="key1" />
|
|
|
|
</Picker>
|
2017-05-06 03:50:47 +00:00
|
|
|
</RNTesterBlock>
|
|
|
|
<RNTesterBlock title="Dropdown Picker">
|
2016-01-29 11:58:35 +00:00
|
|
|
<Picker
|
|
|
|
style={styles.picker}
|
|
|
|
selectedValue={this.state.selected2}
|
|
|
|
onValueChange={this.onValueChange.bind(this, 'selected2')}
|
2016-01-15 14:23:15 +00:00
|
|
|
mode="dropdown">
|
2016-01-29 11:58:35 +00:00
|
|
|
<Item label="hello" value="key0" />
|
|
|
|
<Item label="world" value="key1" />
|
|
|
|
</Picker>
|
2017-05-06 03:50:47 +00:00
|
|
|
</RNTesterBlock>
|
|
|
|
<RNTesterBlock title="Picker with prompt message">
|
2016-01-29 11:58:35 +00:00
|
|
|
<Picker
|
|
|
|
style={styles.picker}
|
|
|
|
selectedValue={this.state.selected3}
|
|
|
|
onValueChange={this.onValueChange.bind(this, 'selected3')}
|
2016-01-15 14:23:15 +00:00
|
|
|
prompt="Pick one, just one">
|
2016-01-29 11:58:35 +00:00
|
|
|
<Item label="hello" value="key0" />
|
|
|
|
<Item label="world" value="key1" />
|
|
|
|
</Picker>
|
2017-05-06 03:50:47 +00:00
|
|
|
</RNTesterBlock>
|
|
|
|
<RNTesterBlock title="Picker with no listener">
|
2016-01-29 11:58:35 +00:00
|
|
|
<Picker style={styles.picker}>
|
|
|
|
<Item label="hello" value="key0" />
|
|
|
|
<Item label="world" value="key1" />
|
|
|
|
</Picker>
|
2016-01-15 14:23:15 +00:00
|
|
|
<Text>
|
2016-01-29 11:58:35 +00:00
|
|
|
Cannot change the value of this picker because it doesn't update selectedValue.
|
2016-01-15 14:23:15 +00:00
|
|
|
</Text>
|
2017-05-06 03:50:47 +00:00
|
|
|
</RNTesterBlock>
|
|
|
|
<RNTesterBlock title="Colorful pickers">
|
2016-01-29 11:58:35 +00:00
|
|
|
<Picker
|
|
|
|
style={[styles.picker, {color: 'white', backgroundColor: '#333'}]}
|
|
|
|
selectedValue={this.state.color}
|
|
|
|
onValueChange={this.onValueChange.bind(this, 'color')}
|
2016-01-15 14:23:15 +00:00
|
|
|
mode="dropdown">
|
2016-01-29 11:58:35 +00:00
|
|
|
<Item label="red" color="red" value="red" />
|
|
|
|
<Item label="green" color="green" value="green" />
|
|
|
|
<Item label="blue" color="blue" value="blue" />
|
|
|
|
</Picker>
|
|
|
|
<Picker
|
|
|
|
style={styles.picker}
|
|
|
|
selectedValue={this.state.color}
|
|
|
|
onValueChange={this.onValueChange.bind(this, 'color')}
|
2016-01-15 14:23:15 +00:00
|
|
|
mode="dialog">
|
2016-01-29 11:58:35 +00:00
|
|
|
<Item label="red" color="red" value="red" />
|
|
|
|
<Item label="green" color="green" value="green" />
|
|
|
|
<Item label="blue" color="blue" value="blue" />
|
|
|
|
</Picker>
|
2017-05-06 03:50:47 +00:00
|
|
|
</RNTesterBlock>
|
|
|
|
</RNTesterPage>
|
2016-01-15 14:23:15 +00:00
|
|
|
);
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2016-01-15 14:23:15 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
changeMode = () => {
|
2016-01-29 11:58:35 +00:00
|
|
|
const newMode = this.state.mode === Picker.MODE_DIALOG
|
|
|
|
? Picker.MODE_DROPDOWN
|
|
|
|
: Picker.MODE_DIALOG;
|
2016-01-15 14:23:15 +00:00
|
|
|
this.setState({mode: newMode});
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
2016-01-15 14:23:15 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
onValueChange = (key: string, value: string) => {
|
2016-01-15 14:23:15 +00:00
|
|
|
const newState = {};
|
|
|
|
newState[key] = value;
|
|
|
|
this.setState(newState);
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
|
|
|
}
|
2016-01-15 14:23:15 +00:00
|
|
|
|
2016-01-29 11:58:35 +00:00
|
|
|
var styles = StyleSheet.create({
|
|
|
|
picker: {
|
|
|
|
width: 100,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = PickerExample;
|