Skip to content

Commit f814950

Browse files
authored
Fix DateTimePicker placeholder, allow undefined as initial value (#639)
* fix datetimepicker placeholder, allow undefined initial value * rename getValue to getStringValue
1 parent 461ec2e commit f814950

File tree

1 file changed

+30
-37
lines changed

1 file changed

+30
-37
lines changed

src/components/dateTimePicker/index.js

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,12 @@ class DateTimePicker extends BaseComponent {
9191
constructor(props) {
9292
super(props);
9393

94-
const initialValue = props.value || new Date();
94+
const initialValue = props.value;
9595
this.chosenDate = initialValue;
9696

9797
this.state = {
9898
showExpandableOverlay: false,
99-
chosenDate: initialValue
99+
value: initialValue
100100
};
101101
}
102102

@@ -115,20 +115,35 @@ class DateTimePicker extends BaseComponent {
115115
};
116116

117117
toggleExpandableOverlay = callback => {
118-
this.setState({showExpandableOverlay: !this.state.showExpandableOverlay},
119-
() => {
120-
if (_.isFunction(callback)) {
121-
callback();
122-
}
123-
});
118+
this.setState({showExpandableOverlay: !this.state.showExpandableOverlay}, () => {
119+
if (_.isFunction(callback)) {
120+
callback();
121+
}
122+
});
124123
};
125124

126125
onDonePressed = () =>
127126
this.toggleExpandableOverlay(() => {
128127
_.invoke(this.props, 'onChange', this.chosenDate);
129-
this.setState({chosenDate: this.chosenDate});
128+
this.setState({value: this.chosenDate});
130129
});
131130

131+
getStringValue = () => {
132+
const {value} = this.state;
133+
const {mode, dateFormat, timeFormat} = this.getThemeProps();
134+
if (value) {
135+
const dateString =
136+
mode === MODES.DATE
137+
? dateFormat
138+
? moment(value).format(dateFormat)
139+
: value.toLocaleDateString()
140+
: timeFormat
141+
? moment(value).format(timeFormat)
142+
: value.toLocaleTimeString();
143+
return dateString;
144+
}
145+
};
146+
132147
renderExpandableOverlay = () => {
133148
const {testID, dialogProps} = this.getThemeProps();
134149
const {showExpandableOverlay} = this.state;
@@ -144,12 +159,7 @@ class DateTimePicker extends BaseComponent {
144159
onDismiss={this.toggleExpandableOverlay}
145160
containerStyle={this.styles.dialog}
146161
testID={`${testID}.dialog`}
147-
supportedOrientations={[
148-
'portrait',
149-
'landscape',
150-
'landscape-left',
151-
'landscape-right'
152-
]} // iOS only
162+
supportedOrientations={['portrait', 'landscape', 'landscape-left', 'landscape-right']} // iOS only
153163
{...dialogProps}
154164
>
155165
<View useSafeArea>
@@ -171,25 +181,20 @@ class DateTimePicker extends BaseComponent {
171181
iconStyle={{tintColor: Colors.dark10}}
172182
onPress={this.toggleExpandableOverlay}
173183
/>
174-
<Button
175-
useCustomTheme={useCustomTheme}
176-
link
177-
iconSource={Assets.icons.check}
178-
onPress={this.onDonePressed}
179-
/>
184+
<Button useCustomTheme={useCustomTheme} link iconSource={Assets.icons.check} onPress={this.onDonePressed}/>
180185
</View>
181186
);
182187
}
183188

184189
renderDateTimePicker() {
185-
const {chosenDate, showExpandableOverlay} = this.state;
190+
const {value, showExpandableOverlay} = this.state;
186191
const {mode, minimumDate, maximumDate, locale, is24Hour, minuteInterval, timeZoneOffsetInMinutes} = this.props;
187192

188193
if (showExpandableOverlay) {
189194
return (
190195
<RNDateTimePicker
191196
mode={mode}
192-
value={chosenDate}
197+
value={value || new Date()}
193198
onChange={this.handleChange}
194199
minimumDate={minimumDate}
195200
maximumDate={maximumDate}
@@ -203,28 +208,16 @@ class DateTimePicker extends BaseComponent {
203208
}
204209

205210
renderExpandable = () => {
206-
return Constants.isAndroid
207-
? this.renderDateTimePicker()
208-
: this.renderExpandableOverlay();
211+
return Constants.isAndroid ? this.renderDateTimePicker() : this.renderExpandableOverlay();
209212
};
210213

211214
render() {
212-
const {chosenDate} = this.state;
213-
const {mode, dateFormat, timeFormat} = this.getThemeProps();
214215
const textInputProps = TextField.extractOwnProps(this.getThemeProps());
215-
const dateString =
216-
mode === MODES.DATE
217-
? dateFormat
218-
? moment(chosenDate).format(dateFormat)
219-
: chosenDate.toLocaleDateString()
220-
: timeFormat
221-
? moment(chosenDate).format(timeFormat)
222-
: chosenDate.toLocaleTimeString();
223216

224217
return (
225218
<TextField
226219
{...textInputProps}
227-
value={dateString}
220+
value={this.getStringValue()}
228221
expandable
229222
renderExpandable={this.renderExpandable}
230223
onToggleExpandableModal={this.toggleExpandableOverlay}

0 commit comments

Comments
 (0)