组件|基于antd封装可删除tag标签组

import { PlusOutlined } from "@ant-design/icons";
import type { InputRef } from "antd";
import { Flex, Input, Tag, theme, Tooltip } from "antd";
import React, { useEffect, useRef, useState } from "react";

const tagInputStyle: React.CSSProperties = {
  width: 64,
  height: 22,
  marginInlineEnd: 8,
  verticalAlign: "top"
};

interface TagsProps {
  value?: Array<string>;
  onChange?: (value: string[]) => void;
}

const Tags: React.FC<TagsProps> = ({ value = [], onChange }) => {
  const { token } = theme.useToken();
  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();
  }, [editInputValue]);

  const handleClose = (removedTag: string) => {
    const newTags = value.filter(tag => tag !== removedTag);
    onChange?.(newTags);
  };

  const showInput = () => {
    setInputVisible(true);
  };

  const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    setInputValue(e.target.value);
  };

  const handleInputConfirm = () => {
    if (inputValue && !value.includes(inputValue)) {
      onChange?.([...value, inputValue]);
    }
    setInputVisible(false);
    setInputValue("");
  };

  const handleEditInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    setEditInputValue(e.target.value);
  };

  const handleEditInputConfirm = () => {
    const newTags = [...value];
    newTags[editInputIndex] = editInputValue;
    onChange?.(newTags);
    setEditInputIndex(-1);
    setEditInputValue("");
  };

  const tagPlusStyle: React.CSSProperties = {
    height: 22,
    background: token.colorBgContainer,
    borderStyle: "dashed"
  };

  return (
    <Flex gap="4px 0" wrap="wrap">
      {value.map<React.ReactNode>((tag, index) => {
        if (editInputIndex === index) {
          return (
            <Input
              ref={editInputRef}
              key={tag}
              size="small"
              style={tagInputStyle}
              value={editInputValue}
              onChange={handleEditInputChange}
              onBlur={handleEditInputConfirm}
              onPressEnter={handleEditInputConfirm}
            />
          );
        }
        const isLongTag = tag.length > 20;
        const tagElem = (
          <Tag key={tag} closeIcon style={{ userSelect: "none" }} 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"
          style={tagInputStyle}
          value={inputValue}
          onChange={handleInputChange}
          onBlur={handleInputConfirm}
          onPressEnter={handleInputConfirm}
        />
      ) : (
        <Tag style={tagPlusStyle} icon={<PlusOutlined />} onClick={showInput}>
          New Tag
        </Tag>
      )}
    </Flex>
  );
};

export default Tags;

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
antd 是一个非常优秀的 React UI 组件库,其提供了丰富、易用的组件,可以帮助我们快速构建美观、高效的 Web 应用程序。如果你想封装自己的组件库并基于 antd 进行扩展,可以按照以下步骤进行: 1. 创建一个基础组件 首先,你需要创建一个基础组件,它包含了你要封装组件的基本结构和样式,可以使用 antd 提供的组件进行合。例如,如果你要封装一个按钮组件,可以创建一个 Button 组件,代码如下: ```jsx import React from 'react'; import { Button } from 'antd'; const MyButton = ({ children, ...restProps }) => { return ( <Button {...restProps}> {children} </Button> ); }; export default MyButton; ``` 2. 根据需求进行扩展 基础组件创建完成后,你可以根据自己的需求进行扩展,添加一些额外的功能或者修改样式等。例如,如果你想为按钮组件添加一个 loading 状态,可以在 MyButton 组件中添加一个 loading 属性,代码如下: ```jsx import React from 'react'; import { Button } from 'antd'; const MyButton = ({ children, loading, ...restProps }) => { return ( <Button {...restProps} loading={loading}> {children} </Button> ); }; export default MyButton; ``` 3. 导出组件并使用 最后,你需要将封装好的组件导出,然后在你的应用程序中使用它。例如,如果你想在应用程序中使用 MyButton 组件,可以按照以下方式进行: ```jsx import React from 'react'; import MyButton from './path/to/MyButton'; const App = () => { return ( <div> <MyButton type="primary" loading={true}>Click Me</MyButton> </div> ); }; export default App; ``` 这样,你就可以基于 antd 进行简单的组件封装了。当然,如果你需要封装更复杂的组件,可能需要更多的步骤和技巧,但这是一个很好的起点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值