Skip to content

Commit

Permalink
Fixed no UI feedback on FormDatePicker when maxDate validation constr…
Browse files Browse the repository at this point in the history
…aint is present (#1952)
  • Loading branch information
velnachev authored Oct 7, 2024
1 parent da8aa1e commit 36262bb
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
6 changes: 6 additions & 0 deletions src/common/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ const colors = {
white: {
main: '#ffffff',
},
red: {
error: '#ED1D1D',
},
}

const borders = {
Expand Down Expand Up @@ -67,6 +70,9 @@ export const themeOptions: ThemeOptions = {
light: colors.blue.mainDark,
dark: darken(colors.blue.dark, 0.2),
},
error: {
main: colors.red.error,
},
},
shape: {
borderRadius: 3,
Expand Down
28 changes: 16 additions & 12 deletions src/components/client/auth/profile/UpdateBirthdateModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,6 @@ const parseDateString = (value: string, originalValue: string) => {

const maxDate = new Date(new Date().setFullYear(new Date().getFullYear() - 18))

const validationSchema: yup.SchemaOf<Pick<UpdateUserAccount, 'birthday'>> = yup
.object()
.defined()
.shape({
birthday: yup
.date()
.transform(parseDateString)
.max(maxDate, 'profile:birthdateModal.ageInvalid')
.required(),
})

function UpdateBirthdateModal({
isOpen,
handleClose,
Expand All @@ -76,6 +65,17 @@ function UpdateBirthdateModal({
const { t } = useTranslation()
const [loading, setLoading] = useState(false)

const validationSchema: yup.SchemaOf<Pick<UpdateUserAccount, 'birthday'>> = yup
.object()
.defined()
.shape({
birthday: yup
.date()
.transform(parseDateString)
.max(maxDate, t('profile:birthdateModal.ageInvalid'))
.required(),
})

const dateBefore18Years = new Date(new Date().setFullYear(new Date().getFullYear() - 18))

const initialValues: Pick<UpdateUserAccount, 'birthday'> = {
Expand Down Expand Up @@ -119,7 +119,11 @@ function UpdateBirthdateModal({
validationSchema={validationSchema}>
<Grid container spacing={3}>
<Grid item xs={12} sm={8}>
<FormDatePicker name="birthday" label={t('profile:birthdateModal.question')} />
<FormDatePicker
name="birthday"
label={t('profile:birthdateModal.question')}
maxDate={maxDate}
/>
</Grid>
<Grid item xs={6}>
<SubmitButton fullWidth label="auth:cta.send" loading={loading} />
Expand Down
28 changes: 25 additions & 3 deletions src/components/common/form/FormDatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,27 @@ import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns'
import { format } from 'date-fns'
import { useField, useFormikContext } from 'formik'
import { useTranslation } from 'next-i18next'
import theme from 'common/theme'

import { DATE_VALUE_FORMAT, getDateFormat } from 'common/util/date'

interface FormDatePickerProps {
name: string
label: string
maxDate?: Date
}

const errorValidationColor = theme.palette.error as unknown as string

/**
* 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
* @param maxDate - optional maximal selectable date
* @returns
*/
export default function FormDatePicker({ name, label }: { name: string; label: string }) {
const [field] = useField(name)
export default function FormDatePicker({ name, label, maxDate }: FormDatePickerProps) {
const [field, meta] = useField(name)
const { setFieldValue } = useFormikContext()
const { i18n } = useTranslation()

Expand Down Expand Up @@ -42,7 +52,19 @@ export default function FormDatePicker({ name, label }: { name: string; label: s
label={label}
value={new Date(field?.value)}
onChange={(newValue) => updateValue(newValue)}
slotProps={{ textField: { size: 'small' } }}
slotProps={{ textField: { size: 'small', helperText: maxDate && meta.error } }}
sx={
maxDate && meta.error
? {
'&.MuiOutlinedInput-root': {
'& fieldset, &:hover fieldset, &.Mui-focused fieldset': {
borderColor: errorValidationColor,
},
},
}
: undefined
}
maxDate={maxDate}
/>
</LocalizationProvider>
)
Expand Down

0 comments on commit 36262bb

Please sign in to comment.