Tag标签

    • 用于标记事物的属性和维度。

    • 进行分类。

    基本

    基本标签的用法,可以通过添加 变为可关闭标签。可关闭标签具有 onClose 事件。

    Tag 标签 - 图2

    1. import { Tag, Input, Tooltip, Icon } from 'antd';
    2. class EditableTagGroup extends React.Component {
    3. state = {
    4. tags: ['Unremovable', 'Tag 2', 'Tag 3'],
    5. inputVisible: false,
    6. inputValue: '',
    7. };
    8. handleClose = removedTag => {
    9. const tags = this.state.tags.filter(tag => tag !== removedTag);
    10. console.log(tags);
    11. this.setState({ tags });
    12. };
    13. showInput = () => {
    14. this.setState({ inputVisible: true }, () => this.input.focus());
    15. };
    16. handleInputChange = e => {
    17. this.setState({ inputValue: e.target.value });
    18. };
    19. handleInputConfirm = () => {
    20. const { inputValue } = this.state;
    21. let { tags } = this.state;
    22. if (inputValue && tags.indexOf(inputValue) === -1) {
    23. tags = [...tags, inputValue];
    24. }
    25. console.log(tags);
    26. this.setState({
    27. tags,
    28. inputVisible: false,
    29. inputValue: '',
    30. });
    31. };
    32. saveInputRef = input => (this.input = input);
    33. render() {
    34. const { tags, inputVisible, inputValue } = this.state;
    35. return (
    36. <div>
    37. {tags.map((tag, index) => {
    38. const isLongTag = tag.length > 20;
    39. const tagElem = (
    40. <Tag key={tag} closable={index !== 0} onClose={() => this.handleClose(tag)}>
    41. {isLongTag ? `${tag.slice(0, 20)}...` : tag}
    42. </Tag>
    43. );
    44. return isLongTag ? (
    45. <Tooltip title={tag} key={tag}>
    46. {tagElem}
    47. </Tooltip>
    48. ) : (
    49. tagElem
    50. );
    51. })}
    52. {inputVisible && (
    53. <Input
    54. ref={this.saveInputRef}
    55. type="text"
    56. size="small"
    57. style={{ width: 78 }}
    58. value={inputValue}
    59. onChange={this.handleInputChange}
    60. onPressEnter={this.handleInputConfirm}
    61. />
    62. )}
    63. {!inputVisible && (
    64. <Icon type="plus" /> New Tag
    65. </Tag>
    66. )}
    67. </div>
    68. );
    69. }
    70. }
    71. ReactDOM.render(<EditableTagGroup />, mountNode);

    热门标签

    选择你感兴趣的话题。

    Tag 标签 - 图4

    使用 rc-tween-one 给标签增加添加或删除动画。

    1. import { Tag, Input, Icon } from 'antd';
    2. import { TweenOneGroup } from 'rc-tween-one';
    3. class EditableTagGroup extends React.Component {
    4. state = {
    5. tags: ['Tag 1', 'Tag 2', 'Tag 3'],
    6. inputVisible: false,
    7. inputValue: '',
    8. };
    9. handleClose = removedTag => {
    10. const tags = this.state.tags.filter(tag => tag !== removedTag);
    11. console.log(tags);
    12. this.setState({ tags });
    13. };
    14. showInput = () => {
    15. this.setState({ inputVisible: true }, () => this.input.focus());
    16. };
    17. handleInputChange = e => {
    18. this.setState({ inputValue: e.target.value });
    19. };
    20. handleInputConfirm = () => {
    21. const { inputValue } = this.state;
    22. let { tags } = this.state;
    23. if (inputValue && tags.indexOf(inputValue) === -1) {
    24. tags = [...tags, inputValue];
    25. }
    26. console.log(tags);
    27. this.setState({
    28. tags,
    29. inputVisible: false,
    30. inputValue: '',
    31. });
    32. };
    33. saveInputRef = input => (this.input = input);
    34. forMap = tag => {
    35. const tagElem = (
    36. <Tag
    37. closable
    38. onClose={e => {
    39. e.preventDefault();
    40. this.handleClose(tag);
    41. }}
    42. >
    43. {tag}
    44. </Tag>
    45. );
    46. return (
    47. <span key={tag} style={{ display: 'inline-block' }}>
    48. {tagElem}
    49. </span>
    50. );
    51. };
    52. const { tags, inputVisible, inputValue } = this.state;
    53. const tagChild = tags.map(this.forMap);
    54. return (
    55. <div style={{ marginBottom: 16 }}>
    56. <TweenOneGroup
    57. enter={{
    58. scale: 0.8,
    59. opacity: 0,
    60. type: 'from',
    61. duration: 100,
    62. onComplete: e => {
    63. e.target.style = '';
    64. },
    65. }}
    66. leave={{ opacity: 0, width: 0, scale: 0, duration: 200 }}
    67. appear={false}
    68. >
    69. {tagChild}
    70. </TweenOneGroup>
    71. </div>
    72. {inputVisible && (
    73. <Input
    74. ref={this.saveInputRef}
    75. type="text"
    76. size="small"
    77. style={{ width: 78 }}
    78. value={inputValue}
    79. onChange={this.handleInputChange}
    80. onBlur={this.handleInputConfirm}
    81. onPressEnter={this.handleInputConfirm}
    82. />
    83. )}
    84. {!inputVisible && (
    85. <Tag onClick={this.showInput} style={{ background: '#fff', borderStyle: 'dashed' }}>
    86. <Icon type="plus" /> New Tag
    87. </Tag>
    88. )}
    89. </div>
    90. );
    91. }
    92. }
    93. ReactDOM.render(<EditableTagGroup />, mountNode);

    我们添加了多种预设色彩的标签样式,用作不同场景使用。如果预设值不能满足你的需求,可以设置为具体的色值。

    1. .ant-tag {
    2. margin-bottom: 8px;
    3. }

    Tag 标签 - 图6

    可通过 CheckableTag 实现类似 Checkbox 的效果,点击切换选中效果。

    控制关闭状态

    1. import { Tag, Button } from 'antd';
    2. class Demo extends React.Component {
    3. state = {
    4. visible: true,
    5. };
    6. render() {
    7. return (
    8. <div>
    9. <Tag
    10. closable
    11. visible={this.state.visible}
    12. onClose={() => this.setState({ visible: false })}
    13. >
    14. Movies
    15. </Tag>
    16. <br />
    17. <Button size="small" onClick={() => this.setState({ visible: !this.state.visible })}>
    18. Toggle
    19. </Button>
    20. </div>
    21. );
    22. }
    23. }

    Tag.CheckableTag

    参数说明类型默认值
    checked设置标签的选中状态booleanfalse
    onChange点击标签时触发的回调(checked) => void-