Skip to content

Commit

Permalink
feat: 🎸 remove isTertiaryDisabled prop and showTertiaryButton
Browse files Browse the repository at this point in the history
remove isTertiaryDisabled and showTertiaryButton props and replace them
with a new prop called tertiaryStatus.

BREAKING CHANGE: 🧨 remove isTertiaryDisabled and showTertiaryButton props and replace them
with a new prop called tertiaryStatus that will determine if we render
the tertiary button as well as how we handle disabling/enabling it.
  • Loading branch information
jamesgriffith9519 committed May 4, 2022
1 parent 5f5ab13 commit 9bc03ad
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 20 deletions.
17 changes: 9 additions & 8 deletions src/components/SQFormDialog/SQFormDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {useInitialRequiredErrors} from '../../hooks/useInitialRequiredErrors';
import type {FormikHelpers, FormikValues, FormikContextType} from 'formik';
import type {DialogProps, GridProps} from '@material-ui/core';
import type {AnyObjectSchema} from 'yup';
import type {SQFormDialogTertiaryValue} from './types';

export interface SQFormDialogProps<Values extends FormikValues> {
/** The secondary button text (Button located on left side of Dialog) */
Expand All @@ -31,12 +32,14 @@ export interface SQFormDialogProps<Values extends FormikValues> {
) => void | Promise<unknown>;
/** Determine if the secondary action button should be displayed (default: true) */
showSecondaryButton?: boolean;
/** Whether to show the tertiary button. (Default: false) */
showTertiaryButton?: boolean;
/**
* determine the status of the tertiary button
* can be one of'HIDE_BUTTON'|'NO_VALIDATION'|'IS_DISABLED'|'IS_ENABLED'|'FORM_VALIDATION_ONLY'|'IS_DISABLED_AND_FORM_VALIDATION',
* this will determine if the button is rendered, as well as if the button is disabled and if it uses form validation.
*/
tertiaryStatus: SQFormDialogTertiaryValue;
/** The tertiary button text */
tertiaryButtonText?: string;
/** Whether the tertiary button is disabled (Default: false) */
isTertiaryDisabled?: boolean;
/** Whether to show save/submit button (default: true) */
shouldDisplaySaveButton?: boolean;
/** The primary button text (Button located on right side of Dialog) */
Expand Down Expand Up @@ -79,9 +82,8 @@ function SQFormDialog<Values extends FormikValues>({
shouldRequireFieldUpdates = false,
validationSchema,
showSecondaryButton = true,
showTertiaryButton = false,
isTertiaryDisabled = false,
onTertiaryClick,
tertiaryStatus = 'HIDE_BUTTON',
}: SQFormDialogProps<Values>): React.ReactElement {
const initialErrors = useInitialRequiredErrors(
validationSchema,
Expand Down Expand Up @@ -111,9 +113,8 @@ function SQFormDialog<Values extends FormikValues>({
title={title}
muiGridProps={muiGridProps}
showSecondaryButton={showSecondaryButton}
showTertiaryButton={showTertiaryButton}
tertiaryStatus={tertiaryStatus}
tertiaryButtonText={tertiaryButtonText}
isTertiaryDisabled={isTertiaryDisabled}
onTertiaryClick={onTertiaryClick}
/>
</Formik>
Expand Down
33 changes: 24 additions & 9 deletions src/components/SQFormDialog/SQFormDialogInner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {DialogProps, GridProps} from '@material-ui/core';
import type {Theme} from '@material-ui/core/styles';
import type {TransitionProps} from '@material-ui/core/transitions';
import type {FormikContextType, FormikValues} from 'formik';
import type {SQFormDialogTertiaryValue} from './types';

interface SQFormDialogInnerProps<Values extends FormikValues> {
/** The secondary button text (Button located on left side of Dialog) */
Expand Down Expand Up @@ -45,16 +46,18 @@ interface SQFormDialogInnerProps<Values extends FormikValues> {
shouldRequireFieldUpdates?: boolean;
/** Title text at the top of the Dialog */
title: string;
/**
* determine the status of the tertiary button
* can be one of'HIDE_BUTTON'|'NO_VALIDATION'|'IS_DISABLED'|'IS_ENABLED'|'FORM_VALIDATION_ONLY'|'IS_DISABLED_AND_FORM_VALIDATION',
* this will determine if the button is rendered, as well as if the button is disabled and if it uses form validation.
*/
tertiaryStatus: SQFormDialogTertiaryValue;
/** Any prop from https://material-ui.com/api/grid */
muiGridProps?: GridProps;
/** Determine if the secondary action button should be displayed */
showSecondaryButton?: boolean;
/** Whether to show the tertiary button. (Default: false) */
showTertiaryButton?: boolean;
/** The tertiary button text */
tertiaryButtonText?: string;
/** Whether the tertiary button is disabled (Default: false) */
isTertiaryDisabled?: boolean;
/** Callback function invoked when the user clicks the tertiary button */
onTertiaryClick?: (formikContext: FormikContextType<Values>) => void;
}
Expand Down Expand Up @@ -121,8 +124,7 @@ function SQFormDialogInner<Values extends FormikValues>({
muiGridProps,
shouldDisplaySaveButton = true,
showSecondaryButton = true,
showTertiaryButton = false,
isTertiaryDisabled = false,
tertiaryStatus = 'HIDE_BUTTON',
onTertiaryClick,
}: SQFormDialogInnerProps<Values>): React.ReactElement {
const theme = useTheme();
Expand All @@ -132,6 +134,19 @@ function SQFormDialogInner<Values extends FormikValues>({
const dialogContentClasses = useDialogContentStyles(theme);
const formikContext = useFormikContext<Values>();

function getIsDisabled() {
switch (tertiaryStatus) {
case 'IS_DISABLED':
return true;
case 'FORM_VALIDATION_ONLY':
return !formikContext.isValid;
case 'IS_DISABLED_AND_FORM_VALIDATION':
return isDisabled || !formikContext.isValid;
default:
return false;
}
}

const {
isDialogOpen: isDialogAlertOpen,
openDialog: openDialogAlert,
Expand Down Expand Up @@ -184,7 +199,7 @@ function SQFormDialogInner<Values extends FormikValues>({
<span style={{paddingRight: '20px'}}>
<SQFormButton
title={tertiaryButtonText}
isDisabled={isTertiaryDisabled || !formikContext.isValid}
isDisabled={getIsDisabled()}
onClick={() => onTertiaryClick?.(formikContext)}
type="button"
>
Expand Down Expand Up @@ -233,7 +248,7 @@ function SQFormDialogInner<Values extends FormikValues>({
showSecondaryButton ? actionsClasses : primaryActionsClasses
}
>
{showTertiaryButton
{tertiaryStatus !== 'HIDE_BUTTON'
? renderTertiaryButton()
: showSecondaryButton && (
<RoundedButton
Expand All @@ -247,7 +262,7 @@ function SQFormDialogInner<Values extends FormikValues>({
{cancelButtonText}
</RoundedButton>
)}
{!showTertiaryButton && shouldDisplaySaveButton && (
{tertiaryStatus === 'HIDE_BUTTON' && shouldDisplaySaveButton && (
<SQFormButton
title={saveButtonText}
isDisabled={isDisabled}
Expand Down
7 changes: 7 additions & 0 deletions src/components/SQFormDialog/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type SQFormDialogTertiaryValue =
| 'HIDE_BUTTON'
| 'NO_VALIDATION'
| 'IS_DISABLED'
| 'IS_ENABLED'
| 'FORM_VALIDATION_ONLY'
| 'IS_DISABLED_AND_FORM_VALIDATION';
2 changes: 1 addition & 1 deletion stories/SQFormDialog.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ WithTertiaryButton.args = {
hello: '',
},
title: 'With Tertiary Button',
showTertiaryButton: true,
tertiaryStatus: 'IS_ENABLED',
tertiaryButtonText: 'Tertiary',
onTertiaryClick: handleTertiaryClick,
};
Expand Down
27 changes: 25 additions & 2 deletions stories/__tests__/SQFormDialog.stories.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ describe('Tests for WithValidation', () => {
isOpen={true}
onSave={handleSave}
onClose={handleClose}
showTertiaryButton={true}
tertiaryStatus="FORM_VALIDATION_ONLY"
tertiaryButtonText={'Tertiary'}
/>
);
Expand Down Expand Up @@ -204,7 +204,7 @@ describe('Tests for Tertiary Button', () => {
isOpen={true}
onSave={handleSave}
onClose={handleClose}
showTertiaryButton={true}
tertiaryStatus="IS_ENABLED"
tertiaryButtonText={tertiaryButtonText}
title={dialogTitleValue}
/>
Expand Down Expand Up @@ -238,4 +238,27 @@ describe('Tests for Tertiary Button', () => {

expect(screen.queryByText(tertiaryButtonText)).toBeNull();
});

it('should disable the tertiary button if the tertiaryStatus is IS_DISABLED_AND_FORM_VALIDATION and the isDisabled value is true', async () => {
const isLoading = true;
render(
<WithValidation
isOpen={true}
onSave={handleSave}
onClose={handleClose}
isDisabled={isLoading}
tertiaryStatus="IS_DISABLED_AND_FORM_VALIDATION"
tertiaryButtonText={'Tertiary'}
/>
);

expect(
await screen.findByRole('button', {name: /Tertiary/i})
).toBeDisabled();

const textField = screen.getByLabelText(/hello/i);
userEvent.type(textField, mockData.hello);

expect(await screen.findByRole('button', {name: /Save/i})).toBeDisabled();
});
});

0 comments on commit 9bc03ad

Please sign in to comment.