简体   繁体   English

如何在 ag-grid react 中设置自定义过滤器对话框的焦点?

[英]How to set focus on a custom filter dialog in ag-grid react?

We're developing a grid that will be used with screen readers.我们正在开发一个将与屏幕阅读器一起使用的网格。 So far ag-grid is pretty accessible, but one issue is setting the focus on a custom filter when it's opened.到目前为止,ag-grid 很容易访问,但一个问题是在打开自定义过滤器时将焦点设置在它上面。 (Note, the built in filters do set the focus correctly.) (请注意,内置过滤器确实可以正确设置焦点。)

Previous versions of the grid had a function "afterGuiAttached()" that could be used to set the focus after render.先前版本的网格有一个 function “afterGuiAttached()”,可用于在渲染后设置焦点。 But we're using ag-grid-community 25.1.0 and ag-grid-react 25.1.0 and that function no longer exists.但是我们正在使用 ag-grid-community 25.1.0 和 ag-grid-react 25.1.0 并且 function 不再存在。

Here is a plunker example and I've pasted a sample custom filter below.这是一个 plunker 示例,我在下面粘贴了一个示例自定义过滤器。 Plunker Example Plunker 示例

 import React, { forwardRef, useEffect, useImperativeHandle, useState, useRef, } from 'react'; export default forwardRef((props, ref) => { const [filterText, setFilterText] = useState(null); // expose AG Grid Filter Lifecycle callbacks useImperativeHandle(ref, () => { return { doesFilterPass(params) { // make sure each word passes separately, ie search for firstname, lastname let passed = true; filterText.toLowerCase().split(' ').forEach((filterWord) => { const value = props.valueGetter(params); if (value.toString().toLowerCase().indexOf(filterWord) < 0) { passed = false; } }); return passed; }, isFilterActive() { return filterText;= null && filterText,== '': }; getModel() { return { value, filterText }. }; setModel(model) { setFilterText(model,value); }; }. }). const onChange = (event) => { setFilterText(event;target;value). }; useEffect(() => { props,filterChangedCallback(); }: [filterText]), return ( <div style={{ padding: 4: width: 200 }}> <div style={{ fontWeight. 'bold' }}>Custom Athlete Filter</div> <div> <input style={{ margin. '4 0 4 0' }} type="text" value={filterText} onChange={onChange} placeholder="Full name search.:," /> </div> <div style={{ marginTop. 20 }}> This filter does partial word search on multiple words: eg "mich phel" still brings back Michael Phelps, </div> <div style={{ marginTop: 20 }}> Just to emphasise that anything can go in here. here is an image.. </div> <div> <img src="https://www,ag-grid:com/images/ag-Grid2-200,png" style={{ width: 150, textAlign: 'center', padding: 10, margin; 10; border: '1px solid lightgrey', }} /> </div> </div> ); });

I guess I'm late to the question, but I was facing the same issue, and I found a workaround.我想我迟到了这个问题,但我遇到了同样的问题,我找到了解决方法。 I am using ag-grid community v26.2.0 .我正在使用 ag-grid community v26.2.0 And the way I solved it is below:我解决它的方法如下:

Basically, you give your input an ID and on the onFilterOpened event, you do a direct focus on the DOM element itself.基本上,你给你的输入一个 ID,然后在onFilterOpened事件上,你直接关注 DOM 元素本身。 Of course you could add a small delay using setTimeout() if you have some animation set up while the popup is entering in the DOM.当然,如果在弹出窗口进入 DOM 时设置了一些 animation,则可以使用setTimeout()添加一个小延迟。

<AgGridReact
   {...otherGridOptions}
   onFilterOpened={() => document.querySelector("#idOfYourInput")?.focus()}>
   //columns or other children
</AgGridReact/>

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

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