|
1 | 1 | import { SupportedDateFormat } from '@logto/schemas'; |
| 2 | +import { useEffect } from 'react'; |
2 | 3 | import { Controller, useFormContext } from 'react-hook-form'; |
3 | 4 | import { Trans, useTranslation } from 'react-i18next'; |
4 | 5 |
|
| 6 | +import { dateFnsDocumentationLink } from '@/consts'; |
5 | 7 | import FormField from '@/ds-components/FormField'; |
6 | 8 | import Select from '@/ds-components/Select'; |
7 | 9 | import TextInput from '@/ds-components/TextInput'; |
@@ -31,6 +33,21 @@ function DateFormatSelector({ index }: Props) { |
31 | 33 | const formatValue = watch(`${fieldPrefix}format`); |
32 | 34 | const formErrors = index === undefined ? errors : errors.parts?.[index]; |
33 | 35 |
|
| 36 | + useEffect(() => { |
| 37 | + if (!formatValue) { |
| 38 | + setValue(`${fieldPrefix}format`, SupportedDateFormat.US, { shouldDirty: true }); |
| 39 | + setValue(`${fieldPrefix}placeholder`, SupportedDateFormat.US, { shouldDirty: true }); |
| 40 | + } |
| 41 | + }, [fieldPrefix, formatValue, setValue]); |
| 42 | + |
| 43 | + useEffect(() => { |
| 44 | + return () => { |
| 45 | + setValue(`${fieldPrefix}format`, ''); |
| 46 | + setValue(`${fieldPrefix}placeholder`, ''); |
| 47 | + setValue(`${fieldPrefix}customFormat`, ''); |
| 48 | + }; |
| 49 | + }, [fieldPrefix, setValue]); |
| 50 | + |
34 | 51 | return ( |
35 | 52 | <div className={styles.dateFormatSelector}> |
36 | 53 | <Controller |
@@ -65,7 +82,7 @@ function DateFormatSelector({ index }: Props) { |
65 | 82 | description={ |
66 | 83 | <Trans |
67 | 84 | components={{ |
68 | | - a: <TextLink targetBlank href="https://date-fns.org/v2.30.0/docs/format" />, |
| 85 | + a: <TextLink targetBlank href={dateFnsDocumentationLink} />, |
69 | 86 | }} |
70 | 87 | > |
71 | 88 | {t('custom_date_format_tip')} |
|
0 commit comments