Skip to content

Commit d8ab668

Browse files
committed
Handle validation for YYYY-MM-DD format
1 parent 9c2af69 commit d8ab668

File tree

3 files changed

+59
-9
lines changed

3 files changed

+59
-9
lines changed

src/components/forms/DatePicker/DatePicker.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,12 @@ export const DatePicker = ({
9797
const parsedRangeDate = rangeDate ? parseDateString(rangeDate) : undefined
9898

9999
const validateInput = (): void => {
100-
const isInvalid = isDateInvalid(externalValue, parsedMinDate, parsedMaxDate)
100+
const isInvalid = isDateInvalid(
101+
externalValue,
102+
dateFormat,
103+
parsedMinDate,
104+
parsedMaxDate
105+
)
101106

102107
if (isInvalid && !externalInputEl?.current?.validationMessage) {
103108
externalInputEl?.current?.setCustomValidity(VALIDATION_MESSAGE)
@@ -134,7 +139,10 @@ export const DatePicker = ({
134139

135140
const inputDate = parseDateString(value, dateFormat, true)
136141
let newValue = ''
137-
if (inputDate && !isDateInvalid(value, parsedMinDate, parsedMaxDate)) {
142+
if (
143+
inputDate &&
144+
!isDateInvalid(value, dateFormat, parsedMinDate, parsedMaxDate)
145+
) {
138146
newValue = formatDate(inputDate)
139147
}
140148

src/components/forms/DatePicker/utils.test.ts

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,32 +84,64 @@ describe('isDateInvalid', () => {
8484
it('returns false if the date is within the min & max', () => {
8585
const testMin = new Date('May 1, 1988')
8686
const testMax = new Date('June 1, 1988')
87-
expect(isDateInvalid('05/16/1988', testMin, testMax)).toEqual(false)
87+
expect(
88+
isDateInvalid(
89+
'05/16/1988',
90+
DEFAULT_EXTERNAL_DATE_FORMAT,
91+
testMin,
92+
testMax
93+
)
94+
).toEqual(false)
8895
})
8996

9097
it('returns true if the date is not within the min & max', () => {
9198
const testMin = new Date('May 1, 1988')
9299
const testMax = new Date('June 1, 1988')
93-
expect(isDateInvalid('08/16/1988', testMin, testMax)).toEqual(true)
100+
expect(
101+
isDateInvalid(
102+
'08/16/1988',
103+
DEFAULT_EXTERNAL_DATE_FORMAT,
104+
testMin,
105+
testMax
106+
)
107+
).toEqual(true)
94108
})
95109

96110
it('returns true if the date is not valid', () => {
97111
const testMin = new Date('May 1, 1988')
98112
const testMax = new Date('June 1, 1988')
99-
expect(isDateInvalid('not a date', testMin, testMax)).toEqual(true)
113+
expect(
114+
isDateInvalid(
115+
'not a date',
116+
DEFAULT_EXTERNAL_DATE_FORMAT,
117+
testMin,
118+
testMax
119+
)
120+
).toEqual(true)
100121
})
101122

102123
describe('with no max date', () => {
103124
it('returns false if the date is after the min', () => {
104125
const testMin = new Date('May 1, 1988')
105-
expect(isDateInvalid('05/16/1988', testMin)).toEqual(false)
126+
expect(
127+
isDateInvalid('05/16/1988', DEFAULT_EXTERNAL_DATE_FORMAT, testMin)
128+
).toEqual(false)
106129
})
107130

108131
it('returns true if the date is not after the min', () => {
109132
const testMin = new Date('May 1, 1988')
110-
expect(isDateInvalid('02/16/1988', testMin)).toEqual(true)
133+
expect(
134+
isDateInvalid('02/16/1988', DEFAULT_EXTERNAL_DATE_FORMAT, testMin)
135+
).toEqual(true)
111136
})
112137
})
138+
139+
describe('with YYYY-MM-DD date format', () => {
140+
const testMin = new Date('May 1, 1988')
141+
expect(isDateInvalid('1988-16-02', INTERNAL_DATE_FORMAT, testMin)).toEqual(
142+
true
143+
)
144+
})
113145
})
114146

115147
describe('isDateWithinMinAndMax', () => {

src/components/forms/DatePicker/utils.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,7 @@ export const formatDate = (
460460

461461
export const isDateInvalid = (
462462
dateString: string,
463+
dateFormat: DateFormat,
463464
minDate: Date,
464465
maxDate?: Date
465466
): boolean => {
@@ -468,14 +469,23 @@ export const isDateInvalid = (
468469
if (dateString) {
469470
isInvalid = true
470471

471-
const dateStringParts = dateString.split('/')
472-
const [month, day, year] = dateStringParts.map((str) => {
472+
const dateStringParts = dateString.split(
473+
dateFormat === DEFAULT_EXTERNAL_DATE_FORMAT ? '/' : '-'
474+
)
475+
const dateParts = dateStringParts.map((str) => {
473476
let value
474477
const parsed = parseInt(str, 10)
475478
if (!Number.isNaN(parsed)) value = parsed
476479
return value
477480
})
478481

482+
let month, day, year
483+
if (dateFormat === DEFAULT_EXTERNAL_DATE_FORMAT) {
484+
;[month, day, year] = dateParts
485+
} else {
486+
;[year, month, day] = dateParts
487+
}
488+
479489
if (month && day && year != null) {
480490
const checkDate = setDate(year, month - 1, day)
481491

0 commit comments

Comments
 (0)