@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
403 lines • 16.3 kB
JavaScript
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _extends from "@babel/runtime/helpers/esm/extends";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
const _excluded = ["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", "wrapper_element", "triangle_position", "direction", "max_height", "id", "data", "prepared_data", "raw_data", "opened", "value", "children"],
_excluded2 = ["children", "className", "triangleRef", "cache_hash", "showFocusRing"],
_excluded3 = ["role", "hash", "children", "className", "on_click", "selected", "active", "value", "disabled"],
_excluded4 = ["children", "className"],
_excluded5 = ["className"];
import "core-js/modules/web.dom-collections.iterator.js";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import React from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { isTrue, makeUniqueId, extendPropsWithContextInClassComponent, validateDOMAttributes, dispatchCustomElementEvent, keycode } from '../../shared/component-helper';
import { getThemeClasses } from '../../shared/Theme';
import { createSpacingClasses } from '../../components/space/SpacingHelper';
import DrawerListContext from './DrawerListContext';
import DrawerListProvider from './DrawerListProvider';
import DrawerListPortal from './DrawerListPortal';
import { drawerListPropTypes, drawerListDefaultProps } from './DrawerListHelpers';
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
};
export default class DrawerList extends React.PureComponent {
constructor(props) {
super(props);
this._id = props.id || makeUniqueId();
}
render() {
var _this$context;
const hasProvider = (_this$context = this.context) === null || _this$context === void 0 ? void 0 : _this$context.drawerList;
if (hasProvider) {
return React.createElement(DrawerListInstance, this.props);
}
return React.createElement(DrawerListProvider, _extends({
id: this._id
}, this.props, {
data: this.props.data || this.props.children
}), React.createElement(DrawerListInstance, _extends({
id: this._id
}, this.props)));
}
}
_defineProperty(DrawerList, "contextType", DrawerListContext);
_defineProperty(DrawerList, "blurDelay", DrawerListProvider.blurDelay);
_defineProperty(DrawerList, "defaultProps", _objectSpread({}, drawerListDefaultProps));
process.env.NODE_ENV !== "production" ? DrawerList.propTypes = _objectSpread({}, drawerListPropTypes) : void 0;
class DrawerListInstance extends React.PureComponent {
constructor(props, context) {
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._id = props.id || makeUniqueId();
this.state = this.state || {};
context.drawerList.setState(Object.entries(propsToFilterOut).reduce((acc, _ref) => {
let [key] = _ref;
if (props[key]) {
acc[key] = props[key];
}
return acc;
}, {}));
context.drawerList.setState({
triangle_position: props.triangle_position
});
}
render() {
var _this$context2;
const props = extendPropsWithContextInClassComponent(this.props, DrawerList.defaultProps, this.context.getTranslation(this.props).DrawerList);
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,
prepared_data: _prepared_data,
raw_data: _raw_data,
opened: _opened,
value: _value,
children
} = props,
attributes = _objectWithoutProperties(props, _excluded);
const id = this._id;
const {
data,
opened,
hidden,
triangle_position,
direction,
max_height,
cache_hash,
selected_item,
active_item,
showFocusRing,
closestToTop,
closestToBottom,
usePortal,
addObservers,
removeObservers,
_refShell,
_refTriangle,
_refUl,
_refRoot
} = this.context.drawerList;
const mainParams = _objectSpread({
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: max_height > 0 ? `${max_height}rem` : null
},
ref: _refUl
};
if (!hidden && (parseFloat(active_item) > -1 || !(parseFloat(active_item) > -1) && !(parseFloat(selected_item) > -1))) {
ulParams['aria-activedescendant'] = `option-${id}-${parseFloat(active_item) > -1 ? active_item : 0}`;
} else if (!isTrue(prevent_selection) && !hidden && parseFloat(selected_item) > -1) {
ulParams['aria-activedescendant'] = `option-${id}-${selected_item}`;
}
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 Items = () => data.map((dataItem, i) => {
const _id = dataItem.__id;
const hash = `option-${id}-${_id}-${i}`;
const liParams = {
role: role === 'menu' ? 'menuitem' : 'option',
'data-item': _id,
id: `option-${id}-${_id}`,
hash,
className: classnames(ignoreEvents || dataItem.ignore_events && 'ignore-events', dataItem.class_name, i === closestToTop && 'closest-to-top', i === closestToBottom && 'closest-to-bottom', i === 0 && 'first-of-type', i === data.length - 1 && 'last-of-type'),
active: _id == active_item,
selected: !dataItem.ignore_events && _id == selected_item,
onClick: this.selectItemHandler,
onKeyDown: this.preventTab,
disabled: dataItem.disabled,
style: dataItem.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 mainList = React.createElement("span", _extends({}, mainParams, {
ref: _refShell
}), React.createElement("span", listParams, hidden === false && data && data.length > 0 ? React.createElement(React.Fragment, null, React.createElement(DrawerList.Options, _extends({
cache_hash: cache_hash + active_item + selected_item + closestToTop + closestToBottom + direction + max_height
}, ulParams, {
showFocusRing: showFocusRing,
triangleRef: _refTriangle
}), typeof options_render === 'function' ? options_render({
data,
Items,
Item: DrawerList.Item
}) : React.createElement(Items, null)), React.createElement(OnMounted, {
addObservers: addObservers,
removeObservers: removeObservers
})) : 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' + (usePortal ? " dnb-drawer-list__root--portal" : ""),
ref: _refRoot
}, usePortal ? React.createElement(DrawerListPortal, {
id: this._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$context2 = this.context) === null || _this$context2 === void 0 ? void 0 : _this$context2.theme, portal_class)
}, mainList) : mainList);
}
}
_defineProperty(DrawerListInstance, "defaultProps", DrawerList.defaultProps);
_defineProperty(DrawerListInstance, "contextType", DrawerListContext);
process.env.NODE_ENV !== "production" ? DrawerListInstance.propTypes = DrawerList.propTypes : void 0;
DrawerList.Options = React.memo(React.forwardRef((props, ref) => {
const {
children,
className = null,
triangleRef = null,
cache_hash = null,
showFocusRing = false
} = props,
rest = _objectWithoutProperties(props, _excluded2);
return React.createElement("ul", _extends({
className: classnames('dnb-drawer-list__options', className, showFocusRing && 'dnb-drawer-list__options--focusring')
}, rest, {
ref: ref
}), children, React.createElement("li", {
className: "dnb-drawer-list__triangle",
"aria-hidden": true,
ref: triangleRef
}));
}), (prevProps, nextProps) => {
if (!prevProps.cache_hash) {
return null;
}
return prevProps.cache_hash === nextProps.cache_hash;
});
DrawerList.Options.displayName = 'DrawerList.Options';
DrawerList.Options.propTypes = {
children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]).isRequired,
cache_hash: PropTypes.string,
showFocusRing: PropTypes.bool,
className: PropTypes.string,
triangleRef: PropTypes.object
};
DrawerList.Item = React.forwardRef((props, ref) => {
const {
role = 'option',
hash = '',
children,
className = null,
on_click = null,
selected,
active = null,
value = null,
disabled
} = props,
rest = _objectWithoutProperties(props, _excluded3);
const params = {
className: classnames(className, 'dnb-drawer-list__option', selected && 'dnb-drawer-list__option--selected', active && 'dnb-drawer-list__option--focus'),
role,
tabIndex: selected ? '0' : '-1',
'aria-selected': active,
disabled,
'aria-disabled': disabled
};
if (selected) {
params['aria-current'] = true;
}
if (on_click) {
params.onClick = () => dispatchCustomElementEvent({
props: _objectSpread(_objectSpread({}, props), {}, {
displayName: DrawerList.Item.displayName
})
}, 'on_click', _objectSpread({
selected,
value
}, rest));
}
return React.createElement("li", _extends({}, params, rest, {
ref: ref,
key: 'li' + hash
}), React.createElement("span", {
className: "dnb-drawer-list__option__inner"
}, React.createElement(ItemContent, {
hash: hash
}, children)));
});
DrawerList.Item.displayName = 'DrawerList.Item';
DrawerList.Item.propTypes = {
role: PropTypes.string,
hash: PropTypes.string,
children: PropTypes.oneOfType([PropTypes.node, PropTypes.func, PropTypes.object]).isRequired,
className: PropTypes.string,
on_click: PropTypes.func,
selected: PropTypes.bool,
active: PropTypes.bool,
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
disabled: PropTypes.bool
};
export function ItemContent(_ref2) {
let {
hash = '',
children = undefined
} = _ref2;
let content = null;
if (Array.isArray(children.content || children)) {
content = (children.content || children).map((item, n) => React.createElement(DrawerListOptionItem, {
key: hash + n,
className: `item-nr-${n + 1}`
}, children.render ? children.render(item, hash + n) : item));
} else if (Object.prototype.hasOwnProperty.call(children, 'content')) {
content = children.render ? children.render(children.content, hash, children) : children.content;
if (content) {
content = React.createElement(DrawerListOptionItem, null, content);
}
} else {
content = children && React.createElement(DrawerListOptionItem, null, children);
}
return Object.prototype.hasOwnProperty.call(children, 'suffix_value') ? React.createElement(React.Fragment, null, content, React.createElement(DrawerListOptionItem, {
className: "dnb-drawer-list__option__suffix"
}, children.suffix_value)) : content;
}
process.env.NODE_ENV !== "production" ? ItemContent.propTypes = {
hash: PropTypes.string,
children: PropTypes.oneOfType([PropTypes.node, PropTypes.object])
} : void 0;
function DrawerListOptionItem(_ref3) {
let {
children = undefined,
className = null
} = _ref3,
props = _objectWithoutProperties(_ref3, _excluded4);
return React.createElement("span", _extends({
className: classnames('dnb-drawer-list__option__item', className)
}, props), children);
}
DrawerList.HorizontalItem = _ref4 => {
let {
className = null
} = _ref4,
props = _objectWithoutProperties(_ref4, _excluded5);
return React.createElement(DrawerListOptionItem, _extends({
className: classnames('dnb-drawer-list__option__item--horizontal', className)
}, props));
};
DrawerList.HorizontalItem.propTypes = {
children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]).isRequired,
className: PropTypes.string
};
class OnMounted extends React.PureComponent {
componentDidMount() {
this.props.addObservers();
}
componentWillUnmount() {
this.props.removeObservers();
}
render() {
return null;
}
}
process.env.NODE_ENV !== "production" ? OnMounted.propTypes = {
addObservers: PropTypes.func.isRequired,
removeObservers: PropTypes.func.isRequired
} : void 0;
//# sourceMappingURL=DrawerList.js.map