Skip to content

Commit c7e4e6c

Browse files
committed
fix: improve date input
1 parent 071f056 commit c7e4e6c

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

app/components/ui/date-input.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export const useDatePickerInputManagement = (
8585
export const DateInput = ({
8686
onChange,
8787
onBlur,
88+
onKeyDown,
8889
value,
8990
format = 'DD/MM/YYYY',
9091
...props
@@ -105,6 +106,12 @@ export const DateInput = ({
105106
datePickerInputManagement.handleInputBlur(e.target.value);
106107
onBlur?.(e);
107108
}}
109+
onKeyDown={(e) => {
110+
if (e.key === 'Enter') {
111+
datePickerInputManagement.handleInputBlur(e.currentTarget.value);
112+
}
113+
onKeyDown?.(e);
114+
}}
108115
onChange={datePickerInputManagement.handleInputChange}
109116
value={
110117
datePickerInputManagement.inputValue ?? dayjs(value).format(format)

app/lib/dayjs/parse-string-to-date.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
import dayjs from 'dayjs';
22

3+
const spacers = [' ', '.', '/', '-', '_'];
4+
const DD_MM = (year?: 'YY' | 'YYYY') => {
5+
return spacers.flatMap((spacer) => {
6+
const yearFormat = year ? `${spacer}${year}` : '';
7+
return [
8+
`D${spacer}M${yearFormat}`,
9+
`DD${spacer}M${yearFormat}`,
10+
`D${spacer}MM${yearFormat}`,
11+
`DD${spacer}MM${yearFormat}`,
12+
];
13+
});
14+
};
15+
316
export const parseStringToDate = (
417
input: string,
518
extraFormats: Array<string> = []
@@ -8,22 +21,14 @@ export const parseStringToDate = (
821
input,
922
[
1023
...extraFormats,
24+
'D',
1125
'DD',
1226
'DDMM',
13-
'DD/MM',
14-
'DD-MM',
15-
'DD.MM',
16-
'DD MM',
1727
'DDMMYY',
18-
'DD/MM/YY',
19-
'DD-MM-YY',
20-
'DD.MM.YY',
21-
'DD MM YY',
2228
'DDMMYYYY',
23-
'DD/MM/YYYY',
24-
'DD-MM-YYYY',
25-
'DD.MM.YYYY',
26-
'DD MM YYYY',
29+
...DD_MM(),
30+
...DD_MM('YY'),
31+
...DD_MM('YYYY'),
2732
],
2833
true
2934
).toDate();

0 commit comments

Comments
 (0)