mirror of
https://github.com/ant-design/ant-design.git
synced 2024-11-28 21:19:37 +08:00
Merge branch 'feature-3.7.0' of github.com:ant-design/ant-design into feature-3.7.0
This commit is contained in:
commit
a9c8998aea
@ -211,6 +211,29 @@ exports[`renders ./components/tag/demo/control.md correctly 1`] = `
|
|||||||
</div>
|
</div>
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
exports[`renders ./components/tag/demo/controlled.md correctly 1`] = `
|
||||||
|
<div>
|
||||||
|
<div
|
||||||
|
class="ant-tag"
|
||||||
|
data-show="true"
|
||||||
|
>
|
||||||
|
Movies
|
||||||
|
<i
|
||||||
|
class="anticon anticon-cross"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<br />
|
||||||
|
<button
|
||||||
|
class="ant-btn ant-btn-sm"
|
||||||
|
type="button"
|
||||||
|
>
|
||||||
|
<span>
|
||||||
|
Toggle
|
||||||
|
</span>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
|
||||||
exports[`renders ./components/tag/demo/hot-tags.md correctly 1`] = `
|
exports[`renders ./components/tag/demo/hot-tags.md correctly 1`] = `
|
||||||
<div>
|
<div>
|
||||||
<h6
|
<h6
|
||||||
|
17
components/tag/__tests__/__snapshots__/index.test.js.snap
Normal file
17
components/tag/__tests__/__snapshots__/index.test.js.snap
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||||
|
|
||||||
|
exports[`Tag can be controlled by visible 1`] = `
|
||||||
|
<div
|
||||||
|
class="ant-tag ant-tag-zoom-appear"
|
||||||
|
data-show="true"
|
||||||
|
/>
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`Tag can be controlled by visible 2`] = `null`;
|
||||||
|
|
||||||
|
exports[`Tag can be controlled by visible 3`] = `
|
||||||
|
<div
|
||||||
|
class="ant-tag"
|
||||||
|
data-show="true"
|
||||||
|
/>
|
||||||
|
`;
|
@ -38,4 +38,17 @@ describe('Tag', () => {
|
|||||||
jest.runAllTimers();
|
jest.runAllTimers();
|
||||||
expect(wrapper.find('.ant-tag').length).toBe(1);
|
expect(wrapper.find('.ant-tag').length).toBe(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('can be controlled by visible', () => {
|
||||||
|
const wrapper = mount(
|
||||||
|
<Tag visible />
|
||||||
|
);
|
||||||
|
expect(wrapper.render()).toMatchSnapshot();
|
||||||
|
wrapper.setProps({ visible: false });
|
||||||
|
jest.runAllTimers();
|
||||||
|
expect(wrapper.render()).toMatchSnapshot();
|
||||||
|
wrapper.setProps({ visible: true });
|
||||||
|
jest.runAllTimers();
|
||||||
|
expect(wrapper.render()).toMatchSnapshot();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
47
components/tag/demo/controlled.md
Normal file
47
components/tag/demo/controlled.md
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
---
|
||||||
|
order: 5
|
||||||
|
title:
|
||||||
|
zh-CN: 控制关闭状态
|
||||||
|
en-US: Controlled
|
||||||
|
---
|
||||||
|
|
||||||
|
## zh-CN
|
||||||
|
|
||||||
|
通过 `visible` 属性控制关闭状态。
|
||||||
|
|
||||||
|
## en-US
|
||||||
|
|
||||||
|
By using the `visible` prop, you can control the close state of Tag.
|
||||||
|
|
||||||
|
````jsx
|
||||||
|
import { Tag, Button } from 'antd';
|
||||||
|
|
||||||
|
class Demo extends React.Component {
|
||||||
|
state = {
|
||||||
|
visible: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<Tag
|
||||||
|
closable
|
||||||
|
visible={this.state.visible}
|
||||||
|
onClose={() => this.setState({ visible: false })}
|
||||||
|
>
|
||||||
|
Movies
|
||||||
|
</Tag>
|
||||||
|
<br />
|
||||||
|
<Button
|
||||||
|
size="small"
|
||||||
|
onClick={() => this.setState({ visible: !this.state.visible })}
|
||||||
|
>
|
||||||
|
Toggle
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ReactDOM.render(<Demo />, mountNode);
|
||||||
|
````
|
@ -19,9 +19,10 @@ Tag for categorizing or markup.
|
|||||||
| Property | Description | Type | Default |
|
| Property | Description | Type | Default |
|
||||||
| -------- | ----------- | ---- | ------- |
|
| -------- | ----------- | ---- | ------- |
|
||||||
| afterClose | Callback executed when close animation is completed | () => void | - |
|
| afterClose | Callback executed when close animation is completed | () => void | - |
|
||||||
| closable | Whether Tag can be closed | boolean | `false` |
|
| closable | Whether the Tag can be closed | boolean | `false` |
|
||||||
| color | Color of the Tag | string | - |
|
| color | Color of the Tag | string | - |
|
||||||
| onClose | Callback executed when tag is closed | (e) => void | - |
|
| onClose | Callback executed when tag is closed | (e) => void | - |
|
||||||
|
| visible | Whether the Tag is closed or not | boolean | `true` |
|
||||||
|
|
||||||
### Tag.CheckableTag
|
### Tag.CheckableTag
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ import * as ReactDOM from 'react-dom';
|
|||||||
import Animate from 'rc-animate';
|
import Animate from 'rc-animate';
|
||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
import omit from 'omit.js';
|
import omit from 'omit.js';
|
||||||
|
import { polyfill } from 'react-lifecycles-compat';
|
||||||
import Icon from '../icon';
|
import Icon from '../icon';
|
||||||
import CheckableTag from './CheckableTag';
|
import CheckableTag from './CheckableTag';
|
||||||
|
|
||||||
@ -14,6 +15,7 @@ export interface TagProps extends React.HTMLAttributes<HTMLDivElement> {
|
|||||||
color?: string;
|
color?: string;
|
||||||
/** 标签是否可以关闭 */
|
/** 标签是否可以关闭 */
|
||||||
closable?: boolean;
|
closable?: boolean;
|
||||||
|
visible?: boolean;
|
||||||
/** 关闭时的回调 */
|
/** 关闭时的回调 */
|
||||||
onClose?: Function;
|
onClose?: Function;
|
||||||
/** 动画关闭后的回调 */
|
/** 动画关闭后的回调 */
|
||||||
@ -24,30 +26,47 @@ export interface TagProps extends React.HTMLAttributes<HTMLDivElement> {
|
|||||||
export interface TagState {
|
export interface TagState {
|
||||||
closing: boolean;
|
closing: boolean;
|
||||||
closed: boolean;
|
closed: boolean;
|
||||||
|
visible: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class Tag extends React.Component<TagProps, TagState> {
|
class Tag extends React.Component<TagProps, TagState> {
|
||||||
static CheckableTag = CheckableTag;
|
static CheckableTag = CheckableTag;
|
||||||
static defaultProps = {
|
static defaultProps = {
|
||||||
prefixCls: 'ant-tag',
|
prefixCls: 'ant-tag',
|
||||||
closable: false,
|
closable: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
constructor(props: TagProps) {
|
static getDerivedStateFromProps(nextProps: TagProps) {
|
||||||
super(props);
|
return ('visible' in nextProps) ? { visible: nextProps.visible } : null;
|
||||||
|
|
||||||
this.state = {
|
|
||||||
closing: false,
|
|
||||||
closed: false,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
close = (e: React.MouseEvent<HTMLElement>) => {
|
state = {
|
||||||
|
closing: false,
|
||||||
|
closed: false,
|
||||||
|
visible: true,
|
||||||
|
};
|
||||||
|
|
||||||
|
componentDidUpdate(_prevProps: TagProps, prevState: TagState) {
|
||||||
|
if (prevState.visible && !this.state.visible) {
|
||||||
|
this.close();
|
||||||
|
} else if (!prevState.visible && this.state.visible) {
|
||||||
|
this.show();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
handleIconClick = (e: React.MouseEvent<HTMLElement>) => {
|
||||||
const onClose = this.props.onClose;
|
const onClose = this.props.onClose;
|
||||||
if (onClose) {
|
if (onClose) {
|
||||||
onClose(e);
|
onClose(e);
|
||||||
}
|
}
|
||||||
if (e.defaultPrevented) {
|
if (e.defaultPrevented || 'visible' in this.props) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.setState({ visible: false });
|
||||||
|
}
|
||||||
|
|
||||||
|
close = () => {
|
||||||
|
if (this.state.closing || this.state.closed) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const dom = ReactDOM.findDOMNode(this) as HTMLElement;
|
const dom = ReactDOM.findDOMNode(this) as HTMLElement;
|
||||||
@ -59,6 +78,12 @@ export default class Tag extends React.Component<TagProps, TagState> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
show = () => {
|
||||||
|
this.setState({
|
||||||
|
closed: false,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
animationEnd = (_: string, existed: boolean) => {
|
animationEnd = (_: string, existed: boolean) => {
|
||||||
if (!existed && !this.state.closed) {
|
if (!existed && !this.state.closed) {
|
||||||
this.setState({
|
this.setState({
|
||||||
@ -70,6 +95,10 @@ export default class Tag extends React.Component<TagProps, TagState> {
|
|||||||
if (afterClose) {
|
if (afterClose) {
|
||||||
afterClose();
|
afterClose();
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
this.setState({
|
||||||
|
closed: false,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -83,7 +112,7 @@ export default class Tag extends React.Component<TagProps, TagState> {
|
|||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { prefixCls, closable, color, className, children, style, ...otherProps } = this.props;
|
const { prefixCls, closable, color, className, children, style, ...otherProps } = this.props;
|
||||||
const closeIcon = closable ? <Icon type="cross" onClick={this.close} /> : '';
|
const closeIcon = closable ? <Icon type="cross" onClick={this.handleIconClick} /> : '';
|
||||||
const isPresetColor = this.isPresetColor(color);
|
const isPresetColor = this.isPresetColor(color);
|
||||||
const classString = classNames(prefixCls, {
|
const classString = classNames(prefixCls, {
|
||||||
[`${prefixCls}-${color}`]: isPresetColor,
|
[`${prefixCls}-${color}`]: isPresetColor,
|
||||||
@ -94,6 +123,7 @@ export default class Tag extends React.Component<TagProps, TagState> {
|
|||||||
const divProps = omit(otherProps, [
|
const divProps = omit(otherProps, [
|
||||||
'onClose',
|
'onClose',
|
||||||
'afterClose',
|
'afterClose',
|
||||||
|
'visible',
|
||||||
]);
|
]);
|
||||||
const tagStyle = {
|
const tagStyle = {
|
||||||
backgroundColor: (color && !isPresetColor) ? color : null,
|
backgroundColor: (color && !isPresetColor) ? color : null,
|
||||||
@ -123,3 +153,7 @@ export default class Tag extends React.Component<TagProps, TagState> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
polyfill(Tag);
|
||||||
|
|
||||||
|
export default Tag;
|
||||||
|
@ -22,6 +22,7 @@ title: Tag
|
|||||||
| closable | 标签是否可以关闭 | boolean | false |
|
| closable | 标签是否可以关闭 | boolean | false |
|
||||||
| color | 标签色 | string | - |
|
| color | 标签色 | string | - |
|
||||||
| onClose | 关闭时的回调 | (e) => void | - |
|
| onClose | 关闭时的回调 | (e) => void | - |
|
||||||
|
| visible | 是否显示标签 | boolean | `true` |
|
||||||
|
|
||||||
### Tag.CheckableTag
|
### Tag.CheckableTag
|
||||||
|
|
||||||
|
@ -80,6 +80,7 @@
|
|||||||
"rc-upload": "~2.5.0",
|
"rc-upload": "~2.5.0",
|
||||||
"rc-util": "^4.0.4",
|
"rc-util": "^4.0.4",
|
||||||
"react-lazy-load": "^3.0.12",
|
"react-lazy-load": "^3.0.12",
|
||||||
|
"react-lifecycles-compat": "^3.0.4",
|
||||||
"react-slick": "~0.23.1",
|
"react-slick": "~0.23.1",
|
||||||
"shallowequal": "^1.0.1",
|
"shallowequal": "^1.0.1",
|
||||||
"warning": "~4.0.1"
|
"warning": "~4.0.1"
|
||||||
|
2
typings/custom-typings.d.ts
vendored
2
typings/custom-typings.d.ts
vendored
@ -102,3 +102,5 @@ declare module "lodash/uniqBy";
|
|||||||
declare module 'intersperse';
|
declare module 'intersperse';
|
||||||
|
|
||||||
declare module "raf";
|
declare module "raf";
|
||||||
|
|
||||||
|
declare module "react-lifecycles-compat";
|
||||||
|
Loading…
Reference in New Issue
Block a user