Skip to content

Commit 2d78304

Browse files
authored
Merge pull request #432 from mashmatrix/accept-parsing-formats
Accept parsing formats to allow customizing date input text handling
2 parents e6010c1 + 3ed5e41 commit 2d78304

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/scripts/DateInput.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ export type DateInputProps = {
118118
opened?: boolean;
119119
defaultOpened?: boolean;
120120
dateFormat?: string;
121+
parsingFormats?: string[];
121122
includeTime?: boolean;
122123
minDate?: string;
123124
maxDate?: string;
@@ -142,6 +143,7 @@ export const DateInput = createFC<DateInputProps, { isFormElement: boolean }>(
142143
value: value_,
143144
defaultValue,
144145
dateFormat,
146+
parsingFormats: parsingFormats_,
145147
includeTime,
146148
className,
147149
cols,
@@ -171,6 +173,7 @@ export const DateInput = createFC<DateInputProps, { isFormElement: boolean }>(
171173
const [value, setValue] = useControlledValue(value_, defaultValue ?? null);
172174
const valueFormat = includeTime ? 'YYYY-MM-DDTHH:mm:ss.SSSZ' : 'YYYY-MM-DD';
173175
const inputValueFormat = dateFormat || (includeTime ? 'L HH:mm' : 'L');
176+
const parsingFormats = parsingFormats_ ?? [inputValueFormat];
174177
const dvalue = dayjs(value ?? undefined, valueFormat);
175178
const [inputValue_, setInputValue] = useState<string | null>(null);
176179
const inputValue =
@@ -201,7 +204,7 @@ export const DateInput = createFC<DateInputProps, { isFormElement: boolean }>(
201204
if (!inputValue) {
202205
newValue = '';
203206
} else {
204-
const dvalue = dayjs(inputValue, inputValueFormat);
207+
const dvalue = dayjs(inputValue, parsingFormats);
205208
if (dvalue.isValid()) {
206209
newValue = dvalue.format(valueFormat);
207210
} else {
@@ -223,7 +226,7 @@ export const DateInput = createFC<DateInputProps, { isFormElement: boolean }>(
223226
const showDatepicker = useEventCallback(() => {
224227
let newValue = value;
225228
if (inputValue != null) {
226-
const dvalue = dayjs(inputValue, inputValueFormat);
229+
const dvalue = dayjs(inputValue, parsingFormats);
227230
if (dvalue.isValid()) {
228231
newValue = dvalue.format(valueFormat);
229232
}

0 commit comments

Comments
 (0)