mirror of
https://github.com/ant-design/ant-design.git
synced 2025-06-08 01:53:34 +08:00
parent
bd1a6c44d4
commit
9772a63b4c
20
components/avatar/SizeContext.tsx
Normal file
20
components/avatar/SizeContext.tsx
Normal 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;
|
@ -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') {
|
||||||
|
@ -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>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user