Skip to content

Commit 1f07140

Browse files
nwidynskiBRobin55snowystinger
authored
fix: table typeahead does not honor row textValue (#7857)
* fix: textValue of row will be used for getKeyForSearch fix: removed unnecessary disabledKeys prop from TableBody * add tests --------- Co-authored-by: BRobin55 <robin.bongardt@web.de> Co-authored-by: Robert Snow <rsnow@adobe.com> Co-authored-by: Robert Snow <snowystinger@gmail.com>
1 parent 7c830d7 commit 1f07140

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

packages/@react-aria/table/src/TableKeyboardDelegate.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,13 @@ export class TableKeyboardDelegate<T> extends GridKeyboardDelegate<T, TableColle
178178
return null;
179179
}
180180

181+
if (item.textValue) {
182+
let substring = item.textValue.slice(0, search.length);
183+
if (this.collator.compare(substring, search) === 0) {
184+
return item.key;
185+
}
186+
}
187+
181188
// Check each of the row header cells in this row for a match
182189
for (let cell of getChildNodes(item, this.collection)) {
183190
let column = collection.columns[cell.index];

packages/react-aria-components/src/Table.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,7 @@ export interface TableBodyRenderProps {
912912
isDropTarget: boolean
913913
}
914914

915-
export interface TableBodyProps<T> extends CollectionProps<T>, StyleRenderProps<TableBodyRenderProps> {
915+
export interface TableBodyProps<T> extends Omit<CollectionProps<T>, 'disabledKeys'>, StyleRenderProps<TableBodyRenderProps> {
916916
/** Provides content to display when there are no rows in the table. */
917917
renderEmptyState?: (props: TableBodyRenderProps) => ReactNode
918918
}

packages/react-aria-components/test/Table.test.js

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,66 @@ describe('Table', () => {
815815
expect(cells[0]).toHaveTextContent('Foo (focused)');
816816
});
817817

818+
it('should support columnHeader typeahead', async () => {
819+
let {getAllByRole} = render(
820+
<Table aria-label="Files">
821+
<MyTableHeader columns={columns}>
822+
{column => (
823+
<MyColumn isRowHeader={column.isRowHeader} childColumns={column.children}>
824+
{column.name}
825+
</MyColumn>
826+
)}
827+
</MyTableHeader>
828+
<TableBody items={rows}>
829+
{item => (
830+
<MyRow columns={columns}>
831+
{column => <Cell>{item[column.id]}</Cell>}
832+
</MyRow>
833+
)}
834+
</TableBody>
835+
</Table>
836+
);
837+
let rowElements = getAllByRole('row');
838+
839+
await user.tab();
840+
expect(document.activeElement).toBe(rowElements[1]);
841+
await user.keyboard('boo');
842+
expect(document.activeElement).toBe(rowElements[3]);
843+
});
844+
845+
it('should support textValue overriding typeahead', async () => {
846+
let rows = [
847+
{id: 1, name: '1. Games', date: '6/7/2020', type: 'File folder', textValue: 'Games'},
848+
{id: 2, name: '2. Program Files', date: '4/7/2021', type: 'File folder', textValue: 'Program Files'},
849+
{id: 3, name: '3. bootmgr', date: '11/20/2010', type: 'System file', textValue: 'bootmgr'},
850+
{id: 4, name: '4. log.txt', date: '1/18/2016', type: 'Text Document', textValue: 'log.txt'}
851+
];
852+
let {getAllByRole} = render(
853+
<Table aria-label="Files">
854+
<MyTableHeader columns={columns}>
855+
{column => (
856+
<MyColumn isRowHeader={column.isRowHeader} childColumns={column.children}>
857+
{column.name}
858+
</MyColumn>
859+
)}
860+
</MyTableHeader>
861+
<TableBody items={rows}>
862+
{item => (
863+
<MyRow columns={columns} textValue={item.textValue}>
864+
{column => <Cell>{item[column.id]}</Cell>}
865+
</MyRow>
866+
)}
867+
</TableBody>
868+
</Table>
869+
);
870+
let rowElements = getAllByRole('row');
871+
872+
await user.tab();
873+
expect(document.activeElement).toBe(rowElements[1]);
874+
await user.keyboard('boo');
875+
expect(document.activeElement).toBe(rowElements[3]);
876+
});
877+
818878
it('should support updating columns', () => {
819879
let tree = render(<DynamicTable tableHeaderProps={{columns}} tableBodyProps={{dependencies: [columns]}} rowProps={{columns}} />);
820880
let headers = tree.getAllByRole('columnheader');
@@ -929,7 +989,7 @@ describe('Table', () => {
929989
}
930990
}
931991
});
932-
992+
933993
describe('colSpan', () => {
934994
it('should render table with colSpans', () => {
935995
let {getAllByRole} = render(<TableCellColSpan />);

0 commit comments

Comments
 (0)