keycloak-scim/libs/ui-shared/src/continue-cancel/ContinueCancelModal.tsx

90 lines
2.1 KiB
TypeScript
Raw Normal View History

2023-01-16 10:23:07 +00:00
import { ReactNode, useState } from "react";
import { useTranslation } from "react-i18next";
import { Button, ButtonProps, Modal, ModalProps } from "@patternfly/react-core";
export type ContinueCancelModalProps = Omit<ModalProps, "ref" | "children"> & {
modalTitle: string;
2023-01-16 10:23:07 +00:00
modalMessage?: string;
buttonTitle: string | ReactNode;
2023-01-16 10:23:07 +00:00
buttonVariant?: ButtonProps["variant"];
isDisabled?: boolean;
onContinue: () => void;
continueLabel?: string;
cancelLabel?: string;
2023-01-16 10:23:07 +00:00
component?: React.ElementType<any> | React.ComponentType<any>;
children?: ReactNode;
};
export const ContinueCancelModal = ({
modalTitle,
modalMessage,
buttonTitle,
isDisabled,
buttonVariant,
onContinue,
continueLabel = "continue",
cancelLabel = "doCancel",
component = "button",
children,
...rest
}: ContinueCancelModalProps) => {
const { t } = useTranslation();
const [open, setOpen] = useState(false);
const Component = component;
return (
<>
<Component
variant={buttonVariant}
onClick={() => setOpen(true)}
isDisabled={isDisabled}
>
{
//@ts-ignore
typeof buttonTitle === "string" ? t(buttonTitle) : buttonTitle
}
</Component>
<Modal
variant="small"
{...rest}
//@ts-ignore
title={t(modalTitle)}
isOpen={open}
onClose={() => setOpen(false)}
actions={[
<Button
id="modal-confirm"
key="confirm"
variant="primary"
onClick={() => {
setOpen(false);
onContinue();
}}
>
{
//@ts-ignore
t(continueLabel)
}
</Button>,
<Button
id="modal-cancel"
key="cancel"
variant="secondary"
onClick={() => setOpen(false)}
>
{
//@ts-ignore
t(cancelLabel)
}
</Button>,
]}
>
{
//@ts-ignore
modalMessage ? t(modalMessage) : children
}
</Modal>
</>
);
};