@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
262 lines (261 loc) • 11 kB
JavaScript
"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 = ["data", "defaultData", "isolatedData", "bubbleValidation", "preventUncommittedChanges", "requireCommit", "dataReference", "showResetButton", "path", "itemPath", "insertAt", "title", "required", "children", "openButton", "showOpenButtonWhen", "onCommit"],
_excluded2 = ["title", "openButton", "showOpenButton", "showResetButton", "switchContainerModeRef", "cancelHandler", "containerModeRef", "rerenderPushContainer", "preventUncommittedChanges", "outerContext", "required", "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 "core-js/modules/web.dom-collections.iterator.js";
import React, { useCallback, useContext, useEffect, useMemo, useReducer, useRef } from 'react';
import classnames from 'classnames';
import Isolation from '../../Form/Isolation';
import useHandleStatus from '../../Form/Isolation/useHandleStatus';
import PushContainerContext from './PushContainerContext';
import IterateItemContext from '../IterateItemContext';
import DataContext from '../../DataContext/Context';
import VisibilityContext from '../../Form/Visibility/VisibilityContext';
import useDataValue from '../../hooks/useDataValue';
import EditContainer, { DoneButton, CancelButton, ResetButton } from '../EditContainer';
import IterateArray from '../Array';
import OpenButton from './OpenButton';
import { Flex, FormStatus, HeightAnimation } from '../../../../components';
import { useArrayLimit, useItemPath, useSwitchContainerMode } from '../hooks';
import Toolbar from '../Toolbar';
import { usePath, useTranslation } from '../../hooks';
import { clearedData } from '../../DataContext/Provider';
import structuredClone from '@ungap/structured-clone';
function PushContainer(props) {
const [, forceUpdate] = useReducer(() => ({}), {});
const outerContext = useContext(DataContext);
const {
data: outerData,
required: requiredInherited
} = outerContext;
const {
data: dataProp,
defaultData: defaultDataProp,
isolatedData,
bubbleValidation,
preventUncommittedChanges = props === null || props === void 0 ? void 0 : props.requireCommit,
requireCommit,
dataReference,
showResetButton,
path,
itemPath,
insertAt,
title,
required = requiredInherited,
children,
openButton,
showOpenButtonWhen,
onCommit
} = props,
rest = _objectWithoutProperties(props, _excluded);
const {
absolutePath
} = useItemPath(itemPath);
const {
path: relativePath
} = usePath({
path,
itemPath
});
const commitHandleRef = useRef();
const switchContainerModeRef = useRef();
const containerModeRef = useRef();
const {
value: entries = [],
moveValueToPath,
getValueByPath
} = useDataValue(path || itemPath);
const {
setNextContainerMode
} = useSwitchContainerMode(path || absolutePath);
const {
hasReachedLimit,
setShowStatus
} = useArrayLimit(path || absolutePath);
const cancelHandler = useCallback(() => {
if (hasReachedLimit) {
setShowStatus(false);
}
}, [hasReachedLimit, setShowStatus]);
const showOpenButton = showOpenButtonWhen === null || showOpenButtonWhen === void 0 ? void 0 : showOpenButtonWhen(entries);
const newItemContextProps = {
path,
itemPath,
entries,
commitHandleRef,
switchContainerMode: switchContainerModeRef.current
};
const data = useMemo(() => {
var _Object$freeze;
if (defaultDataProp) {
return;
}
return _objectSpread(_objectSpread({}, isolatedData), {}, {
pushContainerItems: [(_Object$freeze = Object.freeze(dataProp)) !== null && _Object$freeze !== void 0 ? _Object$freeze : clearedData]
});
}, [dataProp, defaultDataProp, isolatedData]);
if (outerData) {
if (!Object.isFrozen(data)) {
Object.assign(data || {}, outerData);
}
}
const defaultData = useMemo(() => {
return _objectSpread(_objectSpread({}, !dataProp ? isolatedData : null), {}, {
pushContainerItems: [Object.freeze(defaultDataProp !== null && defaultDataProp !== void 0 ? defaultDataProp : clearedData)]
});
}, [dataProp, defaultDataProp, isolatedData]);
const emptyData = useCallback(data => {
var _data$pushContainerIt, _ref;
const firstItem = (_data$pushContainerIt = data.pushContainerItems) === null || _data$pushContainerIt === void 0 ? void 0 : _data$pushContainerIt[0];
if (firstItem === null || typeof firstItem !== 'object') {
return _objectSpread(_objectSpread({}, isolatedData), {}, {
pushContainerItems: [null]
});
}
return _objectSpread(_objectSpread({}, isolatedData), {}, {
pushContainerItems: [(_ref = dataProp !== null && dataProp !== void 0 ? dataProp : defaultDataProp) !== null && _ref !== void 0 ? _ref : clearedData]
});
}, [dataProp, defaultDataProp, isolatedData]);
return React.createElement(Isolation, {
data: data,
defaultData: defaultData,
required: required,
dataReference: dataReference,
emptyData: emptyData,
bubbleValidation: containerModeRef.current === 'view' ? false : bubbleValidation,
commitHandleRef: commitHandleRef,
transformOnCommit: _ref2 => {
let {
pushContainerItems
} = _ref2;
return moveValueToPath(absolutePath || relativePath, typeof insertAt === 'number' ? [...entries.slice(0, insertAt), ...pushContainerItems, ...entries.slice(insertAt)] : [...entries, ...pushContainerItems], absolutePath ? structuredClone(getValueByPath('/')) : {});
},
onCommit: (data, options) => {
const {
clearData,
preventCommit
} = options;
if (hasReachedLimit) {
preventCommit();
setShowStatus(true);
} else {
var _switchContainerModeR;
setNextContainerMode('view');
(_switchContainerModeR = switchContainerModeRef.current) === null || _switchContainerModeR === void 0 ? void 0 : _switchContainerModeR.call(switchContainerModeRef, 'view');
clearData();
}
onCommit === null || onCommit === void 0 ? void 0 : onCommit(data, options);
}
}, React.createElement(PushContainerContext.Provider, {
value: newItemContextProps
}, React.createElement(IterateArray, {
path: "/pushContainerItems",
containerMode: showOpenButton ? 'view' : 'edit',
withoutFlex: true,
omitSectionPath: true
}, React.createElement(NewContainer, _extends({
title: title,
openButton: openButton,
switchContainerModeRef: switchContainerModeRef,
showOpenButton: showOpenButton,
cancelHandler: cancelHandler,
containerModeRef: containerModeRef,
rerenderPushContainer: forceUpdate,
preventUncommittedChanges: preventUncommittedChanges,
showResetButton: showResetButton,
outerContext: outerContext,
required: required
}, rest), children))));
}
function NewContainer(_ref3) {
var _DoneButton, _CancelButton, _FormStatus;
let {
title,
openButton,
showOpenButton,
showResetButton,
switchContainerModeRef,
cancelHandler,
containerModeRef,
rerenderPushContainer,
preventUncommittedChanges,
outerContext,
required,
children
} = _ref3,
rest = _objectWithoutProperties(_ref3, _excluded2);
const {
containerMode,
switchContainerMode
} = useContext(IterateItemContext) || {};
containerModeRef.current = containerMode;
const {
hasContentChanged,
showStatus
} = useHandleStatus({
outerContext,
preventUncommittedChanges,
error: pushContainerError,
name: 'push-container'
});
useEffect(() => {
rerenderPushContainer();
}, [containerMode, rerenderPushContainer]);
const visibilityContext = useContext(VisibilityContext);
switchContainerModeRef.current = switchContainerMode;
const isVisible = (visibilityContext === null || visibilityContext === void 0 ? void 0 : visibilityContext.isVisible) === false ? false : Boolean(!showOpenButton || containerMode === 'edit' || (required || hasContentChanged) && showStatus);
const {
preventUncommittedChangesText
} = useTranslation().Isolation;
const {
createButton
} = useTranslation().IteratePushContainer;
const {
clearData
} = useContext(DataContext) || {};
const restoreOriginalValue = useCallback(() => {
clearData === null || clearData === void 0 ? void 0 : clearData();
}, [clearData]);
const toolbar = React.createElement(Toolbar, null, React.createElement(IterateItemContext.Consumer, null, context => {
const newItemContextProps = _objectSpread(_objectSpread({}, context), {}, {
restoreOriginalValue
});
return React.createElement(IterateItemContext.Provider, {
value: newItemContextProps
}, React.createElement(Flex.Horizontal, {
gap: "large"
}, _DoneButton || (_DoneButton = React.createElement(DoneButton, {
text: createButton
})), showOpenButton && (_CancelButton || (_CancelButton = React.createElement(CancelButton, {
onClick: cancelHandler
}))), (preventUncommittedChanges || showResetButton) && React.createElement(ResetButton, {
hidden: !(showResetButton || showStatus)
})), preventUncommittedChanges && showStatus && (_FormStatus || (_FormStatus = React.createElement(FormStatus, {
no_animation: false,
show: hasContentChanged
}, preventUncommittedChangesText))));
}));
return React.createElement(VisibilityContext.Provider, {
value: {
isVisible,
keepInDOM: false
}
}, React.createElement(EditContainer, _extends({
open: isVisible,
title: title,
toolbar: toolbar
}, rest, {
className: classnames('dnb-forms-section-block--error', rest.className)
}), children), openButton && typeof showOpenButton === 'boolean' && React.createElement(HeightAnimation, {
open: showOpenButton && containerMode === 'view'
}, openButton));
}
const pushContainerError = new Error('Iterate.PushContainer');
PushContainer.OpenButton = OpenButton;
PushContainer._supportsSpacingProps = true;
export default PushContainer;
//# sourceMappingURL=PushContainer.js.map