@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
84 lines (83 loc) • 2.9 kB
JavaScript
"use client";
import _extends from "@babel/runtime-corejs3/helpers/esm/extends";
import React, { useCallback, useContext } from 'react';
import classNames from 'classnames';
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";
const fallbackCloseAction = ({
close
}) => close();
const DialogAction = ({
declineText = null,
confirmText = null,
hideDecline = false,
hideConfirm = false,
onConfirm = fallbackCloseAction,
onDecline = fallbackCloseAction,
className,
children,
...props
}) => {
var _translation$Dialog, _translation$Dialog2;
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 (child.type === Button) {
return React.cloneElement(child, {
...child.props,
on_click: event => {
dispatchCustomElementEvent(child.props, 'on_click', {
event,
close
});
}
}, child.props.children);
} else {
return child;
}
});
}
return React.createElement(Space, _extends({
element: "section",
className: classNames('dnb-dialog__actions', className)
}, props), childrenWithCloseFunc, !children && !hideDecline && React.createElement(Button, {
text: declineText || (translation === null || translation === void 0 || (_translation$Dialog = translation.Dialog) === null || _translation$Dialog === void 0 ? void 0 : _translation$Dialog.declineText),
variant: "secondary",
onClick: onDeclineHandler,
size: (ButtonContext === null || ButtonContext === void 0 ? void 0 : ButtonContext.size) || 'large'
}), !children && !hideConfirm && React.createElement(Button, {
text: confirmText || (translation === null || translation === void 0 || (_translation$Dialog2 = translation.Dialog) === null || _translation$Dialog2 === void 0 ? void 0 : _translation$Dialog2.confirmText),
variant: "primary",
onClick: onConfirmHandler,
size: (ButtonContext === null || ButtonContext === void 0 ? void 0 : ButtonContext.size) || 'large'
}));
};
DialogAction._supportsSpacingProps = true;
export default DialogAction;
//# sourceMappingURL=DialogAction.js.map