Skip to content

Commit 5a98f48

Browse files
committed
Add DateSelector dateFormat prop
1 parent 302c27a commit 5a98f48

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

src/components/forms/DatePicker/DatePicker.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
DEFAULT_EXTERNAL_DATE_FORMAT,
1313
VALIDATION_MESSAGE,
1414
DEFAULT_MIN_DATE,
15+
type DateFormat,
1516
} from './constants'
1617
import { DatePickerLocalization, EN_US } from './i18n'
1718
import {
@@ -33,6 +34,7 @@ type BaseDatePickerProps = {
3334
validationStatus?: ValidationStatus
3435
disabled?: boolean
3536
required?: boolean
37+
dateFormat?: DateFormat
3638
defaultValue?: string
3739
minDate?: string
3840
maxDate?: string
@@ -57,6 +59,7 @@ export const DatePicker = ({
5759
name,
5860
className,
5961
validationStatus,
62+
dateFormat = DEFAULT_EXTERNAL_DATE_FORMAT,
6063
defaultValue,
6164
disabled,
6265
required,
@@ -68,6 +71,8 @@ export const DatePicker = ({
6871
i18n = EN_US,
6972
...inputProps
7073
}: DatePickerProps): React.ReactElement => {
74+
dateFormat ??= DEFAULT_EXTERNAL_DATE_FORMAT
75+
7176
const datePickerEl = useRef<HTMLDivElement>(null)
7277
const externalInputEl = useRef<HTMLInputElement>(null)
7378

@@ -109,7 +114,7 @@ export const DatePicker = ({
109114
const handleSelectDate = (dateString: string, closeCalendar = true): void => {
110115
const parsedValue = parseDateString(dateString)
111116
const formattedValue =
112-
parsedValue && formatDate(parsedValue, DEFAULT_EXTERNAL_DATE_FORMAT)
117+
parsedValue && formatDate(parsedValue, dateFormat)
113118

114119
if (parsedValue) setInternalValue(dateString)
115120
if (formattedValue) setExternalValue(formattedValue)
@@ -128,7 +133,7 @@ export const DatePicker = ({
128133
setExternalValue(value)
129134
if (onChange) onChange(value)
130135

131-
const inputDate = parseDateString(value, DEFAULT_EXTERNAL_DATE_FORMAT, true)
136+
const inputDate = parseDateString(value, dateFormat, true)
132137
let newValue = ''
133138
if (inputDate && !isDateInvalid(value, parsedMinDate, parsedMaxDate)) {
134139
newValue = formatDate(inputDate)
@@ -181,7 +186,7 @@ export const DatePicker = ({
181186
// calendar is closed, show it
182187
const inputDate = parseDateString(
183188
externalValue,
184-
DEFAULT_EXTERNAL_DATE_FORMAT,
189+
dateFormat,
185190
true
186191
)
187192

@@ -347,5 +352,6 @@ export const DatePicker = ({
347352
}
348353

349354
DatePicker.defaultProps = {
355+
dateFormat: DEFAULT_EXTERNAL_DATE_FORMAT,
350356
minDate: DEFAULT_MIN_DATE,
351357
}

src/components/forms/DatePicker/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ export const YEAR_CHUNK = 12
3434
export const DEFAULT_MIN_DATE = '0000-01-01'
3535
export const DEFAULT_EXTERNAL_DATE_FORMAT = 'MM/DD/YYYY'
3636
export const INTERNAL_DATE_FORMAT = 'YYYY-MM-DD'
37+
export type DateFormat = typeof INTERNAL_DATE_FORMAT | typeof DEFAULT_EXTERNAL_DATE_FORMAT

src/components/forms/DatePicker/utils.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import React, { KeyboardEvent } from 'react'
22

3-
import { DEFAULT_EXTERNAL_DATE_FORMAT, INTERNAL_DATE_FORMAT } from './constants'
3+
import {
4+
type DateFormat,
5+
DEFAULT_EXTERNAL_DATE_FORMAT,
6+
INTERNAL_DATE_FORMAT,
7+
} from './constants'
48

59
/**
610
* This file contains the USWDS DatePicker date manipulation functions converted to TypeScript
@@ -355,13 +359,13 @@ export const isDatesYearOutsideMinOrMax = (
355359
* Parse a date with format M-D-YY
356360
*
357361
* @param {string} dateString the date string to parse
358-
* @param {string} dateFormat the format of the date string
362+
* @param {DateFormat} dateFormat the format of the date string
359363
* @param {boolean} adjustDate should the date be adjusted
360364
* @returns {Date} the parsed date
361365
*/
362366
export const parseDateString = (
363367
dateString: string,
364-
dateFormat: string = INTERNAL_DATE_FORMAT,
368+
dateFormat: DateFormat = INTERNAL_DATE_FORMAT,
365369
adjustDate = false
366370
): Date | undefined => {
367371
let date
@@ -430,12 +434,12 @@ export const parseDateString = (
430434
* Format a date to format YYYY-MM-DD
431435
*
432436
* @param {Date} date the date to format
433-
* @param {string} dateFormat the format of the date string
437+
* @param {DateFormat} dateFormat the format of the date string
434438
* @returns {string} the formatted date string
435439
*/
436440
export const formatDate = (
437441
date: Date,
438-
dateFormat: string = INTERNAL_DATE_FORMAT
442+
dateFormat: DateFormat = INTERNAL_DATE_FORMAT
439443
): string => {
440444
const padZeros = (value: number, length: number): string => {
441445
return `0000${value}`.slice(-length)

0 commit comments

Comments
 (0)