UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

355 lines (354 loc) 11.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var _exportNames = { validateDOMAttributes: true, isObject: true, extendDeep: true, dispatchCustomElementEvent: true, toPascalCase: true, toKebabCase: true, toCapitalized: true, makeUniqueId: true, slugify: true, isChildOfElement: true, roundToNearest: true, getClosestScrollViewElement: true, convertJsxToString: true, getStatusState: true, combineLabelledBy: true, combineDescribedBy: true, findElementInChildren: true, escapeRegexChars: true, removeUndefinedProps: true, warn: true, getClosestParent: true, InteractionInvalidation: true, extendPropsWithContext: true, extendExistingPropsWithContext: true, assignPropsWithContext: true, filterProps: true }; Object.defineProperty(exports, "InteractionInvalidation", { enumerable: true, get: function () { return _InteractionInvalidation.InteractionInvalidation; } }); Object.defineProperty(exports, "assignPropsWithContext", { enumerable: true, get: function () { return _assignPropsWithContext.assignPropsWithContext; } }); exports.combineDescribedBy = combineDescribedBy; exports.combineLabelledBy = combineLabelledBy; exports.convertJsxToString = convertJsxToString; exports.dispatchCustomElementEvent = void 0; exports.escapeRegexChars = escapeRegexChars; exports.extendDeep = extendDeep; Object.defineProperty(exports, "extendExistingPropsWithContext", { enumerable: true, get: function () { return _extendPropsWithContext.extendExistingPropsWithContext; } }); Object.defineProperty(exports, "extendPropsWithContext", { enumerable: true, get: function () { return _extendPropsWithContext.extendPropsWithContext; } }); Object.defineProperty(exports, "filterProps", { enumerable: true, get: function () { return _filterProps.filterProps; } }); exports.findElementInChildren = findElementInChildren; Object.defineProperty(exports, "getClosestParent", { enumerable: true, get: function () { return _getClosest.getClosestParent; } }); exports.getClosestScrollViewElement = void 0; exports.getStatusState = getStatusState; exports.isChildOfElement = void 0; exports.isObject = isObject; exports.makeUniqueId = void 0; exports.removeUndefinedProps = removeUndefinedProps; exports.slugify = exports.roundToNearest = void 0; exports.toCapitalized = toCapitalized; exports.validateDOMAttributes = exports.toPascalCase = exports.toKebabCase = void 0; Object.defineProperty(exports, "warn", { enumerable: true, get: function () { return _helpers.warn; } }); var _hasOwn = _interopRequireDefault(require("core-js-pure/stable/object/has-own.js")); var _react = _interopRequireDefault(require("react")); var _whatInput = _interopRequireDefault(require("./helpers/whatInput.js")); var _helpers = require("./helpers.js"); var _getClosest = require("./helpers/getClosest.js"); var _Eufemia = require("./Eufemia.js"); var _componentHelperLegacy = require("./legacy/component-helper-legacy.js"); Object.keys(_componentHelperLegacy).forEach(function (key) { if (key === "default" || key === "__esModule") return; if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return; if (key in exports && exports[key] === _componentHelperLegacy[key]) return; Object.defineProperty(exports, key, { enumerable: true, get: function () { return _componentHelperLegacy[key]; } }); }); var _InteractionInvalidation = require("./helpers/InteractionInvalidation.js"); var _extendPropsWithContext = require("./helpers/extendPropsWithContext.js"); var _assignPropsWithContext = require("./helpers/assignPropsWithContext.js"); var _filterProps = require("./helpers/filterProps.js"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } (0, _Eufemia.init)(); _whatInput.default.specificKeys([9]); (0, _componentHelperLegacy.defineNavigator)(); const startsWithCamelCaseRegex = /(^[a-z]{1,}[A-Z]{1})/; const notOnlyAZOrHyphenRegex = /[^a-z-]/i; const validateDOMAttributes = (props, params) => { if (props && props.attributes) { const attr = props.attributes; if (attr && typeof attr === 'object') { Object.entries(attr).forEach(([key, value]) => { if (key === '__proto__' || key === 'constructor' || key === 'prototype') { return; } Object.assign(params, { [key]: value }); }); } delete params.attributes; } if (params.disabled === null) { delete params.disabled; } if (typeof params.space !== 'undefined') { delete params.space; } if (typeof params.top !== 'undefined') { delete params.top; } if (typeof params.right !== 'undefined') { delete params.right; } if (typeof params.bottom !== 'undefined') { delete params.bottom; } if (typeof params.left !== 'undefined') { delete params.left; } if (typeof params.noCollapse !== 'undefined') { delete params.noCollapse; } if (typeof params.innerSpace !== 'undefined') { delete params.innerSpace; } if (typeof params.labelDirection !== 'undefined') { delete params.labelDirection; } if (params.disabled === true) { params['aria-disabled'] = true; } if (params && typeof params === 'object') { for (const i in params) { if (typeof params[i] === 'function' && i !== 'ref' && !startsWithCamelCaseRegex.test(i)) { delete params[i]; } else if (params[i] === null || notOnlyAZOrHyphenRegex.test(i)) { delete params[i]; } } } return params; }; exports.validateDOMAttributes = validateDOMAttributes; function isObject(item) { return item && typeof item === 'object' && !Array.isArray(item); } function extendDeep(target = {}, ...sources) { for (const source of sources) { if (isObject(source)) { for (const key in source) { if (key === '__proto__' || key === 'constructor') continue; if (!(0, _hasOwn.default)(source, key)) continue; if (!isObject(target)) continue; if (isObject(source[key])) { if (!isObject(target[key])) { target[key] = {}; } extendDeep(target[key], source[key]); } else { target[key] = source[key]; } } } } return target; } const dispatchCustomElementEvent = (src, eventName, eventObjectOrig = undefined) => { let ret = undefined; const eventObject = { ...(eventObjectOrig && eventObjectOrig.event || {}), ...eventObjectOrig }; const props = src && src.props || src; if (typeof props[eventName] === 'function') { const r = props[eventName].apply(src, [eventObject]); if (typeof r !== 'undefined') { ret = r; } } return ret; }; exports.dispatchCustomElementEvent = dispatchCustomElementEvent; const toPascalCase = s => s.split(/_/g).reduce((acc, cur) => acc + cur.replace(/(\w)(\w*)/g, (g0, g1, g2) => g1.toUpperCase() + g2.toLowerCase()), ''); exports.toPascalCase = toPascalCase; const toKebabCase = str => str.replace(/\B[A-Z]/g, letter => `-${letter}`).toLowerCase(); exports.toKebabCase = toKebabCase; function toCapitalized(str) { return typeof str === 'string' ? str.toLowerCase().split('').map((char, index, arr) => index === 0 || arr[index - 1] === ' ' || arr[index - 1] === '-' ? char.toUpperCase() : char).join('') : str; } const makeUniqueId = (prefix = 'id-', length = 8) => prefix + String(Math.random().toString(36).substring(2, 2 + length) + idIncrement++).slice(-length); exports.makeUniqueId = makeUniqueId; let idIncrement = 0; const slugify = s => String(s).toLowerCase().replace(/[^\w\s-]/g, '').replace(/[\s_-]+/g, '-').replace(/^-+|-+$/g, ''); exports.slugify = slugify; const isChildOfElement = (element, target, callback = null) => { try { const contains = element => { if (callback) { const res = callback(element); if (res) { return element; } } return element && element === target; }; if (contains(element)) { return element; } while ((element = element && element.parentElement) && !contains(element)); } catch (e) {} return element; }; exports.isChildOfElement = isChildOfElement; const roundToNearest = (num, target) => { const diff = num % target; return diff > target / 2 ? num - diff + target : num - diff; }; exports.roundToNearest = roundToNearest; const getClosestScrollViewElement = currentElement => { return (0, _getClosest.getClosestParent)('.dnb-scroll-view', currentElement); }; exports.getClosestScrollViewElement = getClosestScrollViewElement; function convertJsxToString(elements, separator = undefined, transformWord = undefined) { if (!Array.isArray(elements)) { elements = [elements]; } const process = word => { if (_react.default.isValidElement(word)) { let element = word; if (transformWord) { element = transformWord(element); } if (Array.isArray(element.props.children)) { word = element.props.children.reduce((acc, word) => { if (typeof word !== 'string') { word = process(word); } if (typeof word === 'string') { acc = (acc + (separator || '') + word).trim(); } return acc; }, ''); } else if (element.props.children) { word = element.props.children; if (typeof word !== 'string') { word = process(word); } if (typeof word === 'string') { word = word.trim(); } else { return undefined; } } else { return undefined; } } return word; }; return Array.from(elements).map(word => process(word)).filter(Boolean).join(separator).trim(); } function getStatusState(status) { return status && status !== 'error' && status !== 'warning' && status !== 'information'; } function combineLabelledBy(...params) { return combineAriaBy('aria-labelledby', params); } function combineDescribedBy(...params) { return combineAriaBy('aria-describedby', params); } function combineAriaBy(type, params) { params = params.map(cur => { if (Array.isArray(cur)) { return cur.join(' '); } if (cur && params.includes(cur[type])) { return null; } if (cur && typeof cur[type] !== 'undefined') { cur = cur[type]; } if (typeof cur !== 'string') { cur = null; } return cur; }); params = params.filter(Boolean).join(' '); if (params === '') { params = undefined; } return params; } function findElementInChildren(children, find) { if (!Array.isArray(children)) { children = [children]; } let result = null; children.some(cur => { if (cur && cur.props && cur.props.children) { const res = findElementInChildren(cur.props.children, find); if (res) { return result = res; } } if (_react.default.isValidElement(cur) && find(cur)) { return result = cur; } return null; }); return result; } function escapeRegexChars(str) { return str.replace(/[-[\]{}()*+?.,\\^$|#]/g, '\\$&'); } function removeUndefinedProps(object) { Object.keys(object || {}).forEach(key => { if (object[key] === undefined) { delete object[key]; } }); return object; } //# sourceMappingURL=component-helper.js.map