@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
73 lines (72 loc) • 2.28 kB
JavaScript
"use client";
import _extends from "@babel/runtime-corejs3/helpers/esm/extends";
var _Hr;
import React, { useContext, useEffect, useState } from 'react';
import classnames from 'classnames';
import { Hr } from "../../../../elements/index.js";
import { Flex, FormStatus, Space } from "../../../../components/index.js";
import IterateItemContext from "../IterateItemContext.js";
import ToolbarContext from "./ToolbarContext.js";
import FieldBoundaryContext from "../../DataContext/FieldBoundary/FieldBoundaryContext.js";
import ArrayItemAreaContext from "../Array/ArrayItemAreaContext.js";
import { useTranslation } from "../../hooks/index.js";
export default function Toolbar({
children,
className,
...rest
} = {}) {
const {
index,
value,
arrayValue: items
} = useContext(IterateItemContext) || {};
const {
toolbarVariant,
divider
} = useContext(ArrayItemAreaContext) || {};
const {
errorInContainer
} = useTranslation().IterateEditContainer;
const {
hasError,
hasVisibleError
} = useContext(FieldBoundaryContext) || {};
const [showError, setShowError] = useState(false);
useEffect(() => {
if (showError && !hasError) {
setShowError(false);
}
}, [hasError, showError]);
if (typeof children === 'function') {
var _children;
children = (_children = children) === null || _children === void 0 ? void 0 : _children({
index,
items,
value
});
}
if (React.Children.count(children) === 0) {
return null;
}
return React.createElement(Space, _extends({
top: toolbarVariant === 'custom' ? false : 'medium',
className: classnames('dnb-forms-iterate-toolbar', className)
}, rest), toolbarVariant !== 'custom' && divider !== 'line' && (_Hr || (_Hr = React.createElement(Hr, {
space: 0
}))), React.createElement(ToolbarContext.Provider, {
value: {
setShowError
}
}, React.createElement(Flex.Horizontal, {
top: toolbarVariant === 'custom' ? false : 'x-small',
gap: "large"
}, children)), React.createElement(FormStatus, {
show: showError && hasVisibleError,
shellSpace: {
top: 'x-small'
},
no_animation: false
}, errorInContainer));
}
Toolbar._supportsSpacingProps = true;
//# sourceMappingURL=Toolbar.js.map