Skip to content

Commit be42c44

Browse files
committed
HF for DateTimePicker not passing the user the chosen date
1 parent 0c2d7d7 commit be42c44

File tree

1 file changed

+41
-29
lines changed

1 file changed

+41
-29
lines changed

src/components/dateTimePicker/index.js

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import Dialog from '../dialog';
1313
import View from '../view';
1414
import Button from '../button';
1515

16-
1716
const MODES = {
1817
DATE: 'date',
1918
TIME: 'time'
@@ -66,7 +65,7 @@ class DateTimePicker extends BaseComponent {
6665
* Props to pass the Dialog component
6766
*/
6867
dialogProps: PropTypes.object
69-
}
68+
};
7069

7170
static defaultProps = {
7271
...TextField.defaultProps,
@@ -89,29 +88,30 @@ class DateTimePicker extends BaseComponent {
8988
this.styles = createStyles(this.props);
9089
}
9190

92-
setDate = (event, date) => {
93-
if (date !== undefined) {
91+
handleChange = (event = {}, date) => {
92+
if (event.type !== 'dismissed' && date !== undefined) {
9493
this.chosenDate = date;
9594

9695
if (Constants.isAndroid) {
97-
this.setState({chosenDate: this.chosenDate, showExpandableOverlay: false});
96+
this.onDonePressed();
9897
}
9998
}
99+
};
100100

101-
_.invoke(this.props, 'onChange');
102-
}
103-
104-
toggleExpandableOverlay = (callback) => {
105-
this.setState({showExpandableOverlay: !this.state.showExpandableOverlay}, () => {
106-
if (_.isFunction(callback)) {
107-
callback();
108-
}
109-
});
101+
toggleExpandableOverlay = callback => {
102+
this.setState({showExpandableOverlay: !this.state.showExpandableOverlay},
103+
() => {
104+
if (_.isFunction(callback)) {
105+
callback();
106+
}
107+
});
110108
};
111109

112-
onDonePressed = () => {
113-
this.toggleExpandableOverlay(() => this.setState({chosenDate: this.chosenDate}));
114-
}
110+
onDonePressed = () =>
111+
this.toggleExpandableOverlay(() => {
112+
_.invoke(this.props, 'onChange', this.chosenDate);
113+
this.setState({chosenDate: this.chosenDate});
114+
});
115115

116116
renderExpandableOverlay = () => {
117117
const {testID, dialogProps} = this.getThemeProps();
@@ -128,7 +128,12 @@ class DateTimePicker extends BaseComponent {
128128
onDismiss={this.toggleExpandableOverlay}
129129
containerStyle={this.styles.dialog}
130130
testID={`${testID}.dialog`}
131-
supportedOrientations={['portrait', 'landscape', 'landscape-left', 'landscape-right']} // iOS only
131+
supportedOrientations={[
132+
'portrait',
133+
'landscape',
134+
'landscape-left',
135+
'landscape-right'
136+
]} // iOS only
132137
{...dialogProps}
133138
>
134139
<View useSafeArea>
@@ -166,32 +171,39 @@ class DateTimePicker extends BaseComponent {
166171

167172
if (showExpandableOverlay) {
168173
return (
169-
<RNDateTimePicker
174+
<RNDateTimePicker
170175
mode={mode}
171176
value={chosenDate}
172-
onChange={this.setDate}
177+
onChange={this.handleChange}
173178
minimumDate={minimumDate}
174179
maximumDate={maximumDate}
175180
/>
176181
);
177182
}
178183
}
179184

180-
renderExpandable = () => {
181-
return Constants.isAndroid ? this.renderDateTimePicker() : this.renderExpandableOverlay();
182-
}
185+
renderExpandable = () => {
186+
return Constants.isAndroid
187+
? this.renderDateTimePicker()
188+
: this.renderExpandableOverlay();
189+
};
183190

184191
render() {
185192
const {chosenDate} = this.state;
186193
const {mode, dateFormat, timeFormat} = this.getThemeProps();
187194
const textInputProps = TextField.extractOwnProps(this.getThemeProps());
188-
const dateString = mode === MODES.DATE ?
189-
(dateFormat ? moment(chosenDate).format(dateFormat) : chosenDate.toLocaleDateString()) :
190-
(timeFormat ? moment(chosenDate).format(timeFormat) : chosenDate.toLocaleTimeString());
191-
195+
const dateString =
196+
mode === MODES.DATE
197+
? dateFormat
198+
? moment(chosenDate).format(dateFormat)
199+
: chosenDate.toLocaleDateString()
200+
: timeFormat
201+
? moment(chosenDate).format(timeFormat)
202+
: chosenDate.toLocaleTimeString();
203+
192204
return (
193-
<TextField
194-
{...textInputProps}
205+
<TextField
206+
{...textInputProps}
195207
value={dateString}
196208
expandable
197209
renderExpandable={this.renderExpandable}

0 commit comments

Comments
 (0)