0

我正在尝试转换EditableTabGroup为功能组件Tags,但是在尝试删除时似乎无法正确转换它this.

EditableTabGroup 工作正常,但是当我在其中渲染TagsTaskform它不起作用。

另外,我怎样才能清除状态标签,以便 onCreate(submit) 标签是一个空数组?

class EditableTagGroup extends React.Component {
  state = {
    tags: [],
    inputVisible: false,
    inputValue: ""
  };

  handleClose = removedTag => {
    const tags = this.state.tags.filter(tag => tag !== removedTag);
    console.log(tags);
    this.setState({ tags });
  };

  showInput = () => {
    this.setState({ inputVisible: true }, () => this.input.focus());
  };

  handleInputChange = e => {
    this.setState({ inputValue: e.target.value });
  };

  handleInputConfirm = () => {
    const { inputValue } = this.state;
    let { tags } = this.state;
    if (inputValue && tags.indexOf(inputValue) === -1) {
      tags = [...tags, inputValue];
    }
    console.log(tags);
    this.setState({
      tags,
      inputVisible: false,
      inputValue: ""
    });
  };

  saveInputRef = input => (this.input = input);

  forMap = tag => {
    const tagElem = (
      <Tag
        closable
        onClose={e => {
          e.preventDefault();
          this.handleClose(tag);
        }}
      >
        {tag}
      </Tag>
    );
    return (
      <span key={tag} style={{ display: "inline-block" }}>
        {tagElem}
      </span>
    );
  };

  render() {
    const { tags, inputVisible, inputValue } = this.state;
    const tagChild = tags.map(this.forMap);
    const { getFieldDecorator } = this.props;

    return (
      <div>
        <div style={{ marginBottom: 16 }}>
          <TweenOneGroup
            enter={{
              scale: 0.8,
              opacity: 0,
              type: "from",
              duration: 100,
              onComplete: e => {
                e.target.style = "";
              }
            }}
            leave={{ opacity: 0, width: 0, scale: 0, duration: 200 }}
            appear={false}
          >
            {tagChild}
          </TweenOneGroup>
        </div>
        {inputVisible && (
          <Input
            ref={this.saveInputRef}
            onChange={this.handleInputChange}
            onPressEnter={this.handleInputConfirm}
            value={inputValue}
            onBlur={this.handleInputConfirm}
            type="text"
            size="small"
            style={{ width: 78 }}
          />
        )}
        {getFieldDecorator("tags", {
          initialValue: this.state.tags
        })(
          <Input
            ref={this.saveInputRef}
            type="text"
            size="small"
            style={{ display: "none" }}
          />
        )}
        {!inputVisible && (
          <Tag
            onClick={this.showInput}
            style={{ background: "#fff", borderStyle: "dashed" }}
          >
            <Icon type="plus" /> New Tag
          </Tag>
        )}
      </div>
    );
  }
}

export default EditableTagGroup;

编辑笑-早上-rwktl

4

1 回答 1

3

所以,为了让它发挥作用,我改变了两件事:

1)您没有从其文件中导出标签。在此示例中,我将其导出为命名导出,但您可能应该将其导出为默认导出 ( export default Tags)。

2)第二个问题出在这部分代码中:

 const handleInputConfirm = () => {
    if (inputValue && state.indexOf(inputValue) === -1) {
      let state = [...state, inputValue];
    }
    setState(state);
    setInputVisible(false);
    setInputValue("");
  };

在 if 条件中,您检查当前标签和用户想要添加的标签,您定义一个let“状态”。这里有2个问题。第一个是您在letif 块内部分配了一个,这意味着它不能从块外部访问,因此该行setState(state)只是将状态设置为相同的状态(状态是指状态变量state,而不是state您在 if 块中定义的新内容)。

第二个问题并不是真正的问题,您只是不应该分配名称与上层范围中的变量相同的新变量。正如您现在可能理解的那样,这是不好的做法。

在此处阅读有关let其范围规则的更多信息。

这是完整的工作代码Tags

import React, { useState } from "react";
import { Tag, Input, Icon } from "antd";
import { TweenOneGroup } from "rc-tween-one";

export const Tags = props => {
  const [state, setState] = useState([]);
  const [inputVisible, setInputVisible] = useState(false);
  const [inputValue, setInputValue] = useState("");

  const handleClose = removedTag => {
    const tags = state.filter(tag => tag !== removedTag);
    setState(tags);
  };

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

  const handleInputChange = e => {
    setInputValue(e.target.value);
  };

  const handleInputConfirm = () => {
    if (inputValue && state.indexOf(inputValue) === -1) {
      var newState = [...state, inputValue];
      setState(newState);
    }
    setInputVisible(false);
    setInputValue("");
  };

  const saveInputRef = input => (input = input);

  const forMap = tag => {
    const tagElem = (
      <Tag
        closable
        onClose={e => {
          e.preventDefault();
          handleClose(tag);
        }}
      >
        {tag}
      </Tag>
    );
    return (
      <span key={tag} style={{ display: "inline-block" }}>
        {tagElem}
      </span>
    );
  };

  const tagChild = state.map(forMap);
  const { getFieldDecorator } = props;

  return (
    <div>
      <div style={{ marginBottom: 16 }}>
        <TweenOneGroup
          enter={{
            scale: 0.8,
            opacity: 0,
            type: "from",
            duration: 100,
            onComplete: e => {
              e.target.style = "";
            }
          }}
          leave={{ opacity: 0, width: 0, scale: 0, duration: 200 }}
          appear={false}
        >
          {tagChild}
        </TweenOneGroup>
      </div>
      {inputVisible && (
        <Input
          ref={saveInputRef}
          onChange={handleInputChange}
          onPressEnter={handleInputConfirm}
          value={inputValue}
          onBlur={handleInputConfirm}
          type="text"
          size="small"
          style={{ width: 78 }}
        />
      )}
      {getFieldDecorator("tags", {
        initialValue: state.tags
      })(
        <Input
          ref={saveInputRef}
          type="text"
          size="small"
          style={{ display: "none" }}
        />
      )}
      {!inputVisible && (
        <Tag
          onClick={showInput}
          style={{ background: "#fff", borderStyle: "dashed" }}
        >
          <Icon type="plus" /> New Tag
        </Tag>
      )}
    </div>
  );
};

至于重置标签,您可以在state里面定义状态Taskform.js并将其Tags作为道具传递给。这样您就可以将state( setState([]))重置为Taskform.js

Taskform.js:

const [tags, setTags] = useState([]);

const handleCreate = () => {
    form.validateFields((err, values) => {
      if (err) {
        return;
      }

      form.resetFields();
      onCreate(values);
      setTags([]);
    });
  };

...

<Tags
   getFieldDecorator={getFieldDecorator}
   state={tags}
   setState={setTags}
/>

标签.js:

...
const { state, setState } = props;

当然你也应该[state, setState] = useState([])Tags.js.

希望能帮助到你!

于 2019-12-01T12:52:14.190 回答