@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
89 lines (88 loc) • 2.61 kB
JavaScript
"use client";
import React, { useCallback, useContext } from 'react';
import clsx from 'clsx';
import Button from "../../button/Button.js";
import Space from "../../space/Space.js";
import { Context } from "../../../shared/index.js";
import ModalContext from "../../modal/ModalContext.js";
import { dispatchCustomElementEvent } from "../../../shared/component-helper.js";
import withComponentMarkers from "../../../shared/helpers/withComponentMarkers.js";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const fallbackCloseAction = ({
close
}) => close();
const DialogAction = ({
declineText = null,
confirmText = null,
hideDecline = false,
hideConfirm = false,
onConfirm = fallbackCloseAction,
onDecline = fallbackCloseAction,
className,
children,
...props
}) => {
const {
translation,
Button: ButtonContext
} = useContext(Context);
const {
close
} = useContext(ModalContext);
let childrenWithCloseFunc;
const onConfirmHandler = useCallback(event => {
dispatchCustomElementEvent({
onConfirm
}, 'onConfirm', {
event,
close
});
}, [close, onConfirm]);
const onDeclineHandler = useCallback(event => {
dispatchCustomElementEvent({
onDecline
}, 'onDecline', {
event,
close
});
}, [close, onDecline]);
if (children) {
childrenWithCloseFunc = React.Children.map(children, child => {
if (React.isValidElement(child) && child.type === Button) {
const childElement = child;
return React.createElement(childElement.type, {
...(childElement.props || {}),
onClick: event => {
dispatchCustomElementEvent(childElement.props, 'onClick', {
event,
close
});
}
}, childElement.props.children);
} else {
return child;
}
});
}
return _jsxs(Space, {
element: "section",
className: clsx('dnb-dialog__actions', className),
...props,
children: [childrenWithCloseFunc, !children && !hideDecline && _jsx(Button, {
text: declineText || translation?.Dialog?.declineText,
variant: "secondary",
onClick: onDeclineHandler,
size: ButtonContext?.size || 'large'
}), !children && !hideConfirm && _jsx(Button, {
text: confirmText || translation?.Dialog?.confirmText,
variant: "primary",
onClick: onConfirmHandler,
size: ButtonContext?.size || 'large'
})]
});
};
withComponentMarkers(DialogAction, {
_supportsSpacingProps: true
});
export default DialogAction;
//# sourceMappingURL=DialogAction.js.map