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

How to clear input field in Draft-js

None 我看过的 Draft-js 演示(由 Facebook,基于 React 构建)展示了如何在提交后清除输入字段。例如,看到这个 code pen linked to from awesome-draft-js where the value you submit remains in the input field after submit. There's also no function in the api 似乎是为了做到这一点而设计的。我为实现这一目标所做的是在提交按钮时创建一个新的空状态,如下所示

onSubmit(){
this.setState({
   editorState: EditorState.createEmpty(),
})
}

但是,由于我在像这样加载编辑器时在构造函数中创建了一个空状态

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

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

推荐使用 EditorState.createEmpty() 来清除编辑器的状态——您应该只在初始化时使用 createEmpty。

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

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

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

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

试试这个

    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})

@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() 删除的 我简直不敢相信这么成熟的库怎么不提供单行重置功能。

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 prop 保存后可以使用 setEditorContent 重置编辑器

setEditorContent(content)