Commit 85c1c118 authored by xwenliang's avatar xwenliang

add TimePicker example for use of Picker.select

parent 5eec07e3
...@@ -19,7 +19,6 @@ class PickerTest extends Component { ...@@ -19,7 +19,6 @@ class PickerTest extends Component {
constructor(props, context) { constructor(props, context) {
super(props, context); super(props, context);
this._showDatePicker();
} }
_createDateData() { _createDateData() {
...@@ -113,6 +112,78 @@ class PickerTest extends Component { ...@@ -113,6 +112,78 @@ class PickerTest extends Component {
Picker.show(); Picker.show();
} }
_showTimePicker() {
let years = [],
months = [],
days = [],
hours = [],
minutes = [];
for(let i=1;i<51;i++){
years.push(i+1980);
}
for(let i=1;i<13;i++){
months.push(i);
hours.push(i);
}
for(let i=1;i<32;i++){
days.push(i);
}
for(let i=1;i<61;i++){
minutes.push(i);
}
let pickerData = [years, months, days, ['am', 'pm'], hours, minutes];
let date = new Date();
let selectedValue = [
[date.getFullYear()],
[date.getMonth()+1],
[date.getDate()],
[date.getHours() > 11 ? 'pm' : 'am'],
[date.getHours() === 12 ? 12 : date.getHours()%12],
[date.getMinutes()]
];
Picker.init({
pickerData,
selectedValue,
pickerTitleText: 'Select Date and Time',
wheelFlex: [2, 1, 1, 2, 1, 1],
onPickerConfirm: pickedValue => {
console.log('area', pickedValue);
},
onPickerCancel: pickedValue => {
console.log('area', pickedValue);
},
onPickerSelect: pickedValue => {
let targetValue = [...pickedValue];
if(parseInt(targetValue[1]) === 2){
if(targetValue[0]%4 === 0 && targetValue[2] > 29){
targetValue[2] = 29;
}
else if(targetValue[0]%4 !== 0 && targetValue[2] > 28){
targetValue[2] = 28;
}
}
else if(targetValue[1] in {4:1, 6:1, 9:1, 11:1} && targetValue[2] > 30){
targetValue[2] = 30;
}
// forbidden some value such as some 2.29, 4.31, 6.31...
if(JSON.stringify(targetValue) !== JSON.stringify(pickedValue)){
// android will return String all the time,but we put Number into picker at first
// so we need to convert them to Number again
targetValue.map((v, k) => {
if(k !== 3){
targetValue[k] = parseInt(v);
}
});
Picker.select(targetValue);
pickedValue = targetValue;
}
}
});
Picker.show();
}
_toggle() { _toggle() {
Picker.toggle(); Picker.toggle();
} }
...@@ -129,6 +200,9 @@ class PickerTest extends Component { ...@@ -129,6 +200,9 @@ class PickerTest extends Component {
<TouchableOpacity style={{marginTop: 40, marginLeft: 20}} onPress={this._showDatePicker.bind(this)}> <TouchableOpacity style={{marginTop: 40, marginLeft: 20}} onPress={this._showDatePicker.bind(this)}>
<Text>DatePicker</Text> <Text>DatePicker</Text>
</TouchableOpacity> </TouchableOpacity>
<TouchableOpacity style={{marginTop: 10, marginLeft: 20}} onPress={this._showTimePicker.bind(this)}>
<Text>TimePicker</Text>
</TouchableOpacity>
<TouchableOpacity style={{marginTop: 10, marginLeft: 20}} onPress={this._showAreaPicker.bind(this)}> <TouchableOpacity style={{marginTop: 10, marginLeft: 20}} onPress={this._showAreaPicker.bind(this)}>
<Text>AreaPicker</Text> <Text>AreaPicker</Text>
</TouchableOpacity> </TouchableOpacity>
......
...@@ -56,9 +56,14 @@ export default { ...@@ -56,9 +56,14 @@ export default {
}, },
select(arr, fn) { select(arr, fn) {
if(ios){
Picker.select(arr);
}
else if(android){
Picker.select(arr, err => { Picker.select(arr, err => {
typeof fn === 'function' && fn(err); typeof fn === 'function' && fn(err);
}); });
}
}, },
toggle(){ toggle(){
......
{ {
"name": "react-native-picker", "name": "react-native-picker",
"version": "4.0.17", "version": "4.0.18",
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment