@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
99 lines (98 loc) • 5.08 kB
JavaScript
"use client";
import _extends from "@babel/runtime/helpers/esm/extends";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
const _excluded = ["label", "className", "children", "size", "maxElements", "variant", "backgroundColor", "color"],
_excluded2 = ["skeleton"];
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 classnames from 'classnames';
import { createSpacingClasses } from '../space/SpacingHelper';
import { validateDOMAttributes, extendPropsWithContext } from '../../shared/component-helper';
import Context from '../../shared/Context';
export const defaultProps = {
label: null,
className: null,
maxElements: 4,
size: 'medium',
children: null,
variant: 'primary',
skeleton: false
};
export const AvatarGroupContext = React.createContext(null);
const AvatarGroup = localProps => {
const context = React.useContext(Context);
const _extendPropsWithConte = extendPropsWithContext(localProps, defaultProps, context === null || context === void 0 ? void 0 : context.AvatarGroup, {
skeleton: context === null || context === void 0 ? void 0 : context.skeleton
}),
{
label,
className,
children: childrenProp,
size,
maxElements: maxElementsProp,
variant,
backgroundColor,
color
} = _extendPropsWithConte,
props = _objectWithoutProperties(_extendPropsWithConte, _excluded);
const maxElements = maxElementsProp && maxElementsProp > 0 ? maxElementsProp : 4;
let children = childrenProp;
let numOfHiddenAvatars = 0;
if (Array.isArray(childrenProp)) {
const total = childrenProp.length;
if (total > maxElements) {
numOfHiddenAvatars = total - maxElements + 1;
}
children = childrenProp.slice(0, total - numOfHiddenAvatars).map((child, i) => {
var _child$props$size, _child$props, _child$props$variant, _child$props2, _child$props$color, _child$props3, _child$props$backgrou, _child$props4;
const appliedSize = (_child$props$size = (_child$props = child.props) === null || _child$props === void 0 ? void 0 : _child$props.size) !== null && _child$props$size !== void 0 ? _child$props$size : size;
const appliedVariant = (_child$props$variant = (_child$props2 = child.props) === null || _child$props2 === void 0 ? void 0 : _child$props2.variant) !== null && _child$props$variant !== void 0 ? _child$props$variant : variant;
const appliedColor = (_child$props$color = (_child$props3 = child.props) === null || _child$props3 === void 0 ? void 0 : _child$props3.color) !== null && _child$props$color !== void 0 ? _child$props$color : color;
const appliedBackgroundColor = (_child$props$backgrou = (_child$props4 = child.props) === null || _child$props4 === void 0 ? void 0 : _child$props4.backgroundColor) !== null && _child$props$backgrou !== void 0 ? _child$props$backgrou : backgroundColor;
return React.cloneElement(child, {
size: appliedSize,
variant: appliedVariant,
color: appliedColor,
backgroundColor: appliedBackgroundColor,
style: _objectSpread(_objectSpread({}, child.props.style), {}, {
zIndex: total - i
}),
key: i
});
});
}
const spacingClasses = createSpacingClasses(props);
const _validateDOMAttribute = validateDOMAttributes({}, props),
{
skeleton
} = _validateDOMAttribute,
attributes = _objectWithoutProperties(_validateDOMAttribute, _excluded2);
return React.createElement(AvatarGroupContext.Provider, {
value: _objectSpread(_objectSpread({}, props), {}, {
variant,
size,
color,
backgroundColor
})
}, React.createElement("span", _extends({
className: classnames('dnb-avatar__group', spacingClasses, className)
}, attributes), React.createElement("span", {
className: "dnb-sr-only"
}, label), children, numOfHiddenAvatars ? React.createElement(ElementsHidden, {
size: size
}, "+", numOfHiddenAvatars) : null));
};
function ElementsHidden(props) {
const {
size,
children
} = props;
return React.createElement("span", {
className: `dnb-avatar__group--elements-left dnb-avatar__group--elements-left--size-${size || 'medium'}`
}, children);
}
AvatarGroup._supportsSpacingProps = true;
export default AvatarGroup;
//# sourceMappingURL=AvatarGroup.js.map