Skip to content

Commit

Permalink
improve2
Browse files Browse the repository at this point in the history
  • Loading branch information
ddcat1115 committed Jun 9, 2017
1 parent 7667225 commit 153fb0f
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 334 deletions.
24 changes: 24 additions & 0 deletions components/avatar/__tests__/__snapshots__/demo.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,30 @@ exports[`renders ./components/avatar/demo/basic.md correctly 1`] = `
</div>
`;

exports[`renders ./components/avatar/demo/dynamic.md correctly 1`] = `
<div>
<span
class="ant-avatar ant-avatar-lg ant-avatar-circle"
style="background-color:#f56a00;"
>
<span
class="ant-avatar-string"
>
U
</span>
</span>
<button
class="ant-btn ant-btn-sm"
style="margin-left:16px;"
type="button"
>
<span>
Change
</span>
</button>
</div>
`;

exports[`renders ./components/avatar/demo/type.md correctly 1`] = `
<div>
<div
Expand Down
49 changes: 49 additions & 0 deletions components/avatar/demo/dynamic.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
order: 2
title:
zh-CN: 自动调整字符大小
en-US: Autoset Font Size
---

## zh-CN

对于字符型的头像,当字符串较长时,字体大小可以根据头像宽度自动调整。

## en-US

For letter type Avatar, when the letters are too long to display, the font size can be automatically adjusted according to the width of the Avatar.

````jsx
import { Avatar, Button } from 'antd';

const UserList = ['U', 'Lucy', 'Tom', 'Edward'];
const colorList = ['#f56a00', '#7265e6', '#ffbf00', '#00a2ae'];

class Autoset extends React.Component {
constructor(props) {
super(props);
this.state = {
user: UserList[0],
color: colorList[0],
};
}
changeUser = () => {
const index = UserList.indexOf(this.state.user);
this.setState({
user: index < UserList.length - 1 ? UserList[index + 1] : UserList[0],
color: index < colorList.length - 1 ? colorList[index + 1] : colorList[0],
});
}
render() {
return (
<div>
<Avatar style={{ backgroundColor: this.state.color }} size="large">{this.state.user}</Avatar>
<Button size="small" style={{ marginLeft: 16 }} onClick={this.changeUser}>Change</Button>
</div>
);
}
}

ReactDOM.render(<Autoset />
, mountNode);
````
64 changes: 41 additions & 23 deletions components/avatar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ export default class Avatar extends React.Component<AvatarProps, any> {
shape: 'circle',
size: 'default',
};
refs: {
avatarChildren: HTMLElement;
};

private avatarChildren: any;

constructor(props) {
super(props);
Expand All @@ -36,7 +35,18 @@ export default class Avatar extends React.Component<AvatarProps, any> {
}

componentDidMount() {
const childrenNode = this.refs.avatarChildren;
this.setScale();
}

componentDidUpdate(prevProps, prevState) {
if (prevProps.children !== this.props.children
|| (prevState.scale !== this.state.scale && this.state.scale === 1)) {
this.setScale();
}
}

setScale = () => {
const childrenNode = this.avatarChildren;
if (childrenNode) {
const childrenWidth = childrenNode.offsetWidth;
const avatarWidth = ReactDOM.findDOMNode(this).getBoundingClientRect().width;
Expand All @@ -45,6 +55,10 @@ export default class Avatar extends React.Component<AvatarProps, any> {
this.setState({
scale: (avatarWidth - 8) / childrenWidth,
});
} else {
this.setState({
scale: 1,
});
}
}
}
Expand All @@ -54,21 +68,12 @@ export default class Avatar extends React.Component<AvatarProps, any> {
prefixCls, shape, size, src, icon, className, ...others,
} = this.props;

let sizeCls = '';
switch (size) {
case 'large':
sizeCls = 'lg';
break;
case 'small':
sizeCls = 'sm';
break;
default:
sizeCls = '';
break;
}
const sizeCls = classNames({
[`${prefixCls}-lg`]: size === 'large',
[`${prefixCls}-sm`]: size === 'small',
});

const classString = classNames(prefixCls, className, {
[`${prefixCls}-${sizeCls}`]: sizeCls,
const classString = classNames(prefixCls, className, sizeCls, {
[`${prefixCls}-${shape}`]: shape,
[`${prefixCls}-image`]: src,
[`${prefixCls}-icon`]: icon,
Expand All @@ -80,17 +85,30 @@ export default class Avatar extends React.Component<AvatarProps, any> {
} else if (icon) {
children = <Icon type={icon} />;
} else {
const childrenNode = this.refs.avatarChildren;
if (childrenNode && this.state.scale !== 1) {
const childrenNode = this.avatarChildren;
if (childrenNode || this.state.scale !== 1) {
const childrenStyle: React.CSSProperties = {
msTransform: `scale(${this.state.scale})`,
WebkitTransform: `scale(${this.state.scale})`,
transform: `scale(${this.state.scale})`,
position: 'absolute',
display: 'inline-block',
left: `calc(50% - ${childrenNode.offsetWidth / 2}px)`,
left: `calc(50% - ${Math.round(childrenNode.offsetWidth / 2)}px)`,
};
children = <span className={`${prefixCls}-string`} ref="avatarChildren" style={childrenStyle}>{children}</span>;
children = <span
className={`${prefixCls}-string`}
ref={span => this.avatarChildren = span}
style={childrenStyle}
>
{children}
</span>;
} else {
children = <span className={`${prefixCls}-string`} ref="avatarChildren">{children}</span>;
children = <span
className={`${prefixCls}-string`}
ref={span => this.avatarChildren = span}
>
{children}
</span>;
}
}
return (
Expand Down
6 changes: 1 addition & 5 deletions components/form/__tests__/__snapshots__/demo.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2268,13 +2268,9 @@ exports[`renders ./components/form/demo/validate-other.md correctly 1`] = `
/>
</div>
<div
aria-disabled="false"
aria-valuemax="100"
aria-valuemin="0"
aria-valuenow="0"
class="ant-slider-handle"
role="slider"
style="left:0%;"
value="0"
/>
<div
class="ant-slider-mark"
Expand Down
12 changes: 2 additions & 10 deletions components/grid/__tests__/__snapshots__/demo.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -511,13 +511,9 @@ exports[`renders ./components/grid/demo/playground.md correctly 1`] = `
/>
</div>
<div
aria-disabled="false"
aria-valuemax="5"
aria-valuemin="0"
aria-valuenow="1"
class="ant-slider-handle"
role="slider"
style="left:20%;"
value="1"
/>
<div
class="ant-slider-mark"
Expand Down Expand Up @@ -608,13 +604,9 @@ exports[`renders ./components/grid/demo/playground.md correctly 1`] = `
/>
</div>
<div
aria-disabled="false"
aria-valuemax="5"
aria-valuemin="0"
aria-valuenow="2"
class="ant-slider-handle"
role="slider"
style="left:40%;"
value="2"
/>
<div
class="ant-slider-mark"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,12 @@ exports[`renders ./components/locale-provider/demo/all.md correctly 1`] = `
<li
aria-disabled="true"
class="ant-pagination-disabled ant-pagination-prev"
tabindex="0"
title="Previous Page"
>
<a />
</li>
<li
class="ant-pagination-item ant-pagination-item-1 ant-pagination-item-active"
tabindex="0"
title="1"
>
<a>
Expand All @@ -81,7 +79,6 @@ exports[`renders ./components/locale-provider/demo/all.md correctly 1`] = `
</li>
<li
class="ant-pagination-item ant-pagination-item-2"
tabindex="0"
title="2"
>
<a>
Expand All @@ -90,7 +87,6 @@ exports[`renders ./components/locale-provider/demo/all.md correctly 1`] = `
</li>
<li
class="ant-pagination-item ant-pagination-item-3"
tabindex="0"
title="3"
>
<a>
Expand All @@ -99,7 +95,6 @@ exports[`renders ./components/locale-provider/demo/all.md correctly 1`] = `
</li>
<li
class="ant-pagination-item ant-pagination-item-4"
tabindex="0"
title="4"
>
<a>
Expand All @@ -108,7 +103,6 @@ exports[`renders ./components/locale-provider/demo/all.md correctly 1`] = `
</li>
<li
class="ant-pagination-item ant-pagination-item-5"
tabindex="0"
title="5"
>
<a>
Expand All @@ -118,7 +112,6 @@ exports[`renders ./components/locale-provider/demo/all.md correctly 1`] = `
<li
aria-disabled="false"
class=" ant-pagination-next"
tabindex="0"
title="Next Page"
>
<a />
Expand Down Expand Up @@ -1548,14 +1541,12 @@ exports[`renders ./components/locale-provider/demo/basic.md correctly 1`] = `
<li
aria-disabled="true"
class="ant-pagination-disabled ant-pagination-prev"
tabindex="0"
title="Previous Page"
>
<a />
</li>
<li
class="ant-pagination-item ant-pagination-item-1 ant-pagination-item-active"
tabindex="0"
title="1"
>
<a>
Expand All @@ -1564,7 +1555,6 @@ exports[`renders ./components/locale-provider/demo/basic.md correctly 1`] = `
</li>
<li
class="ant-pagination-item ant-pagination-item-2"
tabindex="0"
title="2"
>
<a>
Expand All @@ -1573,7 +1563,6 @@ exports[`renders ./components/locale-provider/demo/basic.md correctly 1`] = `
</li>
<li
class="ant-pagination-item ant-pagination-item-3"
tabindex="0"
title="3"
>
<a>
Expand All @@ -1582,7 +1571,6 @@ exports[`renders ./components/locale-provider/demo/basic.md correctly 1`] = `
</li>
<li
class="ant-pagination-item ant-pagination-item-4"
tabindex="0"
title="4"
>
<a>
Expand All @@ -1591,7 +1579,6 @@ exports[`renders ./components/locale-provider/demo/basic.md correctly 1`] = `
</li>
<li
class="ant-pagination-item ant-pagination-item-5"
tabindex="0"
title="5"
>
<a>
Expand All @@ -1601,7 +1588,6 @@ exports[`renders ./components/locale-provider/demo/basic.md correctly 1`] = `
<li
aria-disabled="false"
class=" ant-pagination-next"
tabindex="0"
title="Next Page"
>
<a />
Expand Down
Loading

0 comments on commit 153fb0f

Please sign in to comment.