@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
193 lines (192 loc) • 8.74 kB
JavaScript
"use client";
import _extends from "@babel/runtime-corejs3/helpers/esm/extends";
import React, { Fragment, useCallback, useContext, useEffect, useMemo, useRef } from 'react';
import classnames from 'classnames';
import { warn } from "../../../shared/helpers.js";
import { Dd, Dl, Dt, Span } from "../../../elements/index.js";
import { FormLabel } from "../../../components/index.js";
import SummaryListContext from "../Value/SummaryList/SummaryListContext.js";
import ValueProviderContext from "../Value/Provider/ValueProviderContext.js";
import ValueBlockContext from "./ValueBlockContext.js";
import DataContext from "../DataContext/Context.js";
import { pickSpacingProps } from "../../../components/flex/utils.js";
import IterateItemContext from "../Iterate/IterateItemContext.js";
import { replaceItemNo } from "../Iterate/ItemNo/index.js";
import { convertJsxToString } from "../../../shared/component-helper.js";
import VisibilityContext from "../Form/Visibility/VisibilityContext.js";
import Visibility from "../Form/Visibility/Visibility.js";
import HelpButtonInline, { HelpButtonInlineContent } from "../../../components/help-button/HelpButtonInline.js";
import useId from "../../../shared/helpers/useId.js";
function ValueBlock(localProps) {
var _props$id;
const summaryListContext = useContext(SummaryListContext);
const valueBlockContext = useContext(ValueBlockContext);
const {
prerenderFieldProps
} = useContext(DataContext) || {};
const {
index: iterateIndex
} = useContext(IterateItemContext) || {};
const {
extend
} = useContext(ValueProviderContext);
const props = extend(localProps);
const id = useId((_props$id = props.id) !== null && _props$id !== void 0 ? _props$id : props.forId);
const defaultLayout = summaryListContext !== null && summaryListContext !== void 0 && summaryListContext.isNested ? 'horizontal' : 'vertical';
const {
className,
label: labelProp,
path,
itemPath,
labelSrOnly,
transformLabel = label => label,
inline,
maxWidth = props.composition ? props.maxWidth : 'large',
placeholder,
showEmpty,
children,
composition,
help,
layout = defaultLayout,
gap = 'xx-small'
} = props;
const label = useMemo(() => {
if (inline) {
return null;
}
let label = labelProp;
if (iterateIndex !== undefined) {
label = replaceItemNo(labelProp, iterateIndex);
}
const canRenderToString = React.isValidElement(label) ? typeof label.type === 'string' : true;
return canRenderToString ? transformLabel(label, transformLabelParameters) : label;
}, [inline, iterateIndex, labelProp, transformLabel]);
const hide = prerenderFieldProps || isValueEmpty(children) && !showEmpty && !placeholder;
const hasHelp = (help === null || help === void 0 ? void 0 : help.title) || (help === null || help === void 0 ? void 0 : help.content);
const isComposition = composition === true;
const isCompositionInContext = valueBlockContext === null || valueBlockContext === void 0 ? void 0 : valueBlockContext.composition;
const isCompositionInContextWithoutLabel = !label && isCompositionInContext;
const ref = useRef(null);
useNotInSummaryList(isCompositionInContext ? null : ref, label, path, itemPath);
const summaryListLayout = summaryListContext === null || summaryListContext === void 0 ? void 0 : summaryListContext.layout;
const getHelpContent = useCallback((layout, {
renderOnNextLine = false
} = {}) => {
const breakout = layout === 'vertical' || summaryListLayout === 'vertical';
const content = hasHelp && React.createElement(HelpButtonInlineContent, {
element: "span",
contentId: `${id}-help`,
className: "dnb-forms-value-block__help",
help: help,
breakout: breakout,
outset: breakout
});
return hasHelp && renderOnNextLine ? React.createElement("span", {
className: "dnb-forms-value-block__help--next-line"
}, content) : content;
}, [hasHelp, help, id, summaryListLayout]);
let content = null;
const defaultClass = `dnb-forms-value-block__content dnb-forms-value-block__content--gap-${gap === false ? 'none' : gap}` + (maxWidth ? ` dnb-forms-value-block__content--max-width-${maxWidth}` : "");
const compositionClass = isComposition && 'dnb-forms-value-block__composition--horizontal';
if (hide) {
return null;
}
if (summaryListContext && !isCompositionInContextWithoutLabel) {
const Item = summaryListContext.isNested ? Dl : summaryListContext.layout === 'horizontal' ? Dl.Item : Fragment;
if (!label && !hasHelp && isCompositionInContext) {
content = children ? React.createElement("span", {
className: defaultClass
}, children) : React.createElement("span", {
className: "dnb-forms-value-block__placeholder"
}, placeholder);
} else {
const defaultLayout = isCompositionInContext && label ? 'horizontal' : 'vertical';
const {
layout = defaultLayout
} = summaryListContext;
content = React.createElement(SummaryListContext.Provider, {
value: {
...summaryListContext,
isNested: true
}
}, React.createElement(Item, null, React.createElement(Dt, {
className: classnames('dnb-forms-value-block__label', (!label && !hasHelp || labelSrOnly) && 'dnb-sr-only', className)
}, React.createElement(VisibilityWrapper, null, label && React.createElement("strong", null, label), hasHelp && React.createElement("span", {
className: "dnb-help-button__word-joiner"
}, React.createElement(HelpButtonInline, {
contentId: `${id}-help`,
help: help
})))), React.createElement(Dd, {
className: classnames(compositionClass, maxWidth && `dnb-forms-value-block__content--max-width-${maxWidth}`)
}, React.createElement(VisibilityWrapper, null, !isCompositionInContextWithoutLabel ? getHelpContent(layout) : null, children ? React.createElement("span", {
className: defaultClass
}, children) : React.createElement("span", {
className: "dnb-forms-value-block__placeholder"
}, placeholder)))), isCompositionInContextWithoutLabel && hasHelp ? getHelpContent(layout, {
renderOnNextLine: true
}) : null);
}
} else {
content = React.createElement(React.Fragment, null, React.createElement(Span, _extends({
ref: ref,
className: classnames('dnb-forms-value-block', compositionClass, className, inline && 'dnb-forms-value-block--inline')
}, pickSpacingProps(props)), (label || hasHelp) && React.createElement(FormLabel, {
element: "strong",
className: 'dnb-forms-value-block__label',
labelDirection: inline ? 'horizontal' : 'vertical',
srOnly: labelSrOnly
}, label && React.createElement("span", {
className: "dnb-forms-value-block__label__content"
}, label), hasHelp && React.createElement("span", {
className: "dnb-help-button__word-joiner"
}, React.createElement(HelpButtonInline, {
contentId: `${id}-help`,
help: help
}))), !isCompositionInContextWithoutLabel ? getHelpContent(layout) : null, children ? React.createElement("span", {
className: defaultClass
}, children) : React.createElement("span", {
className: "dnb-forms-value-block__placeholder"
}, placeholder)), isCompositionInContextWithoutLabel && hasHelp ? getHelpContent(layout, {
renderOnNextLine: true
}) : null);
}
return React.createElement(ValueBlockContext.Provider, {
value: props
}, content);
}
function useNotInSummaryList(ref, label, path, itemPath) {
useEffect(() => {
if (ref !== null && ref !== void 0 && ref.current) {
try {
const sibling = ref.current.previousElementSibling;
if (sibling !== null && sibling !== void 0 && sibling.classList.contains('dnb-forms-value-block') && !ref.current.closest('.dnb-forms-summary-list')) {
warn.apply(warn, ['Value components as siblings should be wrapped inside a Value.SummaryList:', {
label,
path,
itemPath
}].filter(Boolean));
}
} catch (error) {}
}
}, [itemPath, label, path, ref]);
}
ValueBlock._supportsSpacingProps = true;
export default ValueBlock;
const transformLabelParameters = {
convertJsxToString
};
function VisibilityWrapper({
children
}) {
const visibilityContext = useContext(VisibilityContext);
if (visibilityContext) {
return React.createElement(Visibility, _extends({
element: "span"
}, visibilityContext.props), children);
}
return children;
}
export function isValueEmpty(value) {
return value === undefined || value === null || value === false;
}
//# sourceMappingURL=ValueBlock.js.map