UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

92 lines (91 loc) 4.04 kB
"use client"; import _extends from "@babel/runtime/helpers/esm/extends"; import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; const _excluded = ["declineText", "confirmText", "hideDecline", "hideConfirm", "onConfirm", "onDecline", "className", "children"]; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import React, { useCallback, useContext } from 'react'; import classNames from 'classnames'; import Button from '../../button/Button'; import Space from '../../space/Space'; import { Context } from '../../../shared'; import ModalContext from '../../modal/ModalContext'; import { dispatchCustomElementEvent } from '../../../shared/component-helper'; const fallbackCloseAction = _ref => { let { close } = _ref; return close(); }; const DialogAction = _ref2 => { var _translation$Dialog, _translation$Dialog2; let { declineText = null, confirmText = null, hideDecline = false, hideConfirm = false, onConfirm = fallbackCloseAction, onDecline = fallbackCloseAction, className, children } = _ref2, props = _objectWithoutProperties(_ref2, _excluded); 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, _objectSpread(_objectSpread({}, 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 ? 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 ? 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