Skip to content

Commit

Permalink
feat: add custom class support to Avatar component
Browse files Browse the repository at this point in the history
  • Loading branch information
adaex committed Aug 28, 2024
1 parent d4f1119 commit e68ac4e
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,55 @@ exports[`avatar demo test avatar demo: custom.md renders correctly 1`] = `
</DocumentFragment>
`;

exports[`avatar demo test avatar demo: custom-class.md renders correctly 1`] = `
<DocumentFragment>
<div
class="avatar-demo-box"
>
<div
class="arco-avatar-wrapper small circle arco-avatar-wrapper-shape-circle"
>
<div
class="arco-avatar avatar-1 arco-avatar-size-small small arco-avatar-shape-circle circle arco-avatar-default-overlap default-overlap"
>
<svg
class="arco-icon arco-icon-user-fill arco-avatar-default arco-avatar-default-icon-size-small"
fill="currentColor"
height="1em"
viewBox="0 0 20 20"
width="1em"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M12.5 10.833c2.301 0 5 1.786 5 5v2.5c0 .46-.373.834-.833.834H3.333a.833.833 0 01-.833-.834v-2.5c0-3.211 2.699-5 5-5h5zM10 1.25a4.167 4.167 0 110 8.333 4.167 4.167 0 010-8.333z"
/>
</svg>
</div>
</div>
<div
class="arco-avatar-wrapper avatar-left-margin small square arco-avatar-wrapper-shape-square"
>
<div
class="arco-avatar avatar-2 arco-avatar-size-small small arco-avatar-shape-square square arco-avatar-default-overlap default-overlap"
>
<svg
class="arco-icon arco-icon-user-fill arco-avatar-default arco-avatar-default-icon-size-small"
fill="currentColor"
height="1em"
viewBox="0 0 20 20"
width="1em"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M12.5 10.833c2.301 0 5 1.786 5 5v2.5c0 .46-.373.834-.833.834H3.333a.833.833 0 01-.833-.834v-2.5c0-3.211 2.699-5 5-5h5zM10 1.25a4.167 4.167 0 110 8.333 4.167 4.167 0 010-8.333z"
/>
</svg>
</div>
</div>
</div>
</DocumentFragment>
`;

exports[`avatar demo test avatar demo: default.md renders correctly 1`] = `
<DocumentFragment>
<div
Expand Down
23 changes: 23 additions & 0 deletions packages/arcodesign/components/avatar/__test__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,27 @@ describe('Avatar', () => {
const groupElements = container.querySelectorAll(`.${prefix}-wrapper`);
expect(groupElements).toHaveLength(7);
});

// 测试 avatarStyle 和 avatarClass 属性
// Test avatarStyle and avatarClass properties
it('avatarStyle and avatarClass render correctly', () => {
const { container } = render(
<div>
<Avatar avatarClass="avatar-1" />
<Avatar shape="square" avatarClass="avatar-2" className="avatar-left-margin" />
</div>,
);

// 通过查询选择器找到带有 'avatar-1' 类的元素,并确保它存在
// Find the element with the 'avatar-1' class using a query selector and ensure it exists
const avatar1Element = container.querySelectorAll('.avatar-1');
expect(avatar1Element).toHaveLength(1);
expect(avatar1Element[0].classList.contains('arco-avatar')).toBe(true);

// 通过查询选择器找到带有 'avatar-2' 类的元素,并确保它存在
// Find the element with the 'avatar-2' class using a query selector and ensure it exists
const avatar2Element = container.querySelectorAll('.avatar-2');
expect(avatar2Element).toHaveLength(1);
expect(avatar2Element[0].classList.contains('arco-avatar')).toBe(true);
});
});
34 changes: 34 additions & 0 deletions packages/arcodesign/components/avatar/demo/custom-class.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## 自定义样式 @en{Custom Avatar}

#### 7

```js
import { Avatar } from '@arco-design/mobile-react';

export default function AvatarDemo() {
return (
<div className="avatar-demo-box">
<Avatar avatarClass="avatar-1" />
<Avatar shape="square" avatarClass="avatar-2" className="avatar-left-margin" />
</div>
);
}
```

```less
.avatar-demo-box {
display: flex;
align-items: center;
justify-content: flex-start;
}
.avatar-left-margin {
.set-prop-with-rtl(margin-left, 24px);
}
.avatar-1 {
border-radius: 10px;
background-color: coral;
}
.avatar-2 {
width: 1rem;
}
```
2 changes: 2 additions & 0 deletions packages/arcodesign/components/avatar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export function componentGenerator<
decoration = null,
textAvatar = '',
avatarStyle = {},
avatarClass = '',
autoFixFontSize = true,
autoFixFontOffset = 2,
avatarName = '',
Expand Down Expand Up @@ -103,6 +104,7 @@ export function componentGenerator<
<div
className={cls(
`${prefixCls}-avatar`,
avatarClass,
`${prefixCls}-avatar-size-${size}`,
size,
`${prefixCls}-avatar-shape-${shape}`,
Expand Down
6 changes: 6 additions & 0 deletions packages/arcodesign/components/avatar/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ export interface AvatarProps<T extends ImageProps = ImageProps> extends BaseProp
* @default {}
*/
avatarStyle?: React.CSSProperties;
/**
* 头像元素的自定义类名
* @en Custom classname for avatar element
* @default ""
*/
avatarClass?: string;
/**
* 是否自动根据头像尺寸调整字体大小
* @en Whether to automatically adjust the font size according to the avatar size
Expand Down

0 comments on commit e68ac4e

Please sign in to comment.