| 62 | * @param ref - The ref attached to the table element. |
| 63 | */ |
| 64 | export function useTable<T>( |
| 65 | props: AriaTableProps, |
| 66 | state: TableState<T> | TreeGridState<T>, |
| 67 | ref: RefObject<HTMLElement | null> |
| 68 | ): GridAria { |
| 69 | let {keyboardDelegate, isVirtualized, layoutDelegate, layout} = props; |
| 70 | |
| 71 | // By default, a KeyboardDelegate is provided which uses the DOM to query layout information (e.g. for page up/page down). |
| 72 | // When virtualized, the layout object will be passed in as a prop and override this. |
| 73 | let collator = useCollator({usage: 'search', sensitivity: 'base'}); |
| 74 | let {direction} = useLocale(); |
| 75 | let disabledBehavior = state.selectionManager.disabledBehavior; |
| 76 | let delegate = useMemo( |
| 77 | () => |
| 78 | keyboardDelegate || |
| 79 | new TableKeyboardDelegate({ |
| 80 | collection: state.collection, |
| 81 | disabledKeys: state.disabledKeys, |
| 82 | disabledBehavior, |
| 83 | ref, |
| 84 | direction, |
| 85 | collator, |
| 86 | layoutDelegate, |
| 87 | layout |
| 88 | }), |
| 89 | [ |
| 90 | keyboardDelegate, |
| 91 | state.collection, |
| 92 | state.disabledKeys, |
| 93 | disabledBehavior, |
| 94 | ref, |
| 95 | direction, |
| 96 | collator, |
| 97 | layoutDelegate, |
| 98 | layout |
| 99 | ] |
| 100 | ); |
| 101 | let id = useId(props.id); |
| 102 | gridIds.set(state as TableState<T>, id); |
| 103 | |
| 104 | let {gridProps} = useGrid( |
| 105 | { |
| 106 | ...props, |
| 107 | id, |
| 108 | keyboardDelegate: delegate |
| 109 | }, |
| 110 | state, |
| 111 | ref |
| 112 | ); |
| 113 | |
| 114 | // Override to include header rows |
| 115 | if (isVirtualized) { |
| 116 | gridProps['aria-rowcount'] = state.collection.size + state.collection.headerRows.length; |
| 117 | } |
| 118 | |
| 119 | if (state.treeColumn != null) { |
| 120 | gridProps.role = 'treegrid'; |
| 121 | } |