提及列表和表情符号顶部位置 (Draft.js)

Has*_*qat 4 emoji reactjs mention draftjs draft-js-plugins

您能帮我如何将其位置从下到上更改吗?我想在文本顶部而不是底部显示提及列表。关于表情符号列表的同样问题。

示例链接。

在此输入图像描述

在此输入图像描述

Mik*_*kov 5

positionSuggestions您可以通过配置选项来实现它。此选项适用于mentionemoji插件。

文档摘录:

职位建议

该函数可用于操纵包含建议的弹出窗口的位置。它接收一个对象作为参数,该对象包含围绕修饰搜索字符串(包括@)的可见矩形。此外,该对象还包含 prevProps、prevState、state 和 props。应该返回一个可以包含各种样式的对象。定义的属性将作为内联样式应用。

constructor 应该这样创建插件:

constructor(props) {
  super(props);

  this.mentionPlugin = createMentionPlugin({
    positionSuggestions: (settings) => {
      return {
        left: settings.decoratorRect.left + 'px',
        top: settings.decoratorRect.top - 40 + 'px', // change this value (40) for manage the distance between cursor and bottom edge of popover
        display: 'block',
        transform: 'scale(1) translateY(-100%)', // transition popover on the value of its height
        transformOrigin: '1em 0% 0px',
        transition: 'all 0.25s cubic-bezier(0.3, 1.2, 0.2, 1)'
      };
    }
  });
}
Run Code Online (Sandbox Code Playgroud)

render方法:

render() {
  const { MentionSuggestions } = this.mentionPlugin;
  const plugins = [this.mentionPlugin];

  return (
    <div className={editorStyles.editor} onClick={this.focus}>
      <Editor
        editorState={this.state.editorState}
        onChange={this.onChange}
        plugins={plugins}
        ref={(element) => { this.editor = element; }}
      />
      <div style={{ visibility: this.state.suggestions.length ? 'visible' : 'hidden'}}>
        <MentionSuggestions
          onSearchChange={this.onSearchChange}
          suggestions={this.state.suggestions}
          onAddMention={this.onAddMention}
        />
      </div>
    </div>
  );
}
Run Code Online (Sandbox Code Playgroud)

在这里检查工作示例 - https://codesandbox.io/s/w62x3472k7

  • 谢谢!就我而言,还需要一个位置:“固定”。另外,对于未来的人,请检查[this](https://github.com/draft-js-plugins/draft-js-plugins/issues/665),这是一个很好的解决方案 (3认同)
  • 感谢@hongbomiao使用 **position: 'fixed'** 它现在在我这边工作。感谢您的解决方案。 (2认同)