Skip to content

Commit 29e5ef5

Browse files
committed
Don't reset internal state on value change
1 parent 7ed1c0b commit 29e5ef5

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

packages/circuit-ui/components/DateInput/DateInput.stories.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const baseArgs = {
4949
};
5050

5151
export const Base = (args: DateInputProps) => {
52-
const [value, setValue] = useState(args.value || '');
52+
const [value, setValue] = useState(args.defaultValue || args.value || '');
5353
return <DateInput {...args} value={value} onChange={setValue} />;
5454
};
5555

@@ -108,14 +108,14 @@ export const Readonly = (args: DateInputProps) => <DateInput {...args} />;
108108
Readonly.args = {
109109
...baseArgs,
110110
label: 'Appointment date',
111-
value: '2017-08-28',
111+
defaultValue: '2017-08-28',
112112
readOnly: true,
113113
};
114114

115115
export const Disabled = (args: DateInputProps) => <DateInput {...args} />;
116116

117117
Disabled.args = {
118118
...baseArgs,
119-
value: '2017-08-28',
119+
defaultValue: '2017-08-28',
120120
disabled: true,
121121
};

packages/circuit-ui/components/DateInput/DateInput.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ export const DateInput = forwardRef<HTMLDivElement, DateInputProps>(
253253
};
254254

255255
const openCalendar = () => {
256-
setSelection(toPlainDate(value) || undefined);
256+
setSelection(state.plainDate || undefined);
257257
setOpen(true);
258258
};
259259

@@ -344,11 +344,11 @@ export const DateInput = forwardRef<HTMLDivElement, DateInputProps>(
344344
}
345345
}
346346

347-
const plainDate = toPlainDate(value);
348-
const calendarButtonLabel = plainDate
349-
? [openCalendarButtonLabel, formatDate(plainDate, locale, 'long')].join(
350-
', ',
351-
)
347+
const calendarButtonLabel = state.plainDate
348+
? [
349+
openCalendarButtonLabel,
350+
formatDate(state.plainDate, locale, 'long'),
351+
].join(', ')
352352
: openCalendarButtonLabel;
353353

354354
const segments = getDateSegments(locale);

packages/circuit-ui/components/DateInput/hooks.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import {
3333
MIN_YEAR,
3434
toPlainDate,
3535
} from '../../util/date.js';
36-
import { isNumber, isString } from '../../util/type-check.js';
36+
import { isNumber } from '../../util/type-check.js';
3737
import { isBackspace, isDelete } from '../../util/key-codes.js';
3838
import { clamp } from '../../util/helpers.js';
3939

@@ -52,6 +52,7 @@ type PartialPlainDate = {
5252

5353
type PlainDateState = {
5454
date: PartialPlainDate;
55+
plainDate: Temporal.PlainDate | undefined;
5556
update: (date: PartialPlainDate) => void;
5657
};
5758

@@ -74,7 +75,7 @@ export function usePlainDateState(
7475
);
7576

7677
useEffect(() => {
77-
if (isString(value)) {
78+
if (value) {
7879
setDate(parseValue(value));
7980
}
8081
}, [value]);
@@ -120,7 +121,12 @@ export function usePlainDateState(
120121
[onChange],
121122
);
122123

123-
return { date, update };
124+
const { year, month, day } = date;
125+
126+
const plainDate =
127+
year && month && day ? new Temporal.PlainDate(year, month, day) : undefined;
128+
129+
return { date, plainDate, update };
124130
}
125131

126132
export function useSegment(

0 commit comments

Comments
 (0)