javascript - Fabric 用户界面 : How do I position a ContextualMenu?

标签 javascript reactjs office-addins

我希望能够在指定点打开上下文菜单。

我可以从 API 文档 ( https://dev.office.com/fabric#/components/contextualmenu ) 中看到我必须将 targetPoint 设置为 IPoint,但没有说明如何实际执行此操作.

那么 - 如何使用 IPoint 在 Fabric UI 中定位组件?

最佳答案

(2019 年 3 月编辑:一些用户报告说,在下面的示例中,应该使用 target 而不是 targetPoint。这就是示例下面对我有用。)

好的 - 据我所知,这个 API 完全没有记录,但以下将围绕鼠标单击生成一个上下文菜单

class Preview extends React.Component {
  constructor (props) {
    super(props)
    this.showContextMenu = this.showContextMenu.bind(this)
    this.state = { showContextMenu: false }
  }
  showContextMenu (e, text) {
    this.setState({
      showContextMenu: ((text.length > 0) ? true : false),
      selectionText: text,
      targetPoint: {
        x: e.clientX,
        y: e.clientY
      }
    })
  }
  render () {
    return (
      <div className='ms-font-m ms-bgColor-themeLighter' onMouseUp={(e) => this.showContextMenu(e, window.getSelection().toString())}>
        <div>
          { this.state.showContextMenu && <ContextualMenu useTargetPoint="true" target={this.state.target} items={[ { key: '1', name: "boo" }, { key: '2', name: "yah" }]} /> }
        </div>
        <p>
          This is a really interesting paragraph, etc...
        </p>
      </div>
    )
  }
}

关于javascript - Fabric 用户界面 : How do I position a ContextualMenu?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43741251/

相关文章:

javascript - 使用 ASP.NET Web api 为 Office 加载项添加 Web 服务

javascript - 单击 RMB 时,jsTree 上下文菜单不显示在树节点上

javascript - 如何加载外部 HTML 文件

reactjs - 在 Jenkins 构建中实现 Jest 测试的正确方法

interop - 使用 Word 互操作设置自定义文档属性

在 Office Outlook 加载项中无法访问 Cookie

javascript - ClipboardData - 未捕获类型错误 : Cannot read property 'length' of undefined

java - 在自动完成的东西中加粗用户输入

javascript - 如何检查 if 语句中顺序重要的条件为真?

reactjs - 无法减小 React 组件中 Material UI CardMedia 中图像的大小