Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Avatar group size #27531

Merged
merged 2 commits into from
Nov 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions components/avatar/SizeContext.tsx
Original file line number Diff line number Diff line change
@@ -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;
}
hengkx marked this conversation as resolved.
Show resolved Hide resolved

export const SizeContextProvider: React.FC<SizeContextProps> = ({ children, size }) => (
<SizeContext.Consumer>
{originSize => (
<SizeContext.Provider value={size || originSize}>{children}</SizeContext.Provider>
)}
</SizeContext.Consumer>
);

export default SizeContext;
11 changes: 7 additions & 4 deletions components/avatar/avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import ResizeObserver from 'rc-resize-observer';
import { ConfigContext } from '../config-provider';
import devWarning from '../_util/devWarning';
import { composeRef } from '../_util/ref';
import { Breakpoint, responsiveArray, ScreenSizeMap } from '../_util/responsiveObserve';
import { Breakpoint, responsiveArray } from '../_util/responsiveObserve';
import useBreakpoint from '../grid/hooks/useBreakpoint';

export type AvatarSize = 'large' | 'small' | 'default' | number | ScreenSizeMap;
import SizeContext, { AvatarSize } from './SizeContext';

export interface AvatarProps {
/** Shape of avatar, options:`circle`, `square` */
Expand Down Expand Up @@ -37,6 +36,8 @@ export interface AvatarProps {
}

const InternalAvatar: React.ForwardRefRenderFunction<unknown, AvatarProps> = (props, ref) => {
const groupSize = React.useContext(SizeContext);

const [scale, setScale] = React.useState(1);
const [mounted, setMounted] = React.useState(false);
const [isImgExist, setIsImgExist] = React.useState(true);
Expand Down Expand Up @@ -87,7 +88,7 @@ const InternalAvatar: React.ForwardRefRenderFunction<unknown, AvatarProps> = (pr
const {
prefixCls: customizePrefixCls,
shape,
size,
size: customSize,
src,
srcSet,
icon,
Expand All @@ -98,6 +99,8 @@ const InternalAvatar: React.ForwardRefRenderFunction<unknown, AvatarProps> = (pr
...others
} = props;

const size = customSize === 'default' ? groupSize : customSize;

const screens = useBreakpoint();
const responsiveSizeStyle: React.CSSProperties = React.useMemo(() => {
if (typeof size !== 'object') {
Expand Down
23 changes: 14 additions & 9 deletions components/avatar/group.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import classNames from 'classnames';
import toArray from 'rc-util/lib/Children/toArray';
import { cloneElement } from '../_util/reactNode';
import { ConfigContext } from '../config-provider';
import Avatar, { AvatarSize } from './avatar';
import Avatar from './avatar';
import Popover from '../popover';
import { AvatarSize, SizeContextProvider } from './SizeContext';

export interface GroupProps {
className?: string;
Expand Down Expand Up @@ -38,7 +39,6 @@ const Group: React.FC<GroupProps> = props => {
const { children, maxPopoverPlacement = 'top' } = props;
const childrenWithProps = toArray(children).map((child, index) => {
return cloneElement(child, {
size,
key: `avatar-key-${index}`,
});
});
Expand All @@ -55,19 +55,24 @@ const Group: React.FC<GroupProps> = props => {
placement={maxPopoverPlacement}
overlayClassName={`${prefixCls}-popover`}
>
<Avatar style={maxStyle} size={size}>{`+${numOfChildren - maxCount}`}</Avatar>
<Avatar style={maxStyle}>{`+${numOfChildren - maxCount}`}</Avatar>
</Popover>,
);
return (
<div className={cls} style={props.style}>
{childrenShow}
</div>
<SizeContextProvider size={size}>
<div className={cls} style={props.style}>
{childrenShow}
</div>
</SizeContextProvider>
);
}

return (
<div className={cls} style={props.style}>
{childrenWithProps}
</div>
<SizeContextProvider size={size}>
<div className={cls} style={props.style}>
{childrenWithProps}
</div>
</SizeContextProvider>
);
};

Expand Down