@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
120 lines • 4.46 kB
JavaScript
import React, { Fragment } from 'react';
import classnames from 'classnames';
import Space from "../space/Space.js";
import { createSpacingClasses, createSpacingProperties, isValidSpaceProp, removeSpaceProps } from "../space/SpacingUtils.js";
export const omitSpacingProps = removeSpaceProps;
export function pickSpacingProps(props) {
const obj = {};
for (const key in props) {
if (isValidSpaceProp(key) && typeof props[key] !== 'undefined') {
obj[key] = props[key];
}
}
return obj;
}
export function getSpaceValue(type, element) {
var _element$props$type, _element$props, _element$props2;
if (!React.isValidElement(element)) {
return;
}
return (_element$props$type = (_element$props = element.props) === null || _element$props === void 0 ? void 0 : _element$props[type]) !== null && _element$props$type !== void 0 ? _element$props$type : typeof ((_element$props2 = element.props) === null || _element$props2 === void 0 ? void 0 : _element$props2.space) === 'object' ? element.props.space[type] : undefined;
}
export function isHeadingElement(element) {
var _element$type;
return React.isValidElement(element) && (element === null || element === void 0 || (_element$type = element.type) === null || _element$type === void 0 ? void 0 : _element$type['_isHeadingElement']) === true;
}
export function getSpaceVariant(element) {
if (React.isValidElement(element)) {
var _element$type2, _element$props3;
if ((element === null || element === void 0 ? void 0 : element.type) === Fragment) {
return 'children';
}
const check = element === null || element === void 0 || (_element$type2 = element.type) === null || _element$type2 === void 0 ? void 0 : _element$type2['_supportsSpacingProps'];
if (typeof check !== 'undefined') {
return check;
}
const keys = ['space', 'top', 'right', 'bottom', 'left'];
const props = (_element$props3 = element === null || element === void 0 ? void 0 : element.props) !== null && _element$props3 !== void 0 ? _element$props3 : {};
if (keys.some(key => key in props)) {
return true;
}
}
return undefined;
}
export function renderWithSpacing(element, spaceProps) {
const variant = getSpaceVariant(element);
const {
wrapInSpace = true
} = spaceProps;
if (variant === false) {
return element;
}
if (variant === 'children') {
return React.Children.toArray(element).map(child => {
var _child$props;
const children = child === null || child === void 0 || (_child$props = child.props) === null || _child$props === void 0 ? void 0 : _child$props.children;
return React.Children.toArray(children).map((element, i) => {
return React.cloneElement(child, {
key: i,
...(child === null || child === void 0 ? void 0 : child.props)
}, wrapWithSpace({
element,
spaceProps,
wrapInSpace
}));
});
});
}
return wrapWithSpace({
element,
spaceProps,
variant,
wrapInSpace
});
}
function isIntrinsicElement(element) {
return React.isValidElement(element) && typeof element.type === 'string';
}
function wrapWithSpace({
element,
spaceProps,
variant = null,
wrapInSpace = true
}) {
const resolvedVariant = variant !== null && variant !== void 0 ? variant : getSpaceVariant(element);
const {
wrapInSpace: _,
...props
} = spaceProps;
if (resolvedVariant === true) {
return React.cloneElement(element, props);
}
if (resolvedVariant === 'children') {
return renderWithSpacing(element, spaceProps);
}
if (!wrapInSpace && isIntrinsicElement(element)) {
return cloneIntrinsicElementWithSpacing(element, spaceProps);
}
return React.createElement(Space, props, element);
}
function cloneIntrinsicElementWithSpacing(element, {
wrapInSpace,
className,
style,
...spaceProps
}) {
var _element$props4, _element$props5;
if (!React.isValidElement(element)) {
return element;
}
return React.cloneElement(element, {
key: spaceProps.key,
className: classnames((_element$props4 = element.props) === null || _element$props4 === void 0 ? void 0 : _element$props4.className, ...createSpacingClasses(spaceProps), className),
style: {
...((_element$props5 = element.props) === null || _element$props5 === void 0 ? void 0 : _element$props5.style),
...createSpacingProperties(spaceProps),
...style
}
});
}
//# sourceMappingURL=utils.js.map