import { Tag } from 'antd'; import { useState } from 'react'; import { Input, Space } from '../'; export interface TagEditableProps { value: string[]; onChange: (value: string[]) => void; placeholder?: string; } const TagEditable = ({ placeholder, onChange, value }: TagEditableProps) => { const [inputValue, setInputValue] = useState(''); const [editInputIndex, setEditInputIndex] = useState(-1); const [editInputValue, setEditInputValue] = useState(''); const handleClose = (removedTag: any) => { onChange(value.filter((tag: any) => tag !== removedTag)); }; const handleInputChange = ({ target: { value } }: any) => { setInputValue(value); }; const handleInputConfirm = (e: { preventDefault: Function }) => { if (inputValue && value.indexOf(inputValue) === -1) { onChange([...value, inputValue]); } setInputValue(''); e.preventDefault(); }; const handleEditInputChange = ({ target: { value } }: any) => { setEditInputValue(value); }; const handleEditInputConfirm = () => { // preventDefault(); const newTags = [...value]; newTags[editInputIndex] = editInputValue; onChange(newTags); setEditInputIndex(-1); setEditInputValue(''); }; return (
{value.map((tag: any, index: any) => { if (editInputIndex === index) { return ( ); } return ( handleClose(tag)}> { setEditInputIndex(index); setEditInputValue(tag); e.preventDefault(); }} > {tag} ); })}
); }; export default TagEditable;