Skip to content

Commit

Permalink
fix: Addressed CC comment
Browse files Browse the repository at this point in the history
  • Loading branch information
bmingles committed Feb 27, 2023
1 parent 7c8c635 commit d652ffd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
13 changes: 9 additions & 4 deletions packages/components/src/BasicModal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,15 @@ it('focuses default button on first render after opening', () => {
expect(getByTestId(`${DEFAULT_TEST_ID}-btn-confirm`)).toHaveFocus();
});

it('focuses cancel button on first render if isConfirmDanger is true', () => {
const { getByTestId } = makeWrapper({ isConfirmDanger: true });
expect(getByTestId(`${DEFAULT_TEST_ID}-btn-cancel`)).toHaveFocus();
});
it.each([true, false])(
'sets button style based on isConfirmDanger value: %s',
isConfirmDanger => {
const { getByTestId } = makeWrapper({ isConfirmDanger });
expect(getByTestId(`${DEFAULT_TEST_ID}-btn-confirm`)).toHaveClass(
isConfirmDanger ? 'btn-danger' : 'btn-primary'
);
}
);

it('does not re-focus default button on re-render', async () => {
const user = userEvent.setup();
Expand Down
10 changes: 2 additions & 8 deletions packages/components/src/BasicModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ function BasicModal(props: BasicModalProps) {
'data-testid': dataTestId,
} = props;

const cancelButton = useRef<HTMLButtonElement>(null);
const confirmButton = useRef<HTMLButtonElement>(null);

const disableModalCheckbox = useRef<HTMLInputElement>(null);
Expand All @@ -67,12 +66,8 @@ function BasicModal(props: BasicModalProps) {
}, [onConfirm, onModalDisable]);

const onOpened = useCallback(() => {
if (isConfirmDanger) {
cancelButton.current?.focus();
} else {
confirmButton.current?.focus();
}
}, [isConfirmDanger]);
confirmButton.current?.focus();
}, []);

let modalBody = '';
if (isOpen) {
Expand Down Expand Up @@ -124,7 +119,6 @@ function BasicModal(props: BasicModalProps) {
kind="secondary"
data-dismiss="modal"
onClick={onCancel}
ref={cancelButton}
data-testid={
dataTestId !== undefined ? `${dataTestId}-btn-cancel` : undefined
}
Expand Down

0 comments on commit d652ffd

Please sign in to comment.