Skip to content

Commit 5cbf429

Browse files
committed
fix(package): Fix close on picker value change and let use picker with async values
1 parent c2a61db commit 5cbf429

2 files changed

Lines changed: 32 additions & 10 deletions

File tree

src/withPickerValues/PickerModal.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@ class PickerModal extends PureComponent<PropsType> {
1818

1919
onValueChange = (value: any) => {
2020
if (this.props.onChangeText) this.props.onChangeText(value);
21+
if (this.props.onSubmitEditing) this.props.onSubmitEditing();
22+
if (this.pickerModal) this.pickerModal.close();
2123
};
2224

2325
renderPicker = () => {
24-
const { values, placeholder, value } = this.props;
25-
if (values && Platform.OS === 'ios') {
26+
const { placeholder, value } = this.props;
27+
if (!this.props.values || !this.props.values.length) return null;
28+
const values = [...this.props.values];
29+
if (Platform.OS === 'ios') {
2630
values.unshift({ value: '', label: placeholder });
2731
}
2832
const picker = (
@@ -40,7 +44,16 @@ class PickerModal extends PureComponent<PropsType> {
4044
{picker}
4145
</KeyboardModal>
4246
) : (
43-
<View style={{ opacity: 0, position: 'absolute', top: 0, bottom: 0, right: 0, left: 0 }}>
47+
<View
48+
style={{
49+
opacity: 0,
50+
position: 'absolute',
51+
top: 0,
52+
bottom: 0,
53+
right: 0,
54+
left: 0,
55+
}}
56+
>
4457
{picker}
4558
</View>
4659
);

src/withPickerValues/withPickerValues.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,22 @@
33
import React from 'react';
44
import PickerModal from './PickerModal';
55

6-
const withPickerModal = Component => props => {
7-
const selectedItem = props.values.find(item => item.value === props.value);
8-
return (
9-
<PickerModal {...props}>
10-
<Component {...props} value={selectedItem ? selectedItem.label : ''} />
11-
</PickerModal>
12-
);
6+
const withPickerModal = Component => {
7+
class WithPickerModal extends React.Component<$FlowFixMeProps, $FlowFixMeState> {
8+
render() {
9+
const selectedItem =
10+
this.props.values && this.props.values.length > 0
11+
? this.props.values.find(item => item && item.value === this.props.value)
12+
: undefined;
13+
return (
14+
<PickerModal {...this.props}>
15+
<Component {...this.props} value={selectedItem ? selectedItem.label : ''} />
16+
</PickerModal>
17+
);
18+
}
19+
}
20+
21+
return WithPickerModal;
1322
};
1423

1524
export default withPickerModal;

0 commit comments

Comments
 (0)