@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
386 lines • 15.1 kB
JavaScript
import _pushInstanceProperty from "core-js-pure/stable/instance/push.js";
import _JSON$parse from "core-js-pure/stable/json/parse.js";
import React from 'react';
import PropTypes from 'prop-types';
import { isTrue, makeUniqueId, dispatchCustomElementEvent, convertJsxToString } from "../../shared/component-helper.js";
import { spacingPropTypes } from "../../components/space/SpacingHelper.js";
import Icon from "../../components/icon/Icon.js";
import CountryFlag from "../../components/country-flag/CountryFlag.js";
export const drawerListPropTypes = {
id: PropTypes.string,
role: PropTypes.string,
cache_hash: PropTypes.string,
triangle_position: PropTypes.string,
scrollable: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
focusable: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
direction: PropTypes.oneOf(['auto', 'top', 'bottom']),
size: PropTypes.oneOf(['default', 'small', 'medium', 'large']),
max_height: PropTypes.number,
no_animation: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
no_scroll_animation: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
prevent_selection: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
action_menu: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
is_popup: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
align_drawer: PropTypes.oneOf(['left', 'right']),
options_render: PropTypes.oneOfType([PropTypes.object, PropTypes.func, PropTypes.node]),
wrapper_element: PropTypes.oneOfType([PropTypes.object, PropTypes.func, PropTypes.node]),
default_value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
skip_portal: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
portal_class: PropTypes.string,
list_class: PropTypes.string,
prevent_close: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
independent_width: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
fixed_position: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
keep_open: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
prevent_focus: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
skip_keysearch: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
opened: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
data: PropTypes.oneOfType([PropTypes.oneOfType([PropTypes.string, PropTypes.func, PropTypes.node, PropTypes.object]), PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.oneOfType([PropTypes.string, PropTypes.node]), PropTypes.shape({
selectedKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
selected_key: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
selected_value: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
suffix_value: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
disabled: PropTypes.bool,
content: PropTypes.oneOfType([PropTypes.string, PropTypes.node, PropTypes.arrayOf(PropTypes.string)])
})]))]),
raw_data: PropTypes.oneOfType([PropTypes.array, PropTypes.object, PropTypes.func]),
ignore_events: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
...spacingPropTypes,
className: PropTypes.string,
children: PropTypes.oneOfType([PropTypes.string, PropTypes.func, PropTypes.node, PropTypes.object, PropTypes.array]),
on_show: PropTypes.func,
on_hide: PropTypes.func,
handle_dismiss_focus: PropTypes.func,
on_change: PropTypes.func,
on_pre_change: PropTypes.func,
on_resize: PropTypes.func,
on_select: PropTypes.func,
on_state_update: PropTypes.func
};
export const drawerListDefaultProps = {
id: null,
role: 'listbox',
cache_hash: null,
triangle_position: 'left',
scrollable: true,
focusable: false,
max_height: null,
direction: 'auto',
size: 'default',
no_animation: false,
no_scroll_animation: false,
prevent_selection: false,
action_menu: false,
is_popup: false,
align_drawer: 'left',
wrapper_element: null,
default_value: null,
value: 'initval',
portal_class: null,
list_class: null,
skip_portal: null,
prevent_close: false,
keep_open: false,
prevent_focus: false,
fixed_position: false,
independent_width: false,
skip_keysearch: false,
opened: null,
data: null,
raw_data: null,
ignore_events: null,
className: null,
children: null,
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,
options_render: null
};
export const drawerListProviderPropTypes = {
enable_body_lock: PropTypes.bool,
page_offset: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
observer_element: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
min_height: PropTypes.oneOfType([PropTypes.string, PropTypes.number])
};
export const drawerListProviderDefaultProps = {
enable_body_lock: false,
page_offset: null,
observer_element: null,
min_height: 10
};
export function parseContentTitle(dataItem, {
separator = '\n',
removeNumericOnlyValues = false,
preferSelectedValue = false
} = {}) {
dataItem = normalizeDataItem(dataItem);
if (!dataItem) {
return null;
}
let ret = '';
const onlyNumericRegex = /[0-9.,-\s]+/;
const hasValue = dataItem && dataItem.selected_value;
if (!(preferSelectedValue && hasValue) && Array.isArray(dataItem.content)) {
ret = dataItem.content.reduce((acc, cur) => {
const converted = convertJsxToString(cur, ' ');
if (!converted) {
return acc;
}
const found = removeNumericOnlyValues && converted.match(onlyNumericRegex);
if (!(found && found[0].length === converted.length)) {
_pushInstanceProperty(acc).call(acc, converted);
}
return acc;
}, []).join(separator);
} else {
ret = convertJsxToString(dataItem.content, ' ');
}
if (hasValue) {
if (preferSelectedValue) {
ret = String(convertJsxToString(dataItem.selected_value, separator, word => {
var _nestedChildren$props;
const nestedChildren = !word.props.children && (word === null || word === void 0 ? void 0 : word.type) !== Icon && (word === null || word === void 0 ? void 0 : word.type) !== CountryFlag && typeof (word === null || word === void 0 ? void 0 : word.type) === 'function' && word.type();
return nestedChildren !== null && nestedChildren !== void 0 && (_nestedChildren$props = nestedChildren.props) !== null && _nestedChildren$props !== void 0 && _nestedChildren$props.children ? nestedChildren : word;
}));
} else if (!onlyNumericRegex.test(dataItem.selected_value)) {
ret = separator + ret;
}
}
return ret;
}
export const hasObjectKeyAsValue = data => {
var _data;
data = ((_data = data) === null || _data === void 0 ? void 0 : _data.raw_data) || data;
return data && typeof data === 'object' && !Array.isArray(data);
};
export function preSelectData(data) {
if (typeof data === 'string') {
data = data[0] === '{' || data[0] === '[' ? _JSON$parse(data) : undefined;
} else if (data && React.isValidElement(data)) {
data = [];
} else if (typeof data === 'function') {
data = data();
}
return data;
}
export function normalizeData(props) {
var _preSelectData;
let data = (_preSelectData = preSelectData(props.data || props.children || props)) !== null && _preSelectData !== void 0 ? _preSelectData : [];
if (typeof data === 'object' && !Array.isArray(data)) {
const list = [];
for (const key in data) {
_pushInstanceProperty(list).call(list, {
selectedKey: key,
selected_key: key,
value: key,
content: data[key],
type: 'object'
});
}
data = list;
}
return data.map((dataItem, __id) => {
const normalized = normalizeDataItem(dataItem, true);
return typeof (normalized === null || normalized === void 0 ? void 0 : normalized.__id) !== 'undefined' ? normalized : {
...normalized,
__id
};
});
}
function normalizeDataItem(dataItem, markAsTransformed = false) {
return dataItem === null ? undefined : typeof dataItem === 'object' && 'content' in dataItem ? dataItem : {
content: dataItem,
...(markAsTransformed ? {
__isTransformed: true
} : {})
};
}
export const getData = props => {
return normalizeData(props);
};
export const getCurrentIndex = (value, data) => {
if (/[^0-9]/.test(String(value))) {
return data === null || data === void 0 ? void 0 : data.findIndex(cur => parseCurrentValue(cur) === value);
}
if (selectedKeyExists()) {
const index = data === null || data === void 0 ? void 0 : data.findIndex(cur => String(parseCurrentValue(cur)) === String(value));
if (index > -1) {
return index;
}
}
if (!isNaN(parseFloat(value))) {
return value;
}
return null;
function selectedKeyExists() {
for (let i = 0, l = data === null || data === void 0 ? void 0 : data.length; i < l; i++) {
var _data$i, _data$i2, _data$i3;
if (i > 10) {
return false;
}
if (typeof ((_data$i = data[i]) === null || _data$i === void 0 ? void 0 : _data$i.selectedKey) !== 'undefined' || typeof ((_data$i2 = data[i]) === null || _data$i2 === void 0 ? void 0 : _data$i2.selected_key) !== 'undefined' || ((_data$i3 = data[i]) === null || _data$i3 === void 0 ? void 0 : _data$i3.type) === 'object') {
return true;
}
}
}
};
export const getSelectedItemValue = (value, state) => {
if (hasObjectKeyAsValue(state)) {
return parseCurrentValue(state.data.filter((_, i) => i === parseFloat(value))[0]);
}
return value;
};
export const parseCurrentValue = current => {
if (typeof (current === null || current === void 0 ? void 0 : current.selectedKey) !== 'undefined') {
return current === null || current === void 0 ? void 0 : current.selectedKey;
}
if (typeof (current === null || current === void 0 ? void 0 : current.selected_key) !== 'undefined') {
return current === null || current === void 0 ? void 0 : current.selected_key;
}
if (typeof (current === null || current === void 0 ? void 0 : current.content) !== 'undefined') {
return current === null || current === void 0 ? void 0 : current.content;
}
return current;
};
export const getEventData = (item_index, data) => {
data = getCurrentData(item_index, data);
if (data && data.__id) {
data = {
...data
};
delete data.__id;
delete data.__isTransformed;
}
return data;
};
export const getCurrentData = (item_index, data) => {
if (typeof data === 'function') {
data = normalizeData(data);
}
data = data && data.find(({
__id
}) => __id === item_index) || null;
if (data && data.__isTransformed) {
return data.content;
}
return data;
};
function getFirstItemFromData(data) {
let firstItemIndex = data.length > 0 ? 0 : null;
let firstGroupIndex = -1;
data.forEach((item, index) => {
var _item$groupIndex;
if (((_item$groupIndex = item.groupIndex) !== null && _item$groupIndex !== void 0 ? _item$groupIndex : undefined) > -1) {
if (firstGroupIndex === -1 || item.groupIndex < firstGroupIndex) {
firstGroupIndex = item.groupIndex;
firstItemIndex = index;
}
if (item.groupIndex === 0) {
return;
}
}
});
return firstItemIndex;
}
export function prepareStartupState(props) {
const selected_item = null;
const raw_data = preSelectData(props.data || (!React.isValidElement(props.children) ? props.children : undefined));
const data = getData(props);
const opened = props.opened !== null ? isTrue(props.opened) : null;
const state = {
id: props.id || makeUniqueId(),
opened,
data,
original_data: data,
raw_data,
direction: props.direction,
max_height: props.max_height,
selected_item,
active_item: selected_item,
on_hide: props.on_hide,
on_show: props.on_show,
on_change: props.on_change,
on_select: props.on_select
};
if (props.value !== null && typeof props.value !== 'undefined' && props.value !== 'initval') {
state.selected_item = state.active_item = getCurrentIndex(props.value, data);
} else if (props.default_value !== null) {
state.selected_item = state.active_item = getCurrentIndex(props.default_value, data);
state._value = props.value;
}
return state;
}
export const prepareDerivedState = (props, state) => {
if (state.opened && !state.data && typeof props.data === 'function') {
state.data = getData(props);
}
if (props.data && props.data !== state._data) {
if (state._data) {
state.cache_hash = state.cache_hash + Date.now();
}
state.data = getData(props);
state.original_data = getData(props);
}
state.skipPortal = isTrue(props.skip_portal);
if (typeof props.wrapper_element === 'string') {
if (typeof document !== 'undefined') {
const wrapper_element = document.querySelector(props.wrapper_element);
if (wrapper_element) {
state.wrapper_element = wrapper_element;
}
}
} else if (props.wrapper_element) {
state.wrapper_element = props.wrapper_element;
}
if (state.selected_item !== props.value && (state._value !== props.value || isTrue(props.prevent_selection))) {
if (props.value === 'initval') {
state.selected_item = null;
} else {
state.selected_item = getCurrentIndex(props.value, state.original_data);
}
if (typeof props.on_state_update === 'function') {
dispatchCustomElementEvent({
props
}, 'on_state_update', {
selected_item: state.selected_item,
value: getSelectedItemValue(state.selected_item, state),
data: getEventData(state.selected_item, state.data)
});
}
}
if (!(state.active_item !== null && parseFloat(state.active_item) > -2) || state._value !== props.value) {
state.active_item = state.selected_item;
}
if (isNaN(parseFloat(state.active_item)) || parseFloat(state.active_item) === -1 || getCurrentData(parseFloat(state.active_item), state.data) === null) {
const firstItem = getFirstItemFromData(state.data);
state.ariaActiveDescendant = firstItem === null ? '' : `option-${state.id}-${firstItem}`;
state.active_item = -1;
} else {
state.ariaActiveDescendant = `option-${state.id}-${state.active_item}`;
}
if (props.direction !== 'auto' && props.direction !== state.direction) {
state.direction = props.direction;
}
if (state.selected_item !== null && parseFloat(state.selected_item) > -1) {
state.current_title = getCurrentDataTitle(state.selected_item, state.data);
}
state._data = props.data;
state._value = props.value;
state.groups = props.groups;
return state;
};
export const getCurrentDataTitle = (selected_item, data) => {
const currentData = getCurrentData(selected_item, data);
return parseContentTitle(currentData, {
separator: ' ',
preferSelectedValue: true
});
};
export const findClosest = (arr, val) => Math.max.apply(null, arr.filter(v => v <= val));
//# sourceMappingURL=DrawerListHelpers.js.map