Skip to content

Commit

Permalink
Ensuring the invalid date shows on the Date input and highlighted lik…
Browse files Browse the repository at this point in the history
…e an error
  • Loading branch information
dr-bizz committed Jun 19, 2024
1 parent a9183c8 commit 1896a5d
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,27 @@ export const PersonBirthday: React.FC<PersonBirthdayProps> = ({
setFieldValue('birthdayYear', date?.year || null);
};

const birthdayDate =
birthdayMonth && birthdayDay
? DateTime.local(birthdayYear ?? 1900, birthdayMonth, birthdayDay)
: null;

const invalidDate =
birthdayMonth === 0 && birthdayDay === 0
? DateTime.local(birthdayYear ?? 1900, birthdayMonth, birthdayDay)
.invalidExplanation !== ''
: false;

const backupBirthdayDate =
`${birthdayMonth}/${birthdayDay}/${birthdayYear}` as unknown as DateTime<boolean>;

return (
<ModalSectionContainer>
<ModalSectionIcon transform="translateY(-100%)" icon={<CakeIcon />} />
<CustomDateField
label={t('Birthday')}
value={
birthdayMonth && birthdayDay
? DateTime.local(birthdayYear ?? 1900, birthdayMonth, birthdayDay)
: null
}
invalidDate={invalidDate}
value={invalidDate ? backupBirthdayDate : birthdayDate}
onChange={(date) => handleDateChange(date)}
/>
</ModalSectionContainer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,28 @@ export const PersonShowMore: React.FC<PersonShowMoreProps> = ({
setFieldValue('anniversaryMonth', date?.month || null);
setFieldValue('anniversaryYear', date?.year || null);
};

const anniversaryDate =
anniversaryMonth && anniversaryDay
? DateTime.local(
anniversaryYear ?? 1900,
anniversaryMonth,
anniversaryDay,
)
: null;

Check warning on line 74 in src/components/Contacts/ContactDetails/ContactDetailsTab/People/Items/PersonModal/PersonShowMore/PersonShowMore.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/Contacts/ContactDetails/ContactDetailsTab/People/Items/PersonModal/PersonShowMore/PersonShowMore.tsx#L74

Added line #L74 was not covered by tests

const invalidAnniversaryDate =
anniversaryMonth === 0 && anniversaryDay === 0
? DateTime.local(

Check warning on line 78 in src/components/Contacts/ContactDetails/ContactDetailsTab/People/Items/PersonModal/PersonShowMore/PersonShowMore.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/Contacts/ContactDetails/ContactDetailsTab/People/Items/PersonModal/PersonShowMore/PersonShowMore.tsx#L78

Added line #L78 was not covered by tests
anniversaryYear ?? 1900,
anniversaryMonth,
anniversaryDay,
).invalidExplanation !== ''
: false;

const backupAnniversaryDate =
`${anniversaryMonth}/${anniversaryDay}/${anniversaryYear}` as unknown as DateTime<boolean>;

return (
<>
{/* Legal First Name and Gender Section */}
Expand Down Expand Up @@ -142,14 +164,9 @@ export const PersonShowMore: React.FC<PersonShowMoreProps> = ({
<Grid item xs={12} sm={6}>
<CustomDateField
label={t('Anniversary')}
invalidDate={invalidAnniversaryDate}
value={
anniversaryMonth && anniversaryDay
? DateTime.local(
anniversaryYear ?? 1900,
anniversaryMonth,
anniversaryDay,
)
: null
invalidAnniversaryDate ? backupAnniversaryDate : anniversaryDate
}
onChange={(date) => date && handleDateChange(date)}
/>
Expand Down
5 changes: 3 additions & 2 deletions src/components/common/DateTimePickers/CustomDateField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ export const CustomDateField: React.FC<DesktopDateFieldProps> = (props) => {
const isDesktop = useMediaQuery(DEFAULT_DESKTOP_MODE_MEDIA_QUERY, {
defaultMatches: true,
});
const { label, value, invalidDate, onChange, ...textFieldProps } = props;

if (isDesktop) {
// If value is not valid render desktop input as it can render invalid value
if (isDesktop || invalidDate) {
return <DesktopDateField {...props} />;
}

const { label, value, onChange, ...textFieldProps } = props;
return (
<MobileDatePicker<DateTime>
label={label}
Expand Down
33 changes: 32 additions & 1 deletion src/components/common/DateTimePickers/DesktopDateField.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,24 @@ import { DesktopDateField } from './DesktopDateField';
interface TestComponentProps {
value?: DateTime | null;
locale?: string;
invalidDate?: boolean;
}

const onChange = jest.fn();

const TestComponent: React.FC<TestComponentProps> = ({
value = DateTime.local(2024, 1, 2, 3, 4, 5),
locale = 'en-US',
invalidDate = false,
}) => (
<UserPreferenceContext.Provider value={{ locale, userId: 'userId' }}>
<LocalizationProvider dateAdapter={AdapterLuxon} adapterLocale={locale}>
<DesktopDateField value={value} onChange={onChange} label="Date" />
<DesktopDateField
value={value}
onChange={onChange}
label="Date"
invalidDate={invalidDate}
/>
</LocalizationProvider>
</UserPreferenceContext.Provider>
);
Expand All @@ -40,6 +47,30 @@ describe('DesktopDateField', () => {
expect(getByRole('textbox')).toHaveValue('');
});

it('shows invalid date when invalidDate prop is TRUE', () => {
const { getByRole, rerender } = render(<TestComponent />);
rerender(
<TestComponent
value={'0/0/2000' as unknown as DateTime}
invalidDate={true}
/>,
);

expect(getByRole('textbox')).toHaveValue('0/0/2000');
});

it('shows default date with a invalid date when invalidDate prop is FALSE', () => {
const { getByRole, rerender } = render(<TestComponent />);
rerender(
<TestComponent
value={'0/0/2000' as unknown as DateTime}
invalidDate={false}
/>,
);

expect(getByRole('textbox')).toHaveValue('1/2/2024');
});

it('the formatted value', () => {
const { getByRole } = render(<TestComponent />);

Expand Down
4 changes: 4 additions & 0 deletions src/components/common/DateTimePickers/DesktopDateField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ export interface DesktopDateFieldProps
extends Omit<StandardTextFieldProps, 'onChange'> {
value: DateTime | null;
onChange: (date: DateTime | null) => void;
invalidDate?: boolean;
}

export const DesktopDateField: React.FC<DesktopDateFieldProps> = ({
value,
onChange,
invalidDate,
...props
}) => {
const locale = useLocale();
Expand All @@ -30,6 +32,8 @@ export const DesktopDateField: React.FC<DesktopDateFieldProps> = ({
setRawDate('');
} else if (value.isValid) {
setRawDate(value.toFormat('D', options));
} else if (invalidDate) {
setRawDate(value as unknown as string);
}
}, [locale, value]);

Expand Down

0 comments on commit 1896a5d

Please sign in to comment.