Tag
Tag for categorizing or markup.
When To Use#
It can be used to tag by dimension or property.
When categorizing.
Examples
TypeScript
JavaScript
import { Tag } from 'infrad';
import React from 'react';
const log = (e: React.MouseEvent<HTMLElement>) => {
console.log(e);
};
const preventDefault = (e: React.MouseEvent<HTMLElement>) => {
e.preventDefault();
console.log('Clicked! But prevent default.');
};
const App: React.FC = () => (
<>
<Tag>Tag 1</Tag>
<Tag>
<a href="https://github.com/ant-design/ant-design/issues/1862">Link</a>
</Tag>
<Tag closable onClose={log}>
Tag 2
</Tag>
<Tag closable onClose={preventDefault}>
Prevent Default
</Tag>
</>
);
export default App;
TypeScript
JavaScript
import { PlusOutlined } from 'infra-design-icons';
import type { InputRef } from 'infrad';
import { Input, Tag, Tooltip } from 'infrad';
import React, { useEffect, useRef, useState } from 'react';
const App: React.FC = () => {
const [tags, setTags] = useState<string[]>(['Unremovable', 'Tag 2', 'Tag 3']);
const [inputVisible, setInputVisible] = useState(false);
const [inputValue, setInputValue] = useState('');
const [editInputIndex, setEditInputIndex] = useState(-1);
const [editInputValue, setEditInputValue] = useState('');
const inputRef = useRef<InputRef>(null);
const editInputRef = useRef<InputRef>(null);
useEffect(() => {
if (inputVisible) {
inputRef.current?.focus();
}
}, [inputVisible]);
useEffect(() => {
editInputRef.current?.focus();
}, [inputValue]);
const handleClose = (removedTag: string) => {
const newTags = tags.filter(tag => tag !== removedTag);
console.log(newTags);
setTags(newTags);
};
const showInput = () => {
setInputVisible(true);
};
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setInputValue(e.target.value);
};
const handleInputConfirm = () => {
if (inputValue && tags.indexOf(inputValue) === -1) {
setTags([...tags, inputValue]);
}
setInputVisible(false);
setInputValue('');
};
const handleEditInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setEditInputValue(e.target.value);
};
const handleEditInputConfirm = () => {
const newTags = [...tags];
newTags[editInputIndex] = editInputValue;
setTags(newTags);
setEditInputIndex(-1);
setInputValue('');
};
return (
<>
{tags.map((tag, index) => {
if (editInputIndex === index) {
return (
<Input
ref={editInputRef}
key={tag}
size="small"
className="tag-input"
value={editInputValue}
onChange={handleEditInputChange}
onBlur={handleEditInputConfirm}
onPressEnter={handleEditInputConfirm}
/>
);
}
const isLongTag = tag.length > 20;
const tagElem = (
<Tag
className="edit-tag"
key={tag}
closable={index !== 0}
onClose={() => handleClose(tag)}
>
<span
onDoubleClick={e => {
if (index !== 0) {
setEditInputIndex(index);
setEditInputValue(tag);
e.preventDefault();
}
}}
>
{isLongTag ? `${tag.slice(0, 20)}...` : tag}
</span>
</Tag>
);
return isLongTag ? (
<Tooltip title={tag} key={tag}>
{tagElem}
</Tooltip>
) : (
tagElem
);
})}
{inputVisible && (
<Input
ref={inputRef}
type="text"
size="small"
className="tag-input"
value={inputValue}
onChange={handleInputChange}
onBlur={handleInputConfirm}
onPressEnter={handleInputConfirm}
/>
)}
{!inputVisible && (
<Tag className="site-tag-plus" onClick={showInput}>
<PlusOutlined /> New Tag
</Tag>
)}
</>
);
};
export default App;
.site-tag-plus {
background: #fff;
border-style: dashed;
}
.edit-tag {
user-select: none;
}
.tag-input {
width: 78px;
margin-right: 8px;
vertical-align: top;
}
TypeScript
JavaScript
import { Button, Tag } from 'infrad';
import React, { useState } from 'react';
const App: React.FC = () => {
const [visible, setVisible] = useState(true);
return (
<>
<Tag closable visible={visible} onClose={() => setVisible(false)}>
Movies
</Tag>
<br />
<Button size="small" onClick={() => setVisible(!visible)}>
Toggle
</Button>
</>
);
};
export default App;
TypeScript
JavaScript
import {
FacebookOutlined,
LinkedinOutlined,
TwitterOutlined,
YoutubeOutlined,
} from 'infra-design-icons';
import { Tag } from 'infrad';
import React from 'react';
const App: React.FC = () => (
<>
<Tag icon={<TwitterOutlined />} color="#55acee">
Twitter
</Tag>
<Tag icon={<YoutubeOutlined />} color="#cd201f">
Youtube
</Tag>
<Tag icon={<FacebookOutlined />} color="#3b5999">
Facebook
</Tag>
<Tag icon={<LinkedinOutlined />} color="#55acee">
LinkedIn
</Tag>
</>
);
export default App;
Presets
magentaredvolcanoorangegoldlimegreencyanbluegeekbluepurple
Custom
#f50#2db7f5#87d068#108ee9
TypeScript
JavaScript
import { Divider, Tag } from 'infrad';
import React from 'react';
const App: React.FC = () => (
<>
<Divider orientation="left">Presets</Divider>
<div>
<Tag color="magenta">magenta</Tag>
<Tag color="red">red</Tag>
<Tag color="volcano">volcano</Tag>
<Tag color="orange">orange</Tag>
<Tag color="gold">gold</Tag>
<Tag color="lime">lime</Tag>
<Tag color="green">green</Tag>
<Tag color="cyan">cyan</Tag>
<Tag color="blue">blue</Tag>
<Tag color="geekblue">geekblue</Tag>
<Tag color="purple">purple</Tag>
</div>
<Divider orientation="left">Custom</Divider>
<div>
<Tag color="#f50">#f50</Tag>
<Tag color="#2db7f5">#2db7f5</Tag>
<Tag color="#87d068">#87d068</Tag>
<Tag color="#108ee9">#108ee9</Tag>
</div>
</>
);
export default App;
TypeScript
JavaScript
import { Tag } from 'infrad';
import React, { useState } from 'react';
const { CheckableTag } = Tag;
const tagsData = ['Movies', 'Books', 'Music', 'Sports'];
const App: React.FC = () => {
const [selectedTags, setSelectedTags] = useState<string[]>(['Books']);
const handleChange = (tag: string, checked: boolean) => {
const nextSelectedTags = checked ? [...selectedTags, tag] : selectedTags.filter(t => t !== tag);
console.log('You are interested in: ', nextSelectedTags);
setSelectedTags(nextSelectedTags);
};
return (
<>
<span style={{ marginRight: 8 }}>Categories:</span>
{tagsData.map(tag => (
<CheckableTag
key={tag}
checked={selectedTags.indexOf(tag) > -1}
onChange={checked => handleChange(tag, checked)}
>
{tag}
</CheckableTag>
))}
</>
);
};
export default App;
Tag 1Tag 2Tag 3
TypeScript
JavaScript
import { PlusOutlined } from 'infra-design-icons';
import type { InputRef } from 'infrad';
import { Input, Tag } from 'infrad';
import { TweenOneGroup } from 'rc-tween-one';
import React, { useEffect, useRef, useState } from 'react';
const App: React.FC = () => {
const [tags, setTags] = useState<string[]>(['Tag 1', 'Tag 2', 'Tag 3']);
const [inputVisible, setInputVisible] = useState<boolean>(false);
const [inputValue, setInputValue] = useState('');
const inputRef = useRef<InputRef>(null);
useEffect(() => {
if (inputVisible) {
inputRef.current?.focus();
}
}, []);
const handleClose = (removedTag: string) => {
const newTags = tags.filter(tag => tag !== removedTag);
console.log(newTags);
setTags(newTags);
};
const showInput = () => {
setInputVisible(true);
};
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setInputValue(e.target.value);
};
const handleInputConfirm = () => {
if (inputValue && tags.indexOf(inputValue) === -1) {
setTags([...tags, inputValue]);
}
setInputVisible(false);
setInputValue('');
};
const forMap = (tag: string) => {
const tagElem = (
<Tag
closable
onClose={e => {
e.preventDefault();
handleClose(tag);
}}
>
{tag}
</Tag>
);
return (
<span key={tag} style={{ display: 'inline-block' }}>
{tagElem}
</span>
);
};
const tagChild = tags.map(forMap);
return (
<>
<div style={{ marginBottom: 16 }}>
<TweenOneGroup
enter={{
scale: 0.8,
opacity: 0,
type: 'from',
duration: 100,
}}
onEnd={e => {
if (e.type === 'appear' || e.type === 'enter') {
(e.target as any).style = 'display: inline-block';
}
}}
leave={{ opacity: 0, width: 0, scale: 0, duration: 200 }}
appear={false}
>
{tagChild}
</TweenOneGroup>
</div>
{inputVisible && (
<Input
ref={inputRef}
type="text"
size="small"
style={{ width: 78 }}
value={inputValue}
onChange={handleInputChange}
onBlur={handleInputConfirm}
onPressEnter={handleInputConfirm}
/>
)}
{!inputVisible && (
<Tag onClick={showInput} className="site-tag-plus">
<PlusOutlined /> New Tag
</Tag>
)}
</>
);
};
export default App;
.site-tag-plus {
background: #fff;
border-style: dashed;
}
Without icon
successprocessingerrorwarningdefault
With icon
successprocessingerrorwarningwaitingstop
TypeScript
JavaScript
import {
CheckCircleOutlined,
ClockCircleOutlined,
CloseCircleOutlined,
ExclamationCircleOutlined,
MinusCircleOutlined,
SyncOutlined,
} from 'infra-design-icons';
import { Divider, Tag } from 'infrad';
import React from 'react';
const App: React.FC = () => (
<>
<Divider orientation="left">Without icon</Divider>
<div>
<Tag color="success">success</Tag>
<Tag color="processing">processing</Tag>
<Tag color="error">error</Tag>
<Tag color="warning">warning</Tag>
<Tag color="default">default</Tag>
</div>
<Divider orientation="left">With icon</Divider>
<div>
<Tag icon={<CheckCircleOutlined />} color="success">
success
</Tag>
<Tag icon={<SyncOutlined spin />} color="processing">
processing
</Tag>
<Tag icon={<CloseCircleOutlined />} color="error">
error
</Tag>
<Tag icon={<ExclamationCircleOutlined />} color="warning">
warning
</Tag>
<Tag icon={<ClockCircleOutlined />} color="default">
waiting
</Tag>
<Tag icon={<MinusCircleOutlined />} color="default">
stop
</Tag>
</div>
</>
);
export default App;
API#
Tag#
Property | Description | Type | Default | Version |
---|---|---|---|---|
closable | Whether the Tag can be closed | boolean | false | |
closeIcon | Custom close icon | ReactNode | - | 4.4.0 |
color | Color of the Tag | string | - | |
icon | Set the icon of tag | ReactNode | - | |
visible | Whether the Tag is closed or not | boolean | true | |
onClose | Callback executed when tag is closed | (e) => void | - |
Tag.CheckableTag#
Property | Description | Type | Default |
---|---|---|---|
checked | Checked status of Tag | boolean | false |
onChange | Callback executed when Tag is checked/unchecked | (checked) => void | - |