EditTagList.tsx 1.37 KB
import * as React from "react";
import { Tag, Input, Tooltip } from "antd";
import { PlusOutlined } from "@ant-design/icons";
import { isArray } from "lodash";
// import "./EditTagList.less";

interface Props {
  value?: string[];
  onChange?: (value: string[]) => any;
}

const colorArr = ["magenta", "geekblue", "cyan", "orange", "green", "lime", "gold", "volcano", "purple", "red"];

export default ({ value = [], onChange }: Props) => {
  function handleClose(tag: string) {
    const tags = value.filter((item) => item !== tag);
    onChange && onChange(tags);
  }
  return (
    <>
      <div style={{ borderStyle: "solid", borderColor: "#eee", padding: 5, borderWidth: 1 }}>
        {isArray(value) &&
          value.map((tag, index) => {
            const isLongTag = tag.length > 20;
            const tagElem = (
              <Tag
                className="edit-tag"
                key={tag}
                closable={value.length > 1}
                color={colorArr[index % 10]}
                onClose={() => handleClose(tag)}
              >
                <span>{isLongTag ? `${tag.slice(0, 20)}...` : tag}</span>
              </Tag>
            );
            return isLongTag ? (
              <Tooltip title={tag} key={tag}>
                {tagElem}
              </Tooltip>
            ) : (
              tagElem
            );
          })}
      </div>
    </>
  );
};