@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
225 lines (224 loc) • 9.15 kB
JavaScript
"use client";
import React, { Fragment, useCallback, useContext, useEffect, useMemo, useRef } from 'react';
import clsx from 'clsx';
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";
import withComponentMarkers from "../../../shared/helpers/withComponentMarkers.js";
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
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 && _jsx(HelpButtonInlineContent, {
element: "span",
contentId: `${id}-help`,
className: "dnb-forms-value-block__help",
help: help,
breakout: breakout,
outset: breakout
});
return hasHelp && renderOnNextLine ? _jsx("span", {
className: "dnb-forms-value-block__help--next-line",
children: 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 ? _jsx("span", {
className: defaultClass,
children: children
}) : _jsx("span", {
className: "dnb-forms-value-block__placeholder",
children: placeholder
});
} else {
const defaultLayout = isCompositionInContext && label ? 'horizontal' : 'vertical';
const {
layout = defaultLayout
} = summaryListContext;
content = _jsxs(SummaryListContext, {
value: {
...summaryListContext,
isNested: true
},
children: [_jsxs(Item, {
children: [_jsx(Dt, {
className: clsx('dnb-forms-value-block__label', (!label && !hasHelp || labelSrOnly) && 'dnb-sr-only', className),
children: _jsxs(VisibilityWrapper, {
children: [label && _jsx("strong", {
children: label
}), hasHelp && _jsx("span", {
className: "dnb-help-button__word-joiner",
children: _jsx(HelpButtonInline, {
contentId: `${id}-help`,
help: help
})
})]
})
}), _jsx(Dd, {
className: clsx(compositionClass, maxWidth && `dnb-forms-value-block__content--max-width-${maxWidth}`),
children: _jsxs(VisibilityWrapper, {
children: [!isCompositionInContextWithoutLabel ? getHelpContent(layout) : null, children ? _jsx("span", {
className: defaultClass,
children: children
}) : _jsx("span", {
className: "dnb-forms-value-block__placeholder",
children: placeholder
})]
})
})]
}), isCompositionInContextWithoutLabel && hasHelp ? getHelpContent(layout, {
renderOnNextLine: true
}) : null]
});
}
} else {
content = _jsxs(_Fragment, {
children: [_jsxs(Span, {
ref: ref,
className: clsx('dnb-forms-value-block', compositionClass, className, inline && 'dnb-forms-value-block--inline'),
...pickSpacingProps(props),
children: [(label || hasHelp) && _jsxs(FormLabel, {
element: "strong",
className: 'dnb-forms-value-block__label',
labelDirection: inline ? 'horizontal' : 'vertical',
srOnly: labelSrOnly,
children: [label && _jsx("span", {
className: "dnb-forms-value-block__label__content",
children: label
}), hasHelp && _jsx("span", {
className: "dnb-help-button__word-joiner",
children: _jsx(HelpButtonInline, {
contentId: `${id}-help`,
help: help
})
})]
}), !isCompositionInContextWithoutLabel ? getHelpContent(layout) : null, children ? _jsx("span", {
className: defaultClass,
children: children
}) : _jsx("span", {
className: "dnb-forms-value-block__placeholder",
children: placeholder
})]
}), isCompositionInContextWithoutLabel && hasHelp ? getHelpContent(layout, {
renderOnNextLine: true
}) : null]
});
}
return _jsx(ValueBlockContext, {
value: props,
children: 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]);
}
withComponentMarkers(ValueBlock, {
_supportsSpacingProps: true
});
export default ValueBlock;
const transformLabelParameters = {
convertJsxToString
};
function VisibilityWrapper({
children
}) {
const visibilityContext = useContext(VisibilityContext);
if (visibilityContext) {
return _jsx(Visibility, {
element: "span",
...visibilityContext.props,
children: children
});
}
return children;
}
export function isValueEmpty(value) {
return value === undefined || value === null || value === false;
}
//# sourceMappingURL=ValueBlock.js.map