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

feat: Avatar support size from ConfigProvider #44288

Merged
merged 4 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/avatar/AvatarContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ export interface AvatarContextType {
shape?: 'circle' | 'square';
}

const AvatarContext = React.createContext<AvatarContextType>({ size: 'default', shape: undefined });
const AvatarContext = React.createContext<AvatarContextType>({ size: undefined, shape: undefined });
li-jia-nan marked this conversation as resolved.
Show resolved Hide resolved

export default AvatarContext;
16 changes: 16 additions & 0 deletions components/avatar/__tests__/Avatar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import mountTest from '../../../tests/shared/mountTest';
import rtlTest from '../../../tests/shared/rtlTest';
import { fireEvent, render } from '../../../tests/utils';
import useBreakpoint from '../../grid/hooks/useBreakpoint';
import ConfigProvider from '../../config-provider';

jest.mock('../../grid/hooks/useBreakpoint');

Expand Down Expand Up @@ -205,4 +206,19 @@ describe('Avatar Render', () => {
expect(avatars?.[2]).toHaveClass('ant-avatar-square');
expect(avatars?.[3]).toHaveClass('ant-avatar-circle');
});

it('should apply the componentSize of CP', () => {
const { container } = render(
<>
<ConfigProvider componentSize="small">
<Avatar>test</Avatar>
</ConfigProvider>
<ConfigProvider componentSize="large">
<Avatar>test</Avatar>
</ConfigProvider>
</>,
);
expect(container.querySelector('.ant-avatar-sm')).toBeTruthy();
expect(container.querySelector('.ant-avatar-lg')).toBeTruthy();
});
});
5 changes: 3 additions & 2 deletions components/avatar/avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import useBreakpoint from '../grid/hooks/useBreakpoint';
import type { AvatarContextType, AvatarSize } from './AvatarContext';
import AvatarContext from './AvatarContext';
import useStyle from './style';
import useSize from '../config-provider/hooks/useSize';

export interface AvatarProps {
/** Shape of avatar, options: `circle`, `square` */
Expand Down Expand Up @@ -93,7 +94,7 @@ const InternalAvatar: React.ForwardRefRenderFunction<HTMLSpanElement, AvatarProp
const {
prefixCls: customizePrefixCls,
shape,
size: customSize = 'default',
size: customSize,
src,
srcSet,
icon,
Expand All @@ -106,7 +107,7 @@ const InternalAvatar: React.ForwardRefRenderFunction<HTMLSpanElement, AvatarProp
...others
} = props;

const size = customSize === 'default' ? avatarCtx?.size : customSize;
const size = useSize((ctxSize) => customSize || avatarCtx?.size || ctxSize || 'default');
li-jia-nan marked this conversation as resolved.
Show resolved Hide resolved

const needResponsive = Object.keys(typeof size === 'object' ? size || {} : {}).some((key) =>
['xs', 'sm', 'md', 'lg', 'xl', 'xxl'].includes(key),
Expand Down