@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
348 lines • 11.5 kB
JavaScript
var _em, _em2;
import React, { useMemo, useCallback } from 'react';
import clsx from 'clsx';
import { convertJsxToString, makeUniqueId } from "../../../../shared/component-helper.js";
import { ToggleButton, Dropdown, Radio, Autocomplete, HelpButton } from "../../../../components/index.js";
import OptionField from "../Option/index.js";
import { useFieldProps } from "../../hooks/index.js";
import { checkForError } from "../../hooks/useFieldProps.js";
import { pickSpacingProps } from "../../../../components/flex/utils.js";
import FieldBlock from "../../FieldBlock/index.js";
import useDataValue from "../../hooks/useDataValue.js";
import withComponentMarkers from "../../../../shared/helpers/withComponentMarkers.js";
import { jsx as _jsx } from "react/jsx-runtime";
function Selection(props) {
var _props$autocompletePr;
const clearValue = useMemo(() => `clear-option-${makeUniqueId()}`, []);
const {
id,
className,
variant = 'dropdown',
layout = 'vertical',
optionsLayout = 'vertical',
placeholder,
value,
info,
warning,
error,
hasError,
disabled,
size,
emptyValue,
width,
htmlAttributes,
setHasFocus,
handleChange,
setDisplayValue,
transformSelection,
data,
groups,
dataPath,
children,
additionalArgs,
autocompleteProps,
dropdownProps
} = useFieldProps(props);
const {
getValueByPath
} = useDataValue();
let dataList = data;
if (dataPath) {
dataList = getValueByPath(dataPath);
}
const hasRenderPropChildren = typeof children === 'function';
const renderedChildren = useMemo(() => {
return resolveChildren(children, value, dataList);
}, [children, dataList, value]);
const handleDrawerListChange = useCallback(({
data,
value
}) => {
const selectedKey = typeof data === 'object' && data ? data.selectedKey : value;
handleChange === null || handleChange === void 0 || handleChange(!selectedKey || selectedKey === clearValue ? emptyValue : selectedKey, {
data
});
}, [handleChange, emptyValue, clearValue]);
const onChangeHandler = useCallback(({
value
}) => {
handleChange === null || handleChange === void 0 || handleChange(value === undefined ? emptyValue : value);
}, [handleChange, emptyValue]);
const handleShow = useCallback(({
data
}) => {
setHasFocus(true, typeof data === 'object' && data ? data.selectedKey : undefined);
}, [setHasFocus]);
const handleHide = useCallback(({
data
}) => {
setHasFocus(false, typeof data === 'object' && data ? data.selectedKey : undefined);
}, [setHasFocus]);
const cn = clsx(`dnb-forms-field-selection dnb-forms-field-selection__variant--${variant} dnb-forms-field-selection--layout-${layout} dnb-forms-field-selection--options-layout--${optionsLayout}`, className);
const fieldBlockProps = {
forId: id,
className: cn,
disableStatusSummary: true,
...pickSpacingProps(props)
};
const onType = props === null || props === void 0 || (_props$autocompletePr = props.autocompleteProps) === null || _props$autocompletePr === void 0 ? void 0 : _props$autocompletePr.onType;
const onTypeAutocompleteHandler = useCallback(event => {
if (typeof onType === 'function') {
const {
value
} = event;
onType({
...event,
...additionalArgs,
value: value === '' ? emptyValue : value
});
}
}, [additionalArgs, emptyValue, onType]);
switch (variant) {
case 'radio':
case 'button':
{
const Component = variant === 'radio' ? Radio : ToggleButton;
const items = renderRadioItems({
id,
value,
variant,
info,
warning,
htmlAttributes,
children: renderedChildren,
dataList: hasRenderPropChildren ? undefined : dataList,
hasError,
iterateOverItems: ({
value: v,
label
}) => {
if (v === value) {
setDisplayValue(label);
}
}
});
const additionalFieldBlockProps = {
asFieldset: hasRenderPropChildren || React.Children.count(items) > 1,
fieldsetRole: variant === 'radio' ? 'radiogroup' : 'group'
};
if (!size) {
additionalFieldBlockProps.labelHeight = 'small';
}
if (width) {
additionalFieldBlockProps.contentWidth = width;
}
return _jsx(FieldBlock, {
...fieldBlockProps,
...additionalFieldBlockProps,
children: _jsx(Component.Group, {
size: size,
className: cn,
layoutDirection: optionsLayout === 'horizontal' ? 'row' : 'column',
disabled: disabled,
onChange: onChangeHandler,
value: String(value !== null && value !== void 0 ? value : ''),
children: items
})
});
}
case 'autocomplete':
case 'dropdown':
{
var _data$find;
const data = renderDropdownItems(hasRenderPropChildren ? undefined : dataList, transformSelection).concat(makeOptions(renderedChildren, transformSelection)).filter(Boolean);
const displayValue = (_data$find = data.find(item => item.selectedKey === value)) === null || _data$find === void 0 ? void 0 : _data$find.content;
setDisplayValue(displayValue);
const sharedProps = {
id,
listClass: 'dnb-forms-field-selection__list',
portalClass: 'dnb-forms-field-selection__portal',
title: placeholder,
value: String(value !== null && value !== void 0 ? value : ''),
status: hasError || checkForError([error, info, warning]) ? 'error' : undefined,
disabled,
...htmlAttributes,
data,
groups,
size,
onChange: handleDrawerListChange,
onOpen: handleShow,
onClose: handleHide,
stretch: true
};
const specificFieldBlockProps = {
contentWidth: width !== null && width !== void 0 ? width : 'large'
};
return _jsx(FieldBlock, {
...fieldBlockProps,
...specificFieldBlockProps,
children: variant === 'autocomplete' ? _jsx(Autocomplete, {
...sharedProps,
...autocompleteProps,
value: autocompleteProps !== null && autocompleteProps !== void 0 && autocompleteProps.preventSelection ? undefined : value,
onType: onTypeAutocompleteHandler,
data: !props.data && !props.dataPath && (autocompleteProps === null || autocompleteProps === void 0 ? void 0 : autocompleteProps.mode) === 'async' ? undefined : data,
selectAll: true
}) : _jsx(Dropdown, {
...sharedProps,
...dropdownProps
})
});
}
}
}
function resolveChildren(children, value, options) {
if (typeof children === 'function') {
return children({
value,
options
});
}
return children;
}
function renderRadioItems({
id,
value: valueProp,
variant,
info,
warning,
htmlAttributes,
children,
dataList,
hasError,
iterateOverItems
}) {
const optionsCount = countOptions(children) + ((dataList === null || dataList === void 0 ? void 0 : dataList.length) || 0);
const createOption = (props, i) => {
const {
value,
title,
children,
error,
help,
size,
...rest
} = props;
const label = title !== null && title !== void 0 ? title : children;
const suffix = help ? _jsx(HelpButton, {
size: "small",
title: convertJsxToString(help.title),
children: help.content
}) : undefined;
iterateOverItems === null || iterateOverItems === void 0 || iterateOverItems({
value,
label
});
const Component = variant === 'radio' ? Radio : ToggleButton;
return _jsx(Component, {
id: optionsCount === 1 ? id : undefined,
label: variant === 'radio' ? label : undefined,
text: variant === 'button' ? label : undefined,
role: "radio",
value: String(value !== null && value !== void 0 ? value : valueProp) || undefined,
status: hasError || checkForError([error, info, warning]) ? 'error' : undefined,
suffix: suffix,
size: size,
...htmlAttributes,
...rest
}, `option-${i}-${id}`);
};
return [...(dataList || []).map((props, i) => {
return createOption(props, i);
}), ...(mapOptions(children, {
createOption
}) || [])].filter(Boolean);
}
export function countOptions(children) {
let count = 0;
React.Children.forEach(children, child => {
if (React.isValidElement(child)) {
if (child.type === OptionField) {
count++;
} else if (child.props.children) {
count += countOptions(child.props.children);
}
}
});
return count;
}
export function mapOptions(children, {
createOption
}) {
return React.Children.map(children, (child, i) => {
if (React.isValidElement(child)) {
if (child.type === OptionField) {
return createOption(child.props, i);
}
if (child.props.children) {
const nestedChildren = mapOptions(child.props.children, {
createOption
});
return React.createElement(child.type, child.props, nestedChildren);
}
}
return child;
});
}
export function makeOptions(children, transformSelection) {
return React.Children.map(children, child => {
var _child;
if (((_child = child) === null || _child === void 0 || (_child = _child['props']) === null || _child === void 0 || (_child = _child.children) === null || _child === void 0 ? void 0 : _child.type) === OptionField) {
child = child['props'].children;
}
if (React.isValidElement(child) && child.type === OptionField) {
var _ref, _props$title, _props$value;
const props = child.props;
const title = (_ref = (_props$title = props.title) !== null && _props$title !== void 0 ? _props$title : props.children) !== null && _ref !== void 0 ? _ref : _em || (_em = _jsx("em", {
children: "Untitled"
}));
const content = props.text ? [title, props.text] : title;
const selectedValue = transformSelection ? transformSelection(props) : undefined;
const selectedKey = String((_props$value = props.value) !== null && _props$value !== void 0 ? _props$value : '');
const disabled = props.disabled;
const style = props.style;
const groupIndex = props.groupIndex;
return {
selectedKey,
selectedValue,
content,
disabled,
style,
groupIndex
};
}
if (child) {
return {
content: child
};
}
return undefined;
});
}
function renderDropdownItems(data, transformSelection) {
return (data === null || data === void 0 ? void 0 : data.map(props => {
const {
value,
title,
text,
disabled,
style,
...rest
} = props;
return {
selectedKey: value,
content: (text ? [title, text] : title) || _em2 || (_em2 = _jsx("em", {
children: "Untitled"
})),
selectedValue: transformSelection ? transformSelection(props) : undefined,
disabled,
style,
...rest
};
})) || [];
}
withComponentMarkers(Selection, {
_supportsSpacingProps: true
});
export default Selection;
//# sourceMappingURL=Selection.js.map