import { Tag, Input } from 'antd'; import { TweenOneGroup } from 'rc-tween-one'; import { PlusOutlined } from '@ant-design/icons'; import { useEffect, useRef, useState } from 'react'; const EditableTagGroup = ({ value, onChange, }: { value?: string[]; onChange?: (tags: string[]) => void; }) => { const [inputValue, setInputValue] = useState(''); const [inputVisible, setInputVisible] = useState(false); const [tags, setTags] = useState([]); const saveInputRef = useRef(); const handleClose = (removedTag: string) => { const _tags = tags.filter((tag) => tag !== removedTag); setTags(_tags); onChange?.(_tags); }; const showInput = () => { setInputVisible(true); }; const handleInputChange = (e) => { setInputValue(e.target.value); }; const handleInputConfirm = () => { if (inputValue && !tags.includes(inputValue)) { setTags([...tags, inputValue]); onChange?.([...tags, inputValue]); } setInputVisible(false); setInputValue(''); }; const tagChild = tags.map((tag) => { const tagElem = ( { e.preventDefault(); handleClose(tag); }} > {tag} ); return ( {tagElem} ); }); useEffect(() => { if (inputVisible && saveInputRef) { saveInputRef.current.focus(); } }, [inputVisible]); useEffect(() => { if (value) { setTags(value); } }, [value]); return ( <> {tagChild} {inputVisible && ( )} {!inputVisible && ( 新建 )} ); }; export default EditableTagGroup;