-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Consistent date format in update birthday modal (#1115)
* Consistent date format in update birthday modal * Generic formatting of date in birthday modal
- Loading branch information
Showing
4 changed files
with
100 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { TextField } from '@mui/material' | ||
import { LocalizationProvider, DatePicker } from '@mui/x-date-pickers' | ||
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns' | ||
import { format } from 'date-fns' | ||
import { useField, useFormikContext } from 'formik' | ||
import { useTranslation } from 'next-i18next' | ||
|
||
import { DATE_VALUE_FORMAT, getDateFormat } from 'common/util/date' | ||
|
||
/** | ||
* MUI date picker to be connected with Formik. Propagates updates to the passed Formik field name | ||
* @param name - name of the Formik field to bind | ||
* @param label - prompt text | ||
* @returns | ||
*/ | ||
export default function FormDatePicker({ name, label }: { name: string; label: string }) { | ||
const [field] = useField(name) | ||
const { setFieldValue } = useFormikContext() | ||
const { i18n } = useTranslation() | ||
|
||
const dateViewFormat = getDateFormat(i18n.language) | ||
const mask = dateViewFormat.replace(new RegExp(/[^./]/g), '_') | ||
|
||
const updateValue = (newValue: Date) => { | ||
let formattedValue | ||
try { | ||
formattedValue = format(newValue, DATE_VALUE_FORMAT) | ||
} catch { | ||
formattedValue = field.value | ||
} finally { | ||
setFieldValue(name, formattedValue, true) | ||
} | ||
} | ||
|
||
return ( | ||
<LocalizationProvider dateAdapter={AdapterDateFns}> | ||
<DatePicker | ||
mask={mask} | ||
inputFormat={dateViewFormat} | ||
label={label} | ||
value={field.value} | ||
onChange={(newValue) => updateValue(newValue)} | ||
renderInput={(params) => <TextField size="small" {...params} />} | ||
/> | ||
</LocalizationProvider> | ||
) | ||
} |