📜  React tagInput 组件 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:51.393000             🧑  作者: Mango

代码示例1
const TagInput = ({ tags }) => {
  const [tagData, setTagData] = React.useState(tags);
  const removeTagData = indexToRemove => {
    setTagData([...tagData.filter((_, index) => index !== indexToRemove)]);
  };
  const addTagData = event => {
    if (event.target.value !== '') {
      setTagData([...tagData, event.target.value]);
      event.target.value = '';
    }
  };
  return (
    
    {tagData.map((tag, index) => (
  • {tag} removeTagData(index)} > x
  • ))}
(event.key === 'Enter' ? addTagData(event) : null)} placeholder="Press enter to add a tag" />
); };