如何清除Draft-js中的输入字段

时间:2016-05-26 14:28:48

标签: reactjs draftjs

我在Draft-js上看到的所有演示(由Facebook构建,基于React)都没有显示如何在提交后清除输入字段。例如,请参阅此code pen linked to from awesome-draft-js,其中您提交的值将在提交后保留在输入字段中。似乎没有function in the api似乎是为了做到这一点。我实现的目的是在按钮提交上创建一个新的空状态,如此

  this.state = {
    editorState: EditorState.createEmpty(),
  };

但是,因为我在编译器加载时在构造函数中创建了一个空状态,如此

{{1}}

我担心我可能不会以正确的方式执行此操作,即先前的状态对象可能会成为内存泄漏。问题:在上述情况下重置状态的预期方法是什么(即按钮提交)

4 个答案:

答案 0 :(得分:21)

NOT 建议使用EditorState.createEmpty()来清除编辑器的状态 - 你应该只在初始化时使用createEmpty。

重置编辑器内容的正确方法:

import { EditorState, ContentState } from 'draft-js';

const editorState = EditorState.push(this.state.editorState, ContentState.createFromText(''));
this.setState({ editorState });

@source:https://github.com/draft-js-plugins/draft-js-plugins/blob/master/FAQ.md

答案 1 :(得分:1)

@Vikram Mevasiya的解决方案无法正确清除阻止列表样式

@Sahil的解决方案使光标在下一个输入上混乱

我发现这是唯一可行的解​​决方案:

// https://github.com/jpuri/draftjs-utils/blob/master/js/block.js
const removeSelectedBlocksStyle = (editorState)  => {
    const newContentState = RichUtils.tryToRemoveBlockStyle(editorState);
    if (newContentState) {
        return EditorState.push(editorState, newContentState, 'change-block-type');
    }
    return editorState;
}

// https://github.com/jpuri/draftjs-utils/blob/master/js/block.js
export const getResetEditorState = (editorState) => {
    const blocks = editorState
        .getCurrentContent()
        .getBlockMap()
        .toList();
    const updatedSelection = editorState.getSelection().merge({
        anchorKey: blocks.first().get('key'),
        anchorOffset: 0,
        focusKey: blocks.last().get('key'),
        focusOffset: blocks.last().getLength(),
    });
    const newContentState = Modifier.removeRange(
        editorState.getCurrentContent(),
        updatedSelection,
        'forward'
    );

    const newState = EditorState.push(editorState, newContentState, 'remove-range');
    return removeSelectedBlocksStyle(newState)
}

它由https://github.com/jpuri/draftjs-utils提供的两个辅助功能的组合。 不想为此npm install整个包。 它重置光标状态,但保持阻止列表样式。通过应用removeSelectedBlocksStyle()可以将其删除 我只是无法相信这么成熟的库没有提供单行重置功能。<​​/ p>

答案 2 :(得分:0)

尝试一下

    let editorState = this.state.editorState
    let contentState = editorState.getCurrentContent()
    const firstBlock = contentState.getFirstBlock()
    const lastBlock = contentState.getLastBlock()
    const allSelected = new SelectionState({
        anchorKey: firstBlock.getKey(),
        anchorOffset: 1,
        focusKey: lastBlock.getKey(),
        focusOffset: lastBlock.getLength(),
        hasFocus: true
    })
    contentState = Modifier.removeRange(contentState, allSelected, 'backward')
    editorState = EditorState.push(editorState, contentState, 'remove-range')
    editorState = EditorState.forceSelection(contentState, contentState.getSelectionAfter())
    this.setState({editorState})

答案 3 :(得分:0)

const content = {
  entityMap: {},
  blocks:[
    { 
      key: "637gr",
      text: "",
      type:"unstyled",
      depth:0,
      inlineStyleRanges:[],
      entityRanges: [],
      data: {}
    }
  ]
};
  1. const [editorContent, setEditorContent] = useState(content);

  2. <Editor contentState={editorContent} />

contentState 道具在保存后起作用,您可以使用 setEditorContent 重置编辑器

setEditorContent(content)