如何将工具提示添加到 AvatarGroup“+x”气泡中

2024-02-12

我想知道是否有一种方法可以将 ToolTip 组件添加到气泡中,以显示 Avatar Group 组件中的其他头像。 以下是组件如何呈现的示例: (图片来自 Material UI 文档 (https://material-ui.com/components/avatars/#grouped https://material-ui.com/components/avatars/#grouped)

我想在最后一个气泡中添加一个工具提示,显示剩余用户的名称。

我目前正在为每个头像添加一个工具提示。这是我的代码

<AvatarGroup max={4}>
     {users.people.map((person, index) => (
          <Tooltip title={person.name} key={index}>
               <Avatar
                    className={classes.smallAvatar}
                    onClick={e => {
                         handleUserClick(e),
                         handleAvatarClick(person);
                    }}
                    src={person.avatar}
               >
                    {person.initials}
               </Avatar>
          </Tooltip>
     ))}
</AvatarGroup>

AFAIK 没有这样的功能,但您可以创建自己的 AvatarGroup 组件(并在将来支持它)。

使用示例:

import React from "react";
import Avatar from "@material-ui/core/Avatar";
import AvatarGroup from "./AvatarGroup"; // Custom AvatarGroup

export default function GroupAvatars() {
  return (
    <AvatarGroup
      max={4}
      extraAvatarsTooltipTitle="Extra Avatar Tooltip"
    >
      <Avatar alt="Remy Sharp" src="/static/images/avatar/1.jpg" />
      <Avatar alt="Travis Howard" src="/static/images/avatar/2.jpg" />
      <Avatar alt="Cindy Baker" src="/static/images/avatar/3.jpg" />
      <Avatar alt="Agnes Walker" src="/static/images/avatar/4.jpg" />
      <Avatar alt="Trevor Henderson" src="/static/images/avatar/5.jpg" />
    </AvatarGroup>
  );
}

以及自定义 AvatarGroup 的代码:

import * as React from 'react';
import PropTypes from 'prop-types';
import { isFragment } from 'react-is';
import clsx from 'clsx';
import { withStyles } from '@material-ui/core/styles';
import Avatar from '@material-ui/core/Avatar';
import Tooltip from '@material-ui/core/Tooltip';
import { chainPropTypes } from '@material-ui/utils';

const SPACINGS = {
  small: -16,
  medium: null,
};

export const styles = (theme) => ({
  /* Styles applied to the root element. */
  root: {
    display: 'flex',
  },
  /* Styles applied to the avatar elements. */
  avatar: {
    border: `2px solid ${theme.palette.background.default}`,
    marginLeft: -8,
    '&:first-child': {
      marginLeft: 0,
    },
  },
});

const AvatarGroup = React.forwardRef(function AvatarGroup(props, ref) {
  const {
    children: childrenProp,
    classes,
    className,
    max = 5,
    spacing = 'medium',
    ...other
  } = props;
  const clampedMax = max < 2 ? 2 : max;

  const children = React.Children.toArray(childrenProp).filter((child) => {
    if (process.env.NODE_ENV !== 'production') {
      if (isFragment(child)) {
        console.error(
          [
            "Material-UI: The AvatarGroup component doesn't accept a Fragment as a child.",
            'Consider providing an array instead.',
          ].join('\n'),
        );
      }
    }

    return React.isValidElement(child);
  });

  const extraAvatars = children.length > clampedMax ? children.length - clampedMax + 1 : 0;

  const marginLeft = spacing && SPACINGS[spacing] !== undefined ? SPACINGS[spacing] : -spacing;

  return (
    <div className={clsx(classes.root, className)} ref={ref} {...other}>
      {children.slice(0, children.length - extraAvatars).map((child, index) => {
        return React.cloneElement(child, {
          className: clsx(child.props.className, classes.avatar),
          style: {
            zIndex: children.length - index,
            marginLeft: index === 0 ? undefined : marginLeft,
            ...child.props.style,
          },
        });
      })}
      {extraAvatars ? (
        <Tooltip
          title={props.extraAvatarsTooltipTitle}
        >
        <Avatar
          className={classes.avatar}
          style={{
            zIndex: 0,
            marginLeft,
          }}
        >
          +{extraAvatars}
        </Avatar>
        </Tooltip>
      ) : null}
    </div>
  );
});

AvatarGroup.propTypes = {
  // ----------------------------- Warning --------------------------------
  // | These PropTypes are generated from the TypeScript type definitions |
  // |     To update them edit the d.ts file and run "yarn proptypes"     |
  // ----------------------------------------------------------------------
  /**
   * The avatars to stack.
   */
  children: PropTypes.node,
  /**
   * Override or extend the styles applied to the component.
   * See [CSS API](#css) below for more details.
   */
  classes: PropTypes.object,
  /**
   * @ignore
   */
  className: PropTypes.string,
  /**
   * Max avatars to show before +x.
   */
  max: chainPropTypes(PropTypes.number, (props) => {
    if (props.max < 2) {
      throw new Error(
        [
          'Material-UI: The prop `max` should be equal to 2 or above.',
          'A value below is clamped to 2.',
        ].join('\n'),
      );
    }
  }),
  /**
   * Spacing between avatars.
   */
  spacing: PropTypes.oneOfType([PropTypes.oneOf(['medium', 'small']), PropTypes.number]),
};

export default withStyles(styles, { name: 'MuiAvatarGroup' })(AvatarGroup);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将工具提示添加到 AvatarGroup“+x”气泡中 的相关文章

随机推荐