Tag标签
用于标记事物的属性和维度。
进行分类。
基本标签的用法,可以通过添加 变为可关闭标签。可关闭标签具有 onClose
事件。
import { Tag, Input, Tooltip, Icon } from 'antd';
class EditableTagGroup extends React.Component {
state = {
tags: ['Unremovable', 'Tag 2', 'Tag 3'],
inputVisible: false,
inputValue: '',
};
handleClose = removedTag => {
const tags = this.state.tags.filter(tag => tag !== removedTag);
console.log(tags);
this.setState({ tags });
};
showInput = () => {
this.setState({ inputVisible: true }, () => this.input.focus());
};
handleInputChange = e => {
this.setState({ inputValue: e.target.value });
};
handleInputConfirm = () => {
const { inputValue } = this.state;
let { tags } = this.state;
if (inputValue && tags.indexOf(inputValue) === -1) {
tags = [...tags, inputValue];
}
console.log(tags);
this.setState({
tags,
inputVisible: false,
inputValue: '',
});
};
saveInputRef = input => (this.input = input);
render() {
const { tags, inputVisible, inputValue } = this.state;
return (
<div>
{tags.map((tag, index) => {
const isLongTag = tag.length > 20;
const tagElem = (
<Tag key={tag} closable={index !== 0} onClose={() => this.handleClose(tag)}>
{isLongTag ? `${tag.slice(0, 20)}...` : tag}
</Tag>
);
return isLongTag ? (
<Tooltip title={tag} key={tag}>
{tagElem}
</Tooltip>
) : (
tagElem
);
})}
{inputVisible && (
<Input
ref={this.saveInputRef}
type="text"
size="small"
style={{ width: 78 }}
value={inputValue}
onChange={this.handleInputChange}
onPressEnter={this.handleInputConfirm}
/>
)}
{!inputVisible && (
<Icon type="plus" /> New Tag
</Tag>
)}
</div>
);
}
}
ReactDOM.render(<EditableTagGroup />, mountNode);
选择你感兴趣的话题。
使用 rc-tween-one 给标签增加添加或删除动画。
import { Tag, Input, Icon } from 'antd';
import { TweenOneGroup } from 'rc-tween-one';
class EditableTagGroup extends React.Component {
state = {
tags: ['Tag 1', 'Tag 2', 'Tag 3'],
inputVisible: false,
inputValue: '',
};
handleClose = removedTag => {
const tags = this.state.tags.filter(tag => tag !== removedTag);
console.log(tags);
this.setState({ tags });
};
showInput = () => {
this.setState({ inputVisible: true }, () => this.input.focus());
};
handleInputChange = e => {
this.setState({ inputValue: e.target.value });
};
handleInputConfirm = () => {
const { inputValue } = this.state;
let { tags } = this.state;
if (inputValue && tags.indexOf(inputValue) === -1) {
tags = [...tags, inputValue];
}
console.log(tags);
this.setState({
tags,
inputVisible: false,
inputValue: '',
});
};
saveInputRef = input => (this.input = input);
forMap = tag => {
const tagElem = (
<Tag
closable
onClose={e => {
e.preventDefault();
this.handleClose(tag);
}}
>
{tag}
</Tag>
);
return (
<span key={tag} style={{ display: 'inline-block' }}>
{tagElem}
</span>
);
};
const { tags, inputVisible, inputValue } = this.state;
const tagChild = tags.map(this.forMap);
return (
<div style={{ marginBottom: 16 }}>
<TweenOneGroup
enter={{
scale: 0.8,
opacity: 0,
type: 'from',
duration: 100,
onComplete: e => {
e.target.style = '';
},
}}
leave={{ opacity: 0, width: 0, scale: 0, duration: 200 }}
appear={false}
>
{tagChild}
</TweenOneGroup>
</div>
{inputVisible && (
<Input
ref={this.saveInputRef}
type="text"
size="small"
style={{ width: 78 }}
value={inputValue}
onChange={this.handleInputChange}
onBlur={this.handleInputConfirm}
onPressEnter={this.handleInputConfirm}
/>
)}
{!inputVisible && (
<Tag onClick={this.showInput} style={{ background: '#fff', borderStyle: 'dashed' }}>
<Icon type="plus" /> New Tag
</Tag>
)}
</div>
);
}
}
ReactDOM.render(<EditableTagGroup />, mountNode);
我们添加了多种预设色彩的标签样式,用作不同场景使用。如果预设值不能满足你的需求,可以设置为具体的色值。
.ant-tag {
margin-bottom: 8px;
}
可通过 CheckableTag
实现类似 Checkbox 的效果,点击切换选中效果。
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>
);
}
}
Tag.CheckableTag
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
checked | 设置标签的选中状态 | boolean | false |
onChange | 点击标签时触发的回调 | (checked) => void | - |