@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
90 lines (89 loc) • 3.09 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
}) => {
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 (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 === 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 && _jsx(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'
})]
});
};
withComponentMarkers(DialogAction, {
_supportsSpacingProps: true
});
export default DialogAction;
//# sourceMappingURL=DialogAction.js.map