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

feat: isConfirmDangerProp #1110

Merged
merged 3 commits into from
Feb 27, 2023
Merged
Changes from 1 commit
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
14 changes: 11 additions & 3 deletions packages/components/src/BasicModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface BasicModalProps {
cancelButtonText?: string;
confirmButtonText?: string;
discardButtonText?: string;
isConfirmDanger?: boolean;
children?: React.ReactNode;
'data-testid'?: string;
}
Expand Down Expand Up @@ -44,10 +45,12 @@ function BasicModal(props: BasicModalProps) {
cancelButtonText = 'Cancel',
confirmButtonText = 'Okay',
discardButtonText = 'Discard',
isConfirmDanger = false,
children,
'data-testid': dataTestId,
} = props;

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

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

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would probably trust the user and keep the focus on the primary action.

cancelButton.current?.focus();
} else {
confirmButton.current?.focus();
}
}, [isConfirmDanger]);

let modalBody = '';
if (isOpen) {
Expand Down Expand Up @@ -117,6 +124,7 @@ function BasicModal(props: BasicModalProps) {
kind="secondary"
data-dismiss="modal"
onClick={onCancel}
ref={cancelButton}
data-testid={
dataTestId !== undefined ? `${dataTestId}-btn-cancel` : undefined
}
Expand All @@ -126,7 +134,7 @@ function BasicModal(props: BasicModalProps) {
)}
<ButtonGroup>
<Button
kind="primary"
kind={isConfirmDanger ? 'danger' : 'primary'}
onClick={onConfirmClicked}
ref={confirmButton}
data-testid={
Expand Down