/* * Copyright (c) 2015 Nordic Semiconductor ASA * * SPDX-License-Identifier: LicenseRef-Nordic-4-Clause */ import React, { type ReactNode } from 'react'; import Modal from 'react-bootstrap/Modal'; import Button, { type ButtonVariants } from '../Button/Button'; import Spinner from '../Spinner/Spinner'; import './dialog.scss'; type CoreProps = { isVisible: boolean; onHide?: () => void; className?: string; size?: 'sm' | 'm' | 'lg' | 'xl'; children: ReactNode; }; type DialogProps = CoreProps & { closeOnUnfocus?: boolean; closeOnEsc?: boolean; }; export const Dialog = ({ isVisible, closeOnUnfocus = false, closeOnEsc = false, onHide = () => {}, className = '', size = 'm', children, }: DialogProps) => ( { if (closeOnEsc && onHide) { onHide(); } }} show={isVisible} size={size === 'm' ? undefined : size} backdrop={closeOnUnfocus ? true : 'static'} onHide={() => { if (closeOnUnfocus && onHide) { onHide(); } }} centered className={`dialog ${className}`} > {children} ); Dialog.Header = ({ title, headerIcon, showSpinner, }: { title: string; headerIcon?: string; showSpinner?: boolean; }) => (
{title} {showSpinner && (
)}
{headerIcon && }
); Dialog.Body = ({ children }: { children: ReactNode }) => ( {children} ); Dialog.Footer = ({ children }: { children: ReactNode }) => ( {children} ); export interface DialogButtonProps { onClick: () => void; variant?: ButtonVariants; className?: string; disabled?: boolean; children: ReactNode; } export const DialogButton = ({ variant = 'secondary', onClick, className = '', disabled = false, children, }: DialogButtonProps) => ( ); interface GenericDialogProps extends CoreProps { title: string; footer: ReactNode; headerIcon?: string; showSpinner?: boolean; closeOnUnfocus?: boolean; closeOnEsc?: boolean; } export const GenericDialog = ({ isVisible, onHide, title, headerIcon, children, className, footer, showSpinner = false, closeOnUnfocus, closeOnEsc, size, }: GenericDialogProps) => ( {children} {footer} ); interface InfoProps extends CoreProps { title?: string; headerIcon?: string; onHide: () => void; footer?: ReactNode; } export const InfoDialog = ({ isVisible, title = 'Info', headerIcon = 'information', children, onHide, size, className, footer, }: InfoProps) => ( Close} > {children} ); export const ErrorDetails = ({ detail }: { detail: string }) => (
Show technical details
            {detail}
        
); export const ErrorDialog = (props: Omit) => InfoDialog({ ...props, title: props.title ?? 'Error', headerIcon: 'alert', }); interface ConfirmationDialogProps extends Omit { title?: string; headerIcon?: string; confirmLabel?: string; onConfirm: () => void; cancelLabel?: string; onCancel?: () => void; optionalLabel?: string; onOptional?: () => void; } export const ConfirmationDialog = ({ isVisible, title = 'Confirm', headerIcon, children, className, confirmLabel = 'Confirm', onConfirm, cancelLabel = 'Cancel', onCancel, optionalLabel, onOptional, size, }: ConfirmationDialogProps) => ( {confirmLabel} {onOptional && optionalLabel && ( {optionalLabel} )} {onCancel && ( {cancelLabel} )} } > {children} );