简体   繁体   English

如何使用React和Office Fabric将按钮聚焦到详细信息列表中

[英]How to focus a button in a details list using React and Office Fabric

Anyone have suggestions on how to focus a button in react DetailsList? 任何人都有关于如何将一个按钮聚焦在反应DetailsList中的建议? Refs seem like the way to go, and I'd like to do something like this: Refs似乎是要走的路,我想做这样的事情:

        findDOMNode<HTMLButtonElement>(this.refs.mybutton).focus()

But I haven't been able to get a ref handle on the button element. 但是我无法在按钮元素上获得ref句柄。

One approach I've tried is in my DetailsList: 我尝试过的一种方法是在我的DetailsList中:

        <DetailsList
            onRenderItemColumn={this.renderItemColumn}
            ref={(list) => {this.detailsList = list;}}
            ....
        />

And the button I want to focus (coming from renderItemColumn): 我想要关注的按钮(来自renderItemColumn):

                <PrimaryButton
                    onClick={() => this.handleActionSelection(fieldContent)}
                    ariaDescription={buttonText}
                    text={buttonText}
                    ref={"ButtonIWantToFocus"}
                />

In didComponentMount() I can now get access to the DetailList, but I'm not sure how to get the button ref to focus. 在didComponentMount()中,我现在可以访问DetailList,但我不知道如何让按钮引用焦点。

Alternatively I can define the button as: 或者,我可以将按钮定义为:

                    <PrimaryButton
                        disabled={true}
                        ariaDescription={buttonText}
                        text={buttonText}
                        ref={(button) => {this.focusButton = button;}}
                    />

This give me a handle to the button, but it has no focus(), function. 这给了我一个按钮的句柄,但它没有focus(),函数。

Thanks for your ideas. 谢谢你的想法。

I think you are not referencing the ref correctly (you don't need findDOMNode ). 我认为你没有正确引用ref (你不需要findDOMNode )。
After you set and attach your ref to the class instance using the this key word you just reference it using the this key word. 使用this关键字设置并将ref附加到类实例后,只需使用this关键字引用它即可。

Here is a small example: 这是一个小例子:

 class App extends React.Component { focusBtn = () => { this.btn.focus(); }; render() { return ( <div> <div onClick={this.focusBtn}>click here to focus the button below</div> <hr/> <button ref={ref => {this.btn = ref}}>im a button</button> </div> ); } } ReactDOM.render(<App />, document.getElementById("root")); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="root"></div> 

Edit 编辑
Sorry i just noticed you were talking about a component (not just a component but a library component of fabric-ui ). 对不起,我刚刚注意到你在谈论一个组件(不仅仅是一个组件,而是一个fabric-ui的库组件)。
After doing some reading on their github page i found out you should use the prop componentRef instead of ref . 在他们的github页面上做了一些阅读后我发现你应该使用prop componentRef而不是ref
Here is the relevant example with fabric-ui : 以下是fabric-ui的相关示例:

 class App extends React.Component { focusBtn = () => { this.btn.focus(); }; render() { return ( <div> <div onClick={this.focusBtn}>click here to focus the button below</div> <hr/> <Fabric.PrimaryButton componentRef={ref => {this.btn = ref}}>im a button</Fabric.PrimaryButton> </div> ); } } ReactDOM.render(<App />, document.getElementById("root")); 
 button.ms-Button.ms-Button--primary.css-32:focus{ border: 2px solid red; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <link rel="stylesheet" href="https://static2.sharepointonline.com/files/fabric/office-ui-fabric-js/1.4.0/css/fabric.min.css"> <link rel="stylesheet" href="https://static2.sharepointonline.com/files/fabric/office-ui-fabric-js/1.4.0/css/fabric.components.min.css"> <script type="text/javascript" src="//unpkg.com/office-ui-fabric-react/dist/office-ui-fabric-react.min.js"></script> <div id="root"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM