Skip to content

Commit 2060383

Browse files
authored
feat(avatar): support image fill mode in taro avatar component (#3405)
* feat(avatar): support image fill mode in taro avatar component * fix(avatar): deprecate fit prop in taro avatar component rather than remove
1 parent da29016 commit 2060383

File tree

5 files changed

+42
-2
lines changed

5 files changed

+42
-2
lines changed

src/packages/avatar/avatar.taro.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const defaultProps = {
2323
background: '#eee',
2424
color: '#666',
2525
fit: 'cover',
26+
mode: 'scaleToFill',
2627
src: '',
2728
alt: '',
2829
avatarIndex: 0,
@@ -40,6 +41,7 @@ export const Avatar: FunctionComponent<Partial<TaroAvatarProps>> & {
4041
color,
4142
src,
4243
icon,
44+
mode,
4345
fit,
4446
avatarIndex,
4547
className,
@@ -130,6 +132,7 @@ export const Avatar: FunctionComponent<Partial<TaroAvatarProps>> & {
130132
className={`nut-avatar-img nut-avatar-${groupSize || size || 'normal'}-img`}
131133
src={src}
132134
style={{ objectFit: fit }}
135+
mode={mode}
133136
onError={errorEvent}
134137
/>
135138
)}

src/packages/avatar/demo.taro.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import Demo6 from './demos/taro/demo6'
1313
import Demo7 from './demos/taro/demo7'
1414
import Demo8 from './demos/taro/demo8'
1515
import Demo9 from './demos/taro/demo9'
16+
import Demo10 from './demos/taro/demo10'
1617

1718
const AvatarDemo = () => {
1819
const [translated] = useTranslate({
@@ -26,6 +27,7 @@ const AvatarDemo = () => {
2627
f645fc65: '组合头像可控制层级方向',
2728
'43f00872': '点击头像触发事件',
2829
f645fc66: '列表展示',
30+
imageMode: '图片填充模式',
2931
},
3032
'zh-TW': {
3133
'67f78db5': '支持三種尺寸:small、normal、large',
@@ -37,6 +39,7 @@ const AvatarDemo = () => {
3739
f645fc65: '組合頭像可控制層級方向',
3840
'43f00872': '點擊頭像觸發事件',
3941
f645fc66: '列表展示',
42+
imageMode: '圖片填充模式',
4043
},
4144
'en-US': {
4245
'67f78db5': 'Support three sizes: small, normal, large',
@@ -49,6 +52,7 @@ const AvatarDemo = () => {
4952
f645fc65: 'Combining avatars to control hierarchy direction',
5053
'43f00872': 'Click on the avatar to trigger the event',
5154
f645fc66: 'list',
55+
imageMode: 'Image fill mode',
5256
},
5357
})
5458

@@ -76,6 +80,8 @@ const AvatarDemo = () => {
7680
<Demo8 />
7781
<View className="h2">{translated.f645fc66}</View>
7882
<Demo9 />
83+
<View className="h2">{translated.imageMode}</View>
84+
<Demo10 />
7985
</ScrollView>
8086
</>
8187
)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React from 'react'
2+
import { Avatar, Cell } from '@nutui/nutui-react-taro'
3+
4+
const Demo10 = () => {
5+
const src =
6+
'https://storage.360buyimg.com/imgtools/e067cd5b69-07c864c0-dd02-11ed-8b2c-d7f58b17086a.png'
7+
return (
8+
<Cell>
9+
<Avatar src={src} mode="scaleToFill" style={{ marginRight: 10 }} />
10+
<Avatar src={src} mode="aspectFit" style={{ marginRight: 10 }} />
11+
<Avatar src={src} mode="aspectFill" style={{ marginRight: 10 }} />
12+
<Avatar src={src} mode="widthFix" />
13+
</Cell>
14+
)
15+
}
16+
export default Demo10

src/packages/avatar/doc.taro.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ Icon 和字符型可以自定义图标颜色及背景色
9090

9191
:::
9292

93+
### 图片填充模式
94+
95+
:::demo
96+
97+
<CodeBlock src='taro/demo10.tsx'></CodeBlock>
98+
99+
:::
100+
93101
## Avatar
94102

95103
### Props
@@ -101,8 +109,9 @@ Icon 和字符型可以自定义图标颜色及背景色
101109
| background | 设置 Icon、字符类型头像的背景色 | `string` | `#eee` |
102110
| color | 设置 Icon、字符类型头像的颜色 | `string` | `#666` |
103111
| src | 设置图片类型头像的地址 | `string` | `-` |
112+
| mode | 图片裁剪、缩放模式,同 Taro Image 的 mode 属性 | `scaleToFill` \| `aspectFit` \| `aspectFill` \| `widthFix` \| `heightFix` \| `top` \| `bottom` \| `center` \| `left` \| `right` \| `top left` \| `top right` \| `bottom left` \| `bottom right` | `scaleToFill` |
104113
| icon | 设置 Icon 类型头像图标 | `ReactNode` | `-` |
105-
| onClick | 点击头像触发事件 | `(e: MouseEvent<HTMLDivElement>) => void` | `-` |
114+
| onClick | 点击头像触发事件 | `(e: ITouchEvent) => void` | `-` |
106115
| onError | 图片加载失败的事件 | `() => void` | `-` |
107116

108117
## Avatar.Group

src/types/spec/avatar/taro.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
import { ITouchEvent } from '@tarojs/components'
1+
import { ITouchEvent, ImageProps } from '@tarojs/components'
22
import { BaseAvatar, BaseAvatarGroup } from './base'
33

44
export interface TaroAvatarProps extends Omit<BaseAvatar, 'onClick'> {
55
avatarIndex: number
6+
/** 图片裁剪、缩放的模式,同 Taro Image 的 mode 属性 */
7+
mode: keyof ImageProps.Mode
8+
/**
9+
* @deprecated 请使用 mode 属性代替
10+
*/
11+
fit: BaseAvatar['fit']
612
onClick: (e: ITouchEvent) => void
713
}
814

0 commit comments

Comments
 (0)