UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

370 lines (369 loc) 13.5 kB
"use client"; import _defineProperty from "@babel/runtime-corejs3/helpers/esm/defineProperty"; import _extends from "@babel/runtime-corejs3/helpers/esm/extends"; import _pushInstanceProperty from "core-js-pure/stable/instance/push.js"; import React, { useContext } from 'react'; import classnames from 'classnames'; import { isTrue, extendPropsWithContextInClassComponent, validateDOMAttributes, keycode, warn } from "../../shared/component-helper.js"; import { getThemeClasses } from "../../shared/Theme.js"; import { createSpacingClasses } from "../../components/space/SpacingHelper.js"; import E from "../../elements/Element.js"; import DrawerListContext from "./DrawerListContext.js"; import DrawerListProvider from "./DrawerListProvider.js"; import DrawerListPortal from "./DrawerListPortal.js"; import { drawerListDefaultProps } from "./DrawerListHelpers.js"; import { DrawerListHorizontalItem, DrawerListItem } from "./DrawerListItem.js"; const propsToFilterOut = { on_show: null, on_hide: null, handle_dismiss_focus: null, on_change: null, on_pre_change: null, on_resize: null, on_select: null, on_state_update: null, on_key_down: null, options_render: null, wrapper_element: null }; function DrawerList(props) { const drawerListContext = useContext(DrawerListContext); if (drawerListContext !== null && drawerListContext !== void 0 && drawerListContext.drawerList) { return React.createElement(DrawerListInstance, props); } const { data, children, ...rest } = props; return React.createElement(DrawerListProvider, _extends({}, rest, { data: data || (!React.isValidElement(children) ? children : undefined) }), React.createElement(DrawerListInstance, props)); } DrawerList.blurDelay = DrawerListProvider.blurDelay; class DrawerListInstance extends React.Component { constructor(props) { super(props); _defineProperty(this, "preventTab", e => { switch (keycode(e)) { case 'tab': if (!this.context.drawerList.hasFocusOnElement) { e.preventDefault(); this.context.drawerList.setHidden(); } break; case 'page down': case 'page up': e.preventDefault(); break; } }); _defineProperty(this, "selectItemHandler", event => { const selected_item = parseFloat(event.currentTarget.getAttribute('data-item')); if (selected_item > -1) { this.context.drawerList.selectItemAndClose(selected_item, { fireSelectEvent: true, event }); } }); this.state = this.state || {}; } componentDidMount() { this.context.drawerList.setState(Object.entries(propsToFilterOut).reduce((acc, [key]) => { if (this.props[key]) { acc[key] = this.props[key]; } return acc; }, {})); this.context.drawerList.setState({ triangle_position: this.props.triangle_position }); } render() { var _renderData$, _this$context; const props = extendPropsWithContextInClassComponent(this.props, DrawerListInstance.defaultProps); const { role, align_drawer, fixed_position, independent_width, scrollable, focusable, size, no_animation, no_scroll_animation, prevent_selection, action_menu, is_popup, portal_class, list_class, ignore_events, options_render, className, cache_hash: _cache_hash, wrapper_element: _wrapper_element, triangle_position: _triangle_position, direction: _direction, max_height: _max_height, id: _id, data: _data, opened: _opened, value: _value, children, ...attributes } = props; function noNullNumbers({ selected_item, active_item, max_height, ...rest }) { return { selected_item: selected_item !== null && selected_item !== void 0 ? selected_item : undefined, active_item: active_item !== null && active_item !== void 0 ? active_item : undefined, max_height: max_height !== null && max_height !== void 0 ? max_height : undefined, ...rest }; } const { id, data, groups, opened, hidden, triangle_position, direction, max_height, cache_hash, selected_item, active_item, showFocusRing, closestToTop, closestToBottom, skipPortal, addObservers, removeObservers, _refShell, _refTriangle, _refUl, _refRoot } = noNullNumbers(this.context.drawerList); const renderData = makeRenderData(data, groups, this.context.getTranslation(this.props).DrawerList); const hasGroups = renderData.length > 1 || ((_renderData$ = renderData[0]) === null || _renderData$ === void 0 ? void 0 : _renderData$.groupTitle) !== undefined; const mainParams = { id: `${id}-drawer-list`, className: classnames(`dnb-drawer-list dnb-drawer-list--${direction}`, (isTrue(independent_width) || isTrue(action_menu)) && 'dnb-drawer-list--independent-width', createSpacingClasses(props), className, opened && 'dnb-drawer-list--opened', hidden && 'dnb-drawer-list--hidden', triangle_position && `dnb-drawer-list--triangle-position-${triangle_position}`, align_drawer && `dnb-drawer-list--${align_drawer}`, size && `dnb-drawer-list--${size}`, isTrue(action_menu) && `dnb-drawer-list--action-menu`, isTrue(is_popup) && 'dnb-drawer-list--is-popup', isTrue(scrollable) && 'dnb-drawer-list--scroll', isTrue(no_scroll_animation) && 'dnb-drawer-list--no-scroll-animation'), ...attributes }; const listParams = { id: `${id}-listbox`, className: classnames('dnb-drawer-list__list', list_class, isTrue(no_animation) && 'dnb-drawer-list__list--no-animation') }; const ulParams = { role, id: `${id}-ul`, 'aria-expanded': opened, 'aria-labelledby': `${id}-label`, tabIndex: -1, style: { maxHeight: parseFloat(max_height) > 0 ? `${max_height}rem` : null }, ref: _refUl }; if (!hidden) { ulParams['aria-activedescendant'] = this.context.drawerList.ariaActiveDescendant; } if (isTrue(focusable)) { ulParams.tabIndex = 0; } validateDOMAttributes(this.props, mainParams); validateDOMAttributes(null, listParams); validateDOMAttributes(null, ulParams); Object.assign(this.context.drawerList.attributes, validateDOMAttributes(null, attributes)); const ignoreEvents = isTrue(ignore_events); const GroupItems = () => renderData.filter(Boolean).map(({ groupTitle, groupData: data, hideTitle }, j) => { var _Items; const Items = () => data.map((dataItem, i) => { const { __id, ignore_events, class_name, disabled, style } = dataItem; const hash = `option-${id}-${__id}-${i}`; const tagId = `option-${id}-${__id}`; const liParams = { role: role === 'menu' ? 'menuitem' : 'option', 'data-item': __id, id: tagId, hash, className: classnames(ignoreEvents || ignore_events && 'ignore-events', class_name, i === 0 && 'first-of-type' + (j === 0 ? " first-item" : ""), i === data.length - 1 && 'last-of-type' + (j === renderData.length - 1 ? " last-item" : ""), tagId === closestToTop && 'closest-to-top', tagId === closestToBottom && 'closest-to-bottom'), active: __id === active_item, selected: !ignore_events && __id === selected_item, onClick: this.selectItemHandler, onKeyDown: this.preventTab, disabled: disabled, style: style }; if (ignoreEvents) { liParams.active = null; liParams.selected = null; liParams.onClick = null; liParams.onKeyDown = null; liParams.className = classnames(liParams.className, 'dnb-drawer-list__option--ignore'); } return React.createElement(DrawerList.Item, _extends({ key: hash }, liParams), dataItem); }); const ItemsRendered = () => typeof options_render === 'function' ? options_render({ data, Items, Item: DrawerList.Item }) : _Items || (_Items = React.createElement(Items, null)); if (hasGroups) { const groupdId = `${id}-group-title-${j}`; return React.createElement("ul", { key: j, role: "group", "aria-labelledby": groupdId, className: classnames('dnb-drawer-list__group', j === 0 && 'first-of-type', j === renderData.length - 1 && 'last-of-type') }, React.createElement("li", { id: groupdId, role: "presentation", className: classnames('dnb-drawer-list__group-title', hideTitle && 'dnb-sr-only', groupdId === closestToBottom && 'closest-to-bottom', groupdId === closestToTop && 'closest-to-top') }, groupTitle), React.createElement(ItemsRendered, null)); } else { return React.createElement(ItemsRendered, { key: j }); } }); const mainList = React.createElement("span", _extends({}, mainParams, { ref: _refShell }), React.createElement("span", listParams, hidden === false && renderData.length > 0 ? React.createElement(React.Fragment, null, React.createElement(DrawerList.Options, _extends({ hasGroups: hasGroups, cache_hash: cache_hash + active_item + selected_item + closestToTop + closestToBottom + direction + max_height }, ulParams, { showFocusRing: showFocusRing, triangleRef: _refTriangle }), React.createElement(GroupItems, null)), React.createElement(OnMounted, { addObservers: addObservers, removeObservers: removeObservers })) : React.isValidElement(children) && React.createElement("span", { className: "dnb-drawer-list__content" }, children, React.createElement("span", { className: "dnb-drawer-list__triangle", ref: _refTriangle })))); return React.createElement("span", { className: 'dnb-drawer-list__root' + (!skipPortal ? " dnb-drawer-list__root--portal" : ""), ref: _refRoot }, React.createElement(DrawerListPortal, { id: id, rootRef: _refRoot, opened: hidden === false, include_owner_width: align_drawer === 'right', independent_width: isTrue(independent_width), fixed_position: isTrue(fixed_position), className: getThemeClasses((_this$context = this.context) === null || _this$context === void 0 ? void 0 : _this$context.theme, portal_class), skipPortal: skipPortal }, mainList)); } } _defineProperty(DrawerListInstance, "defaultProps", { ...drawerListDefaultProps }); _defineProperty(DrawerListInstance, "contextType", DrawerListContext); function makeRenderData(data, groups, translation) { const renderData = []; const noIndex = []; if (Array.isArray(data) && data.length > 0) { data.forEach(dataItem => { var _dataItem$groupIndex; const index = (_dataItem$groupIndex = dataItem.groupIndex) !== null && _dataItem$groupIndex !== void 0 ? _dataItem$groupIndex : undefined; if (index >= 0) { var _context; if (!renderData[index]) { let groupTitle = groups === null || groups === void 0 ? void 0 : groups[index]; let hideTitle = false; if (!groupTitle) { if (index === 0) { groupTitle = translation.defaultGroupSR; hideTitle = true; } else { warn(`Missing group title for groupIndex: ${index}`); groupTitle = `${translation.missingGroup} ${index + 1}`; } } renderData[index] = { groupTitle, hideTitle, groupData: [] }; } _pushInstanceProperty(_context = renderData[index].groupData).call(_context, dataItem); } else { _pushInstanceProperty(noIndex).call(noIndex, dataItem); } }); } if (noIndex.length > 0) { _pushInstanceProperty(renderData).call(renderData, { groupTitle: renderData.length > 0 ? translation.noGroupSR : undefined, hideTitle: true, groupData: noIndex }); } return renderData; } DrawerList.Options = React.memo(React.forwardRef((props, ref) => { const { children, className, triangleRef, cache_hash, showFocusRing = false, hasGroups = false, ...rest } = props; return React.createElement(E, _extends({ internalClass: false, as: hasGroups ? 'span' : 'ul', className: classnames('dnb-drawer-list__options', className, showFocusRing && 'dnb-drawer-list__options--focusring') }, rest, { ref: ref }), children, React.createElement(E, { internalClass: false, as: hasGroups ? 'span' : 'li', className: "dnb-drawer-list__triangle", "aria-hidden": true, ref: triangleRef })); }), (prevProps, nextProps) => { if (!prevProps.cache_hash) { return false; } return prevProps.cache_hash === nextProps.cache_hash; }); DrawerList.Item = DrawerListItem; DrawerList.HorizontalItem = DrawerListHorizontalItem; class OnMounted extends React.PureComponent { componentDidMount() { this.props.addObservers(); } componentWillUnmount() { this.props.removeObservers(); } render() { return null; } } export default DrawerList; //# sourceMappingURL=DrawerList.js.map