Skip to content

Commit e8c5549

Browse files
committed
add support for autoFocus and tabIndex in props.domProps
1 parent 0b7bcf5 commit e8c5549

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

examples/src/pages/tests/table/props/active-row-index/default.page.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export default function KeyboardNavigationForRows() {
5555
defaultActiveRowIndex={99}
5656
keyboardNavigation="row"
5757
domProps={{
58+
autoFocus: true,
5859
style: {
5960
height: 800,
6061
},

source/src/components/HeadlessTable/index.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ export function HeadlessTable(
165165
...domProps
166166
} = props;
167167

168+
const { autoFocus } = domProps;
169+
168170
const domRef = useRef<HTMLDivElement>(null);
169171

170172
const [scrollSize, setTotalScrollSize] = useState({
@@ -183,6 +185,9 @@ export function HeadlessTable(
183185
if (!node) {
184186
return;
185187
}
188+
if (autoFocus && document.activeElement !== node) {
189+
node.focus();
190+
}
186191
const onResize = () => {
187192
// it's not enough to read the size from onResize
188193
// since that doesn't account for scrollbar presence and size

source/src/components/InfiniteTable/index.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ function InfiniteTableBody<T>() {
173173
activeCellIndex,
174174
rowDetailRenderer,
175175
showHoverRows,
176+
domProps,
176177
} = componentState;
177178

178179
const LoadMaskCmp = components?.LoadMask ?? LoadMask;
@@ -243,11 +244,14 @@ function InfiniteTableBody<T>() {
243244
computed,
244245
});
245246

247+
const { autoFocus, tabIndex } = domProps ?? {};
248+
246249
return (
247250
<InfiniteTableBodyContainer onContextMenu={onContextMenu}>
248251
<HeadlessTable
249252
debugId={debugId}
250-
tabIndex={0}
253+
tabIndex={tabIndex ?? 0}
254+
autoFocus={autoFocus ?? undefined}
251255
activeRowIndex={
252256
componentState.ready && keyboardNavigation === 'row'
253257
? activeRowIndex
@@ -336,7 +340,16 @@ export const InfiniteTableComponent = React.memo(
336340

337341
const licenseValid = useLicense(licenseKey);
338342

339-
const domProps = useDOMProps<T>(componentState.domProps);
343+
const {
344+
// remove autoFocus and tabIndex from the domProps
345+
// that will be spread on the root DOM element
346+
// as they are meant for the scroller element
347+
autoFocus: _,
348+
tabIndex: __,
349+
...initialDOMProps
350+
} = componentState.domProps ?? {};
351+
352+
const domProps = useDOMProps<T>(initialDOMProps);
340353

341354
React.useEffect(() => {
342355
brain.setScrollStopDelay(scrollStopDelay);

source/src/components/VirtualScrollContainer/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface VirtualScrollContainerProps {
2222

2323
scrollable?: Scrollable;
2424
tabIndex?: number;
25+
autoFocus?: boolean;
2526

2627
onContainerScroll?: (scrollPos: {
2728
scrollTop: number;
@@ -42,6 +43,7 @@ export const VirtualScrollContainer = React.forwardRef(
4243
className,
4344
tabIndex,
4445
style,
46+
autoFocus,
4547
} = props;
4648

4749
const domRef = ref ?? useRef<HTMLDivElement | null>(null);
@@ -54,6 +56,7 @@ export const VirtualScrollContainer = React.forwardRef(
5456
<div
5557
ref={domRef}
5658
style={style}
59+
autoFocus={autoFocus}
5760
tabIndex={tabIndex}
5861
className={join(
5962
className,

0 commit comments

Comments
 (0)