Skip to content

Commit

Permalink
Merge pull request #199 from mapbox/disabled-state-modal-actions
Browse files Browse the repository at this point in the history
Support disabled state in modal actions
  • Loading branch information
tristen authored Oct 4, 2022
2 parents f90fce3 + 69d237b commit c691183
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ Changelog

## HEAD

- Support `disabled` option in modal actions. [#199](https://github.com/mapbox/mr-ui/pull/199)


## 2.0.0

- [fix] added new styling for ControlToggleGroup component that is consistent with actual usage.
Expand Down
24 changes: 24 additions & 0 deletions src/components/modal/modal-actions.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,30 @@ describe('ModalActions', () => {
fireEvent.click(actions[2]);
expect(mockedTertiaryCallback).toHaveBeenCalledTimes(1);
});

test('disabled callbacks', () => {
const mockedPrimaryCallback = jest.fn();
const mockedSecondaryCallback = jest.fn();
const mockedTertiaryCallback = jest.fn();
render(
<ModalActions
primaryAction={{ text: 'Okay', disabled: true, callback: mockedPrimaryCallback }}
secondaryAction={{ text: 'Cancel', disabled: true, callback: mockedSecondaryCallback }}
tertiaryAction={{ text: 'Exit', disabled: true, callback: mockedTertiaryCallback }}
/>
);

const actions = screen.queryAllByRole('button');

fireEvent.click(actions[0]);
expect(mockedPrimaryCallback).not.toHaveBeenCalled();

fireEvent.click(actions[1]);
expect(mockedSecondaryCallback).not.toHaveBeenCalled();

fireEvent.click(actions[2]);
expect(mockedTertiaryCallback).not.toHaveBeenCalled();
});
});

describe('primary and tertiary', () => {
Expand Down
15 changes: 12 additions & 3 deletions src/components/modal/modal-actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ interface Props {
text: string;
callback: () => void;
destructive?: boolean;
disabled?: boolean;
},
secondaryAction?: {
text: string;
callback: () => void;
disabled?: boolean;
},
tertiaryAction?: {
text: string;
callback: () => void;
disabled?: boolean;
}
}

Expand All @@ -34,6 +37,7 @@ export default function ModalActions({
variant="secondary"
size="medium"
onClick={secondaryAction.callback}
disabled={secondaryAction.disabled}
data-test="secondary-modal-action"
passthroughProps={{ 'aria-label': secondaryAction.text }}
>
Expand All @@ -58,6 +62,7 @@ export default function ModalActions({
aria-label={tertiaryAction.text}
variant="tertiary"
onClick={tertiaryAction.callback}
disabled={tertiaryAction.disabled}
data-test="tertiary-modal-action"
>
{tertiaryAction.text}
Expand All @@ -77,6 +82,7 @@ export default function ModalActions({
variant={primaryButtonVariant}
size="medium"
onClick={primaryAction.callback}
disabled={primaryAction.disabled}
data-test="primary-modal-action"
passthroughProps={{
tabIndex: 0,
Expand All @@ -96,14 +102,17 @@ ModalActions.propTypes = {
primaryAction: PropTypes.shape({
text: PropTypes.string.isRequired,
callback: PropTypes.func.isRequired,
destructive: PropTypes.bool
destructive: PropTypes.bool,
disabled: PropTypes.bool
}).isRequired,
secondaryAction: PropTypes.shape({
text: PropTypes.string.isRequired,
callback: PropTypes.func.isRequired
callback: PropTypes.func.isRequired,
disabled: PropTypes.bool
}),
tertiaryAction: PropTypes.shape({
text: PropTypes.string.isRequired,
callback: PropTypes.func.isRequired
callback: PropTypes.func.isRequired,
disabled: PropTypes.bool
})
};
8 changes: 5 additions & 3 deletions src/components/modal/modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ interface Props {
text: string;
callback: () => void;
destructive?: boolean;
disabled?: boolean;
},
secondaryAction?: {
text: string;
callback: () => void;
disabled?: boolean;
},
tertiaryAction?: {
text: string;
callback: () => void;
disabled?: boolean;
}
}

Expand Down Expand Up @@ -181,9 +184,8 @@ Modal.propTypes = {
*/
onExit: PropTypes.func,
/**
* A function that should the primary application node, which should be
* `aria-hidden` when the modal is open. By default, returns
* `document.getElementById('app')`.
* Modal container size. Options are `small`, `large`, or `auto`. If `auto`
* is provided, a width is not specified.
*/
size: PropTypes.oneOf(['small', 'large', 'auto']),
/**
Expand Down

0 comments on commit c691183

Please sign in to comment.