Skip to content

Commit e2e2dbe

Browse files
committed
clearer back to default action and feedback onChange
1 parent 0a8a078 commit e2e2dbe

1 file changed

Lines changed: 43 additions & 50 deletions

File tree

src/components/widgets/DateTimePicker.js

Lines changed: 43 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ export default class DateTimePicker extends Component {
1616
constructor(props) {
1717
super(props);
1818
const {time, date} = this.parseDateTime(props.value);
19-
const isValidTime = isDateTime(testDate + ' ' + time) || time === timePlaceholder;
20-
const isValidDate = isDateTime(date + ' ' + testTime) || date === datePlaceholder;
19+
const isValidTime = isDateTime(testDate + ' ' + time) || ['', timePlaceholder].includes(time);
20+
const isValidDate = isDateTime(date + ' ' + testTime) || ['', datePlaceholder].includes(date);
2121

2222
this.state = {
2323
calendarOpen: false,
@@ -34,8 +34,10 @@ export default class DateTimePicker extends Component {
3434
this.toPlotlyJSDate = this.toPlotlyJSDate.bind(this);
3535
this.onMonthChange = this.onMonthChange.bind(this);
3636
this.onYearChange = this.onYearChange.bind(this);
37-
this.updateTime = this.updateTime.bind(this);
38-
this.updateDate = this.updateDate.bind(this);
37+
this.onTimeChange = this.onTimeChange.bind(this);
38+
this.onDateChange = this.onDateChange.bind(this);
39+
this.onTimeUpdate = this.onTimeUpdate.bind(this);
40+
this.onDateUpdate = this.onDateUpdate.bind(this);
3941
}
4042

4143
toPlotlyJSDate(value) {
@@ -116,72 +118,61 @@ export default class DateTimePicker extends Component {
116118
return {date: parsed[0], time: parsed[1] ? parsed[1] : ''};
117119
}
118120

119-
updateTime(value) {
120-
const {date: currentDate} = this.parseDateTime(this.props.value);
121-
const update = currentDate + ' ' + value;
121+
onTimeChange(value) {
122122
const isValidTime = isDateTime(testDate + ' ' + value);
123+
this.setState({
124+
timeInputClassName:
125+
isValidTime || value === ''
126+
? 'datetimepicker-container-time-input'
127+
: 'datetimepicker-container-time-input +error',
128+
timeValue: value,
129+
});
130+
}
123131

124-
if (value === '') {
125-
this.setState({
126-
timeInputClassName: 'datetimepicker-container-time-input',
127-
timeValue: timePlaceholder,
128-
});
129-
return;
130-
}
132+
onDateChange(value) {
133+
const isValidDate = isDateTime(value + ' ' + testTime);
134+
this.setState({
135+
dateInputClassName:
136+
isValidDate || value === ''
137+
? 'datetimepicker-container-date-input'
138+
: 'datetimepicker-container-date-input +error',
139+
dateValue: value,
140+
});
141+
}
131142

132-
if (isValidTime) {
133-
this.props.onChange(update);
143+
onTimeUpdate() {
144+
const {time: currentTime, date: currentDate} = this.parseDateTime(this.props.value);
145+
const isValidTime = isDateTime(testDate + ' ' + this.state.timeValue);
146+
147+
if (this.state.timeValue === '') {
134148
this.setState({
135-
timeValue: value,
136149
timeInputClassName: 'datetimepicker-container-time-input',
150+
timeValue: currentTime,
137151
});
138152
return;
139153
}
140154

141-
if (value === timePlaceholder) {
142-
return;
143-
}
144-
145-
if (!isValidTime) {
146-
this.setState({
147-
timeInputClassName: 'datetimepicker-container-time-input +error',
148-
timeValue: value,
149-
});
155+
if (isValidTime) {
156+
this.props.onChange(currentDate + ' ' + this.state.timeValue);
150157
}
151158
}
152159

153-
updateDate(value) {
154-
const {time: currentTime} = this.parseDateTime(this.props.value);
155-
const update = value + ' ' + currentTime;
156-
const isValidDate = isDateTime(value + ' ' + testTime);
160+
onDateUpdate() {
161+
const {date: currentDate, time: currentTime} = this.parseDateTime(this.props.value);
162+
const isValidDate = isDateTime(this.state.dateValue + ' ' + testTime);
157163

158164
if (isValidDate) {
159-
this.props.onChange(update);
160-
this.setState({
161-
dateValue: value,
162-
dateInputClassName: 'datetimepicker-container-date-input',
163-
});
165+
this.props.onChange(this.state.dateValue + ' ' + currentTime);
164166
return;
165167
}
166168

167-
if (value === '') {
169+
if (this.state.dateValue === '') {
168170
this.setState({
169-
dateValue: datePlaceholder,
171+
dateValue: currentDate,
170172
dateInputClassName: 'datetimepicker-container-date-input',
171173
});
172174
return;
173175
}
174-
175-
if (value === datePlaceholder) {
176-
return;
177-
}
178-
179-
if (!isValidDate) {
180-
this.setState({
181-
dateValue: value,
182-
dateInputClassName: 'datetimepicker-container-date-input +error',
183-
});
184-
}
185176
}
186177

187178
render() {
@@ -193,7 +184,8 @@ export default class DateTimePicker extends Component {
193184
<TextInput
194185
value={this.state.dateValue}
195186
editableClassName={this.state.dateInputClassName}
196-
onUpdate={this.updateDate}
187+
onChange={this.onDateChange}
188+
onUpdate={this.onDateUpdate}
197189
placeHolder={datePlaceholder}
198190
/>
199191
<div className="datetimepicker-container-icons">
@@ -242,7 +234,8 @@ export default class DateTimePicker extends Component {
242234
<div className="datetimepicker-container-time">
243235
<TextInput
244236
value={this.state.timeValue}
245-
onUpdate={this.updateTime}
237+
onChange={this.onTimeChange}
238+
onUpdate={this.onTimeUpdate}
246239
placeHolder={timePlaceholder}
247240
editableClassName={this.state.timeInputClassName}
248241
/>

0 commit comments

Comments
 (0)