Skip to content

Commit 8646658

Browse files
authored
fix (DateInput, DateTimeInput): update internal *Input date when value changed (#142)
* typo(readme): remove duplicated stirng * fix(DateInput): reflect changed input value in internal date * fix(DateTimeInput): reflect input value change in internal date * fix lint errors
1 parent df50d34 commit 8646658

File tree

5 files changed

+34
-4
lines changed

5 files changed

+34
-4
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,6 @@ You can also set locale for *Input component locally using ``localization`` prop
228228
| ``minDate`` | {string\|moment} Minimum date that can be selected |
229229
| ``inlineLabel`` | {bool} A field can have its label next to instead of above it. |
230230
| ``closeOnMouseLeave`` | {bool} Should close when cursor leaves calendar popup. Default: ``true`` |
231-
| ``timeFormat`` | {string} One of ["24", "AMPM", "ampm"]. Default: ``"24"`` |
232231
| ``preserveViewMode`` | {bool} Preserve last mode (`day`, `hour`, `minute`) each time user opens dialog. Default ``true`` |
233232
| ``mountNode`` | {any} The node where the picker should mount. |
234233
| ``onClear`` | {func} Called after clear icon has clicked. |

src/inputs/DateInput.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,20 @@ class DateInput extends BaseInput<DateInputProps, DateInputState> {
193193
};
194194
}
195195

196+
public componentDidUpdate = (prevProps: DateInputProps) => {
197+
// update internal date if ``value`` prop changed and successuffly parsed
198+
if (prevProps.value !== this.props.value) {
199+
const parsed = parseValue(this.props.value, this.props.dateFormat, this.props.localization);
200+
if (parsed) {
201+
this.setState({
202+
year: parsed.year(),
203+
month: parsed.month(),
204+
date: parsed.date(),
205+
});
206+
}
207+
}
208+
}
209+
196210
public render() {
197211
const {
198212
value,

src/inputs/DateTimeInput.tsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ class DateTimeInput extends BaseInput<DateTimeInputProps, DateTimeInputState> {
201201

202202
constructor(props: DateTimeInputProps) {
203203
super(props);
204-
const parsedValue = parseValue(props.value, props.dateFormat, props.localization);
204+
const parsedValue = parseValue(props.value, this.getDateTimeFormat(), props.localization);
205205
this.state = {
206206
mode: props.startMode,
207207
year: parsedValue ? parsedValue.year() : undefined,
@@ -213,6 +213,22 @@ class DateTimeInput extends BaseInput<DateTimeInputProps, DateTimeInputState> {
213213
};
214214
}
215215

216+
public componentDidUpdate = (prevProps: DateTimeInputProps) => {
217+
// update internal date if ``value`` prop changed and successuffly parsed
218+
if (prevProps.value !== this.props.value) {
219+
const parsed = parseValue(this.props.value, this.getDateTimeFormat(), this.props.localization);
220+
if (parsed) {
221+
this.setState({
222+
year: parsed.year(),
223+
month: parsed.month(),
224+
date: parsed.date(),
225+
hour: parsed.hour(),
226+
minute: parsed.minute(),
227+
});
228+
}
229+
}
230+
}
231+
216232
public render() {
217233
const {
218234
value,

src/inputs/DatesRangeInput.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,9 @@ class DatesRangeInput extends BaseInput<DatesRangeInputProps, BaseInputState> {
161161
const markedParsed = parseArrayOrValue(marked, dateFormat, localization);
162162
const minDateParsed = parseValue(minDate, dateFormat, localization);
163163
const maxDateParsed = parseValue(maxDate, dateFormat, localization);
164-
164+
165165
let initializeWith;
166-
166+
167167
if (!initialDate && minDateParsed || maxDateParsed) {
168168
initializeWith = minDateParsed || maxDateParsed;
169169
} else {

tslint.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"quotemark": [true, "single"],
2424
"object-literal-sort-keys": false,
2525
"max-classes-per-file": false,
26+
"no-trailing-whitespace": [ true, "ignore-blank-lines"],
2627
"trailing-comma": [
2728
true,
2829
{

0 commit comments

Comments
 (0)