Skip to content

Commit

Permalink
feat: Avatar support size from ConfigProvider (#44288)
Browse files Browse the repository at this point in the history
* feat: Avatar support size from ConfigProvider

* Update components/avatar/avatar.tsx

Co-authored-by: MadCcc <1075746765@qq.com>
Signed-off-by: lijianan <574980606@qq.com>

* test: update snap

* Update components/avatar/AvatarContext.ts

Co-authored-by: MadCcc <1075746765@qq.com>
Signed-off-by: lijianan <574980606@qq.com>

---------

Signed-off-by: lijianan <574980606@qq.com>
Co-authored-by: MadCcc <1075746765@qq.com>
  • Loading branch information
li-jia-nan and MadCcc committed Aug 18, 2023
1 parent 74d9654 commit d375cbd
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
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>({});

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');

const needResponsive = Object.keys(typeof size === 'object' ? size || {} : {}).some((key) =>
['xs', 'sm', 'md', 'lg', 'xl', 'xxl'].includes(key),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ exports[`ConfigProvider components Avatar configProvider componentDisabled 1`] =

exports[`ConfigProvider components Avatar configProvider componentSize large 1`] = `
<span
class="config-avatar config-avatar-circle"
class="config-avatar config-avatar-lg config-avatar-circle"
>
<span
class="config-avatar-string"
Expand All @@ -621,7 +621,7 @@ exports[`ConfigProvider components Avatar configProvider componentSize middle 1`

exports[`ConfigProvider components Avatar configProvider componentSize small 1`] = `
<span
class="config-avatar config-avatar-circle"
class="config-avatar config-avatar-sm config-avatar-circle"
>
<span
class="config-avatar-string"
Expand Down Expand Up @@ -17493,7 +17493,7 @@ exports[`ConfigProvider components List configProvider componentSize large 1`] =
class="config-list-item-meta-avatar"
>
<span
class="config-avatar config-avatar-circle config-avatar-image"
class="config-avatar config-avatar-lg config-avatar-circle config-avatar-image"
>
<img
src="https://xsgames.co/randomusers/avatar.php?g=pixel"
Expand Down Expand Up @@ -17597,7 +17597,7 @@ exports[`ConfigProvider components List configProvider componentSize small 1`] =
class="config-list-item-meta-avatar"
>
<span
class="config-avatar config-avatar-circle config-avatar-image"
class="config-avatar config-avatar-sm config-avatar-circle config-avatar-image"
>
<img
src="https://xsgames.co/randomusers/avatar.php?g=pixel"
Expand Down

0 comments on commit d375cbd

Please sign in to comment.