@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
150 lines (149 loc) • 5.13 kB
JavaScript
"use client";
import _extends from "@babel/runtime/helpers/esm/extends";
import React, { useCallback, useContext, useReducer, useRef } from 'react';
import classnames from 'classnames';
import { Card, HeightAnimation } from "../../../../components/index.js";
import IterateItemContext from "../IterateItemContext.js";
import ArrayItemAreaContext from "./ArrayItemAreaContext.js";
import FieldBoundaryContext from "../../DataContext/FieldBoundary/FieldBoundaryContext.js";
import { useIsomorphicLayoutEffect as useLayoutEffect } from "../../../../shared/helpers/useIsomorphicLayoutEffect.js";
function ArrayItemArea(props) {
const [, forceUpdate] = useReducer(() => ({}), {});
const {
mode,
open,
ariaLabel,
onAnimationEnd,
className,
children,
openDelay = 100,
variant = 'outline',
toolbarVariant,
...restProps
} = props;
const localContextRef = useRef();
const {
hasError,
hasSubmitError
} = useContext(FieldBoundaryContext) || {};
localContextRef.current = useContext(IterateItemContext) || {};
const nextFocusElementRef = useRef();
const {
isNew
} = localContextRef.current;
const determineMode = useCallback(() => {
const {
value,
initialContainerMode
} = localContextRef.current;
if (initialContainerMode === 'auto') {
if (hasSubmitError || hasError || !value || typeof value === 'object' && Object.keys(value).length === 0) {
return 'edit';
}
}
}, [hasError, hasSubmitError]);
if (determineMode() === 'edit') {
localContextRef.current.containerMode = 'edit';
if (!localContextRef.current.modeOptions) {
localContextRef.current.modeOptions = {};
}
localContextRef.current.modeOptions.omitFocusManagement = true;
}
if (localContextRef.current.containerMode === 'auto') {
localContextRef.current.containerMode = 'view';
}
useLayoutEffect(() => {
if (mode === 'edit') {
const editMode = determineMode();
if (editMode) {
const {
switchContainerMode
} = localContextRef.current;
switchContainerMode?.(editMode, {
omitFocusManagement: true,
preventUpdate: true
});
}
}
}, [determineMode, mode]);
const {
handleRemove,
index,
previousContainerMode,
containerMode
} = localContextRef.current;
const openRef = useRef(open !== null && open !== void 0 ? open : containerMode === mode && !isNew);
const isRemoving = useRef(false);
const setOpenState = useCallback(open => {
openRef.current = open;
forceUpdate();
}, []);
useLayoutEffect(() => {
if (!isRemoving.current) {
if (typeof open !== 'undefined') {
setOpenState(open);
} else {
if (openRef.current !== (containerMode === mode)) {
if (isNew) {
setTimeout(() => {
setOpenState(containerMode === mode);
}, openDelay);
} else {
setOpenState(containerMode === mode);
}
}
}
}
}, [containerMode, isNew, mode, open, openDelay, setOpenState]);
const setFocus = useCallback(state => {
if (localContextRef.current.modeOptions?.omitFocusManagement !== true && !hasSubmitError && containerMode === mode && containerMode !== previousContainerMode) {
if (state === 'opened') {
localContextRef.current.elementRef?.current?.focus?.();
} else if (state === 'closed') {
nextFocusElementRef.current?.focus?.();
}
}
}, [containerMode, hasSubmitError, mode, previousContainerMode]);
const handleAnimationEnd = useCallback(state => {
if (!openRef.current && isRemoving.current) {
isRemoving.current = false;
localContextRef.current.fulfillRemove?.();
}
setFocus(state);
onAnimationEnd?.(state);
}, [onAnimationEnd, setFocus]);
const handleRemoveItem = useCallback(() => {
try {
nextFocusElementRef.current = Array.from(localContextRef.current.elementRef.current.parentElement.childNodes).at(index - 1);
} catch (e) {}
isRemoving.current = true;
setOpenState(false);
handleRemove?.({
keepItems: true
});
}, [handleRemove, index, setOpenState]);
return React.createElement(ArrayItemAreaContext.Provider, {
value: {
handleRemoveItem,
variant,
toolbarVariant,
divider: restProps.divider
}
}, React.createElement(HeightAnimation, {
className: classnames('dnb-forms-section-block', className, variant && `dnb-forms-section-block--variant-${variant}`, isNew && 'dnb-forms-section-block--new', hasSubmitError && 'dnb-forms-section-block--error'),
open: openRef.current,
onAnimationEnd: handleAnimationEnd,
duration: 450,
keepInDOM: true
}, React.createElement(Card, _extends({
stack: true,
filled: variant === 'filled',
innerSpace: variant === 'basic' ? false : 'small',
className: "dnb-forms-section-block__inner"
}, restProps, {
"aria-label": ariaLabel
}), children)));
}
ArrayItemArea._supportsSpacingProps = true;
export default ArrayItemArea;
//# sourceMappingURL=ArrayItemArea.js.map