Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DateRange: make onCancel and onSubmit events optional (web) #3812

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions packages/gestalt-datepicker/src/DateRange.jsdom.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,30 @@ describe('DateRange', () => {
await userEvent.click(secondaryEndDateButton);
expect(screen.getByDisplayValue('10 / 09 / 2024')).toBeInTheDocument();
});

it('renders apply and cancel buttons', () => {
render(
<DateRange
dateValue={{ startDate: new Date(), endDate: new Date() }}
onCancel={() => {}}
onDateChange={() => {}}
onDateError={{ startDate: () => {}, endDate: () => {} }}
onSubmit={() => {}}
/>,
);
expect(screen.getByRole('button', { name: /cancel/i })).toBeVisible();
expect(screen.getByRole('button', { name: /apply/i })).toBeVisible();
});

it('not renders apply and cancel buttons if event handlers are not present', () => {
render(
<DateRange
dateValue={{ startDate: new Date(), endDate: new Date() }}
onDateChange={() => {}}
onDateError={{ startDate: () => {}, endDate: () => {} }}
/>,
);
expect(screen.queryByRole('button', { name: /cancel/i })).toBeNull();
expect(screen.queryByRole('button', { name: /apply/i })).toBeNull();
});
});
42 changes: 22 additions & 20 deletions packages/gestalt-datepicker/src/DateRange.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type Props = {
/**
* Callback triggered when the user clicks the Cancel button to not persist the selected dates. It should be used to close DateRange. See the [controlled component variant](https://gestalt.pinterest.systems/web/daterange#Controlled-component) to learn more.
*/
onCancel: () => void;
onCancel?: () => void;
/**
* Callback triggered when the end date input loses focus. See the [error messaging variant](https://gestalt.pinterest.systems/web/daterange#Error-messaging) to learn more.
*/
Expand Down Expand Up @@ -92,7 +92,7 @@ type Props = {
/**
* Callback triggered when the user clicks the Apply button to persist the selected dates. It should be used to persist the dates selected and close the DateRange. See the [controlled component variant](https://gestalt.pinterest.systems/web/daterange#Controlled-component) to learn more.
*/
onSubmit: () => void;
onSubmit?: () => void;
/**
* An optional RadioGroup to provide preestablished date range options. See the [with RadioGroup variant](https://gestalt.pinterest.systems/web/daterange#With-RadioGroup) to learn more.
*/
Expand Down Expand Up @@ -364,25 +364,27 @@ function DateRange({
}
/>
</Box>
<Flex.Item alignSelf={isMobile ? 'center' : 'end'}>
<Box marginBottom={4} marginEnd={4}>
<ButtonGroup>
<Button color="gray" onClick={() => onCancel()} text={cancelText} />
{onSubmit && onCancel ? (
<Flex.Item alignSelf={isMobile ? 'center' : 'end'}>
<Box marginBottom={4} marginEnd={4}>
<ButtonGroup>
<Button color="gray" onClick={() => onCancel()} text={cancelText} />

<Button
color="red"
disabled={
!!dateErrorMessage?.startDate ||
!!dateErrorMessage?.endDate ||
!dateValue.startDate ||
!dateValue.endDate
}
onClick={() => onSubmit()}
text={applyText}
/>
</ButtonGroup>
</Box>
</Flex.Item>
<Button
color="red"
disabled={
!!dateErrorMessage?.startDate ||
!!dateErrorMessage?.endDate ||
!dateValue.startDate ||
!dateValue.endDate
}
onClick={() => onSubmit()}
text={applyText}
/>
</ButtonGroup>
</Box>
</Flex.Item>
) : null}
</Flex>
</Box>
</Flex>
Expand Down
Loading