Skip to content

Commit 70ab1d4

Browse files
authored
feat(Avatar): the size attr supports rpx units (#3387)
1 parent 483a071 commit 70ab1d4

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

src/avatar/avatar.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { SuperComponent, wxComponent, RelationsOptions } from '../common/src/index';
22
import config from '../common/config';
33
import avatarProps from './props';
4-
import { setIcon } from '../common/utils';
4+
import { setIcon, systemInfo } from '../common/utils';
55

66
const { prefix } = config;
77
const name = `${prefix}-avatar`;
@@ -27,6 +27,7 @@ export default class Avatar extends SuperComponent {
2727
classPrefix: name,
2828
isShow: true,
2929
zIndex: 0,
30+
systemInfo,
3031
};
3132

3233
relations: RelationsOptions = {

src/avatar/avatar.wxml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
>
2323
<view
2424
class="{{this.getClass(classPrefix, size || 'medium', shape, bordered)}} {{prefix}}-class-image"
25-
style="{{this.getSize(size)}}"
25+
style="{{this.getSize(size, systemInfo)}}"
2626
aria-label="{{ ariaLabel || alt ||'头像'}}"
2727
aria-role="{{ ariaRole || 'img'}}"
2828
aria-hidden="{{ ariaHidden }}"

src/avatar/avatar.wxs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,16 @@ module.exports = {
1111
return classNames.join(' ');
1212
},
1313

14-
getSize: function (size = 'medium') {
15-
var pxIndex = size.indexOf('px');
16-
if (pxIndex > -1) {
17-
return 'width:' + size + ';height:' + size + ';font-size:' + ((size.slice(0, pxIndex) / 8) * 3 + 2) + 'px;';
14+
getSize: function (size = 'medium', systemInfo) {
15+
var res = getRegExp('^([0-9]+)(px|rpx)$').exec(size);;
16+
17+
if (res && res.length >= 3) {
18+
var px = res[1];
19+
if (res[2] === 'rpx') {
20+
px = Math.floor((systemInfo.windowWidth * res[1]) / 750);
21+
}
22+
23+
return 'width:' + size + ';height:' + size + ';font-size:' + ((px / 8) * 3 + 2) + 'px';
1824
}
1925
},
2026

0 commit comments

Comments
 (0)