UNPKG

zmp-react

Version:

Build full featured iOS & Android apps using ZMP & React

73 lines (70 loc) 2.61 kB
import _extends from "@babel/runtime/helpers/extends"; import React, { useRef, forwardRef, useImperativeHandle } from 'react'; import { classNames } from '../shared/utils'; import { DEFAULT_AVATAR_GROUP_COUNTER } from '../../common/constants'; import Avatar from './avatar'; /* dts-props id: string | number; className: string; style: React.CSSProperties; maxCounter? : number; horizontal?: boolean; onCounterClick?: (event?: any) => void ref?: React.MutableRefObject<{el: HTMLElement | null}>; CHILDREN_PROP */ var AvatarGroup = /*#__PURE__*/forwardRef(function (_ref, ref) { var id = _ref.id, children = _ref.children, horizontalProps = _ref.horizontal, className = _ref.className, style = _ref.style, _ref$maxCounter = _ref.maxCounter, maxCounterProps = _ref$maxCounter === void 0 ? DEFAULT_AVATAR_GROUP_COUNTER : _ref$maxCounter, onCounterClick = _ref.onCounterClick; var elRef = useRef(null); useImperativeHandle(ref, function () { return { el: elRef.current }; }); var handleOnCounterClick = function handleOnCounterClick(e) { if (typeof onCounterClick === 'function') { onCounterClick(e); } }; var maxCounter = maxCounterProps < DEFAULT_AVATAR_GROUP_COUNTER ? DEFAULT_AVATAR_GROUP_COUNTER : maxCounterProps; var horizontal = maxCounter > DEFAULT_AVATAR_GROUP_COUNTER ? true : horizontalProps; var childrenComponents = children ? [].concat(children) : []; childrenComponents = childrenComponents.filter(function (child) { return child && child.type && (child.type.displayName === 'zmp-avatar' || child.type.name === 'zmp-avatar'); }); var total = childrenComponents.length; var dataToDisplay = childrenComponents.slice(0, maxCounter).map(function (child) { return child.props; }); var classes = classNames('avatar-group', { 'avatar-group-horizontal': horizontal }, className); return /*#__PURE__*/React.createElement("div", { id: id, ref: elRef, className: classes, style: style }, dataToDisplay.map(function (avatarProps, index) { return /*#__PURE__*/React.createElement(Avatar, _extends({ className: "avatar-group-item", key: avatarProps.id || "avatar-" + index }, avatarProps, { story: false, online: false, size: 24 })); }), total > maxCounter && /*#__PURE__*/React.createElement(Avatar, { onClick: handleOnCounterClick, className: "avatar-group-item avatar-max-counter", size: 24 }, "+", total - maxCounter)); }); AvatarGroup.displayName = 'zmp-avatar-group'; export default AvatarGroup;