Skip to content

Commit

Permalink
feat: 🎸 throwAlertOnCancel control prop for SQFormDialog (#807)
Browse files Browse the repository at this point in the history
* feat: 🎸 throwAlertOnCancel control prop for SQFormDialog

throwAlertOnCancel allows the option to override the Are You Sure dialog
when cancelling SQFormDialog while in the middle of filling out. Set to
true and will throw alert by default (same as previous behavior).

✅ Closes: #806
  • Loading branch information
samasastudio authored Oct 5, 2022
1 parent 938101a commit 3d2b1f2
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/components/SQFormDialog/SQFormDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ export type SQFormDialogProps<Values extends FormikValues> = {
helperText?: string;
/** helper text type to eventually get passed to SQFormHelperText component */
helperTextType?: 'fail' | 'error' | 'valid';
/** option to throw an Are You Sure alert when hitting cancel while in the middle of filling out a the form. true by default. */
throwAlertOnCancel?: boolean;
};

function SQFormDialog<Values extends FormikValues>({
Expand All @@ -93,6 +95,7 @@ function SQFormDialog<Values extends FormikValues>({
tertiaryButtonVariant = 'outlined',
helperText,
helperTextType = 'error',
throwAlertOnCancel = true,
}: SQFormDialogProps<Values>): React.ReactElement {
const initialErrors = useInitialRequiredErrors(
validationSchema,
Expand Down Expand Up @@ -128,6 +131,7 @@ function SQFormDialog<Values extends FormikValues>({
tertiaryButtonVariant={tertiaryButtonVariant}
helperText={helperText}
helperTextType={helperTextType}
throwAlertOnCancel={throwAlertOnCancel}
/>
</Formik>
);
Expand Down
5 changes: 4 additions & 1 deletion src/components/SQFormDialog/SQFormDialogInner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ type SQFormDialogInnerProps<Values extends FormikValues> = {
helperText?: string;
/** helper text type to pass to SQFormHelperText component */
helperTextType?: 'fail' | 'error' | 'valid';
/** option to throw an Are You Sure alert when hitting cancel while in the middle of filling out a the form. true by default. */
throwAlertOnCancel?: boolean;
};

/*
Expand Down Expand Up @@ -136,6 +138,7 @@ function SQFormDialogInner<Values extends FormikValues>({
tertiaryButtonVariant,
helperText,
helperTextType = 'error',
throwAlertOnCancel = true,
}: SQFormDialogInnerProps<Values>): React.ReactElement {
const theme = useTheme();
const titleClasses = useTitleStyles(theme);
Expand Down Expand Up @@ -169,7 +172,7 @@ function SQFormDialogInner<Values extends FormikValues>({
return;
}

if (!formikContext.dirty) {
if (!formikContext.dirty || !throwAlertOnCancel) {
onClose && onClose(event, reason);
} else {
openDialogAlert();
Expand Down
45 changes: 45 additions & 0 deletions stories/__tests__/SQFormDialog.stories.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,48 @@ describe('Tests for Tertiary Button', () => {
).toBeDisabled();
});
});

describe('Tests for throwAlertOnCancel', () => {
it('should skip alerts if throwAlertOnCancel is set to false and form is currently dirty', async () => {
// render component with throwAlertOnCancel set to false
render(
<WithValidation
isOpen={true}
onSave={handleSave}
onClose={handleClose}
throwAlertOnCancel={false}
/>
);

// dirty form by filling out text field
const textField = screen.getByLabelText(/hello/i);
userEvent.type(textField, mockData.hello);

// select cancel and see if onClose handler is called
const cancelButton = screen.getByRole('button', {name: /cancel/i});
userEvent.click(cancelButton);

await waitFor(() => {
expect(handleClose).toHaveBeenCalledTimes(1);
});
});

it('should not skip alerts if throwAlertOnCancel is set to true and form is currently dirty', async () => {
// render component with throwAlertOnCancel set to true (by default)
render(
<WithValidation isOpen={true} onSave={handleSave} onClose={handleClose} />
);

// dirty form by filling out text field
const textField = screen.getByLabelText(/hello/i);
userEvent.type(textField, mockData.hello);

// select cancel and see if onClose handler is called
const cancelButton = screen.getByRole('button', {name: /cancel/i});
userEvent.click(cancelButton);

await waitFor(() => {
expect(handleClose).toHaveBeenCalledTimes(0);
});
});
});

0 comments on commit 3d2b1f2

Please sign in to comment.