fix: Avatar group size (#27531)

* fix: Avatar group size

* fix
This commit is contained in:
Tom Xu 2020-11-05 16:22:21 +08:00 committed by GitHub
parent bd1a6c44d4
commit 9772a63b4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 13 deletions

View File

@ -0,0 +1,20 @@
import * as React from 'react';
import { ScreenSizeMap } from '../_util/responsiveObserve';
export type AvatarSize = 'large' | 'small' | 'default' | number | ScreenSizeMap;
const SizeContext = React.createContext<AvatarSize>('default');
export interface SizeContextProps {
size?: AvatarSize;
}
export const SizeContextProvider: React.FC<SizeContextProps> = ({ children, size }) => (
<SizeContext.Consumer>
{originSize => (
<SizeContext.Provider value={size || originSize}>{children}</SizeContext.Provider>
)}
</SizeContext.Consumer>
);
export default SizeContext;

View File

@ -5,10 +5,9 @@ import ResizeObserver from 'rc-resize-observer';
import { ConfigContext } from '../config-provider'; import { ConfigContext } from '../config-provider';
import devWarning from '../_util/devWarning'; import devWarning from '../_util/devWarning';
import { composeRef } from '../_util/ref'; import { composeRef } from '../_util/ref';
import { Breakpoint, responsiveArray, ScreenSizeMap } from '../_util/responsiveObserve'; import { Breakpoint, responsiveArray } from '../_util/responsiveObserve';
import useBreakpoint from '../grid/hooks/useBreakpoint'; import useBreakpoint from '../grid/hooks/useBreakpoint';
import SizeContext, { AvatarSize } from './SizeContext';
export type AvatarSize = 'large' | 'small' | 'default' | number | ScreenSizeMap;
export interface AvatarProps { export interface AvatarProps {
/** Shape of avatar, options:`circle`, `square` */ /** Shape of avatar, options:`circle`, `square` */
@ -37,6 +36,8 @@ export interface AvatarProps {
} }
const InternalAvatar: React.ForwardRefRenderFunction<unknown, AvatarProps> = (props, ref) => { const InternalAvatar: React.ForwardRefRenderFunction<unknown, AvatarProps> = (props, ref) => {
const groupSize = React.useContext(SizeContext);
const [scale, setScale] = React.useState(1); const [scale, setScale] = React.useState(1);
const [mounted, setMounted] = React.useState(false); const [mounted, setMounted] = React.useState(false);
const [isImgExist, setIsImgExist] = React.useState(true); const [isImgExist, setIsImgExist] = React.useState(true);
@ -87,7 +88,7 @@ const InternalAvatar: React.ForwardRefRenderFunction<unknown, AvatarProps> = (pr
const { const {
prefixCls: customizePrefixCls, prefixCls: customizePrefixCls,
shape, shape,
size, size: customSize,
src, src,
srcSet, srcSet,
icon, icon,
@ -98,6 +99,8 @@ const InternalAvatar: React.ForwardRefRenderFunction<unknown, AvatarProps> = (pr
...others ...others
} = props; } = props;
const size = customSize === 'default' ? groupSize : customSize;
const screens = useBreakpoint(); const screens = useBreakpoint();
const responsiveSizeStyle: React.CSSProperties = React.useMemo(() => { const responsiveSizeStyle: React.CSSProperties = React.useMemo(() => {
if (typeof size !== 'object') { if (typeof size !== 'object') {

View File

@ -3,8 +3,9 @@ import classNames from 'classnames';
import toArray from 'rc-util/lib/Children/toArray'; import toArray from 'rc-util/lib/Children/toArray';
import { cloneElement } from '../_util/reactNode'; import { cloneElement } from '../_util/reactNode';
import { ConfigContext } from '../config-provider'; import { ConfigContext } from '../config-provider';
import Avatar, { AvatarSize } from './avatar'; import Avatar from './avatar';
import Popover from '../popover'; import Popover from '../popover';
import { AvatarSize, SizeContextProvider } from './SizeContext';
export interface GroupProps { export interface GroupProps {
className?: string; className?: string;
@ -38,7 +39,6 @@ const Group: React.FC<GroupProps> = props => {
const { children, maxPopoverPlacement = 'top' } = props; const { children, maxPopoverPlacement = 'top' } = props;
const childrenWithProps = toArray(children).map((child, index) => { const childrenWithProps = toArray(children).map((child, index) => {
return cloneElement(child, { return cloneElement(child, {
size,
key: `avatar-key-${index}`, key: `avatar-key-${index}`,
}); });
}); });
@ -55,19 +55,24 @@ const Group: React.FC<GroupProps> = props => {
placement={maxPopoverPlacement} placement={maxPopoverPlacement}
overlayClassName={`${prefixCls}-popover`} overlayClassName={`${prefixCls}-popover`}
> >
<Avatar style={maxStyle} size={size}>{`+${numOfChildren - maxCount}`}</Avatar> <Avatar style={maxStyle}>{`+${numOfChildren - maxCount}`}</Avatar>
</Popover>, </Popover>,
); );
return ( return (
<div className={cls} style={props.style}> <SizeContextProvider size={size}>
{childrenShow} <div className={cls} style={props.style}>
</div> {childrenShow}
</div>
</SizeContextProvider>
); );
} }
return ( return (
<div className={cls} style={props.style}> <SizeContextProvider size={size}>
{childrenWithProps} <div className={cls} style={props.style}>
</div> {childrenWithProps}
</div>
</SizeContextProvider>
); );
}; };