Skip to content

Commit f0aa6aa

Browse files
committed
Merge branch 'main' of github.com:adobe/react-spectrum
2 parents 52c048e + 05a37a0 commit f0aa6aa

File tree

4 files changed

+73
-4
lines changed

4 files changed

+73
-4
lines changed

packages/react-aria-components/docs/TagGroup.mdx

+2-2
Original file line numberDiff line numberDiff line change
@@ -707,9 +707,9 @@ import {ListStateContext} from 'react-aria-components';
707707

708708
function SelectionCount() {
709709
/*- begin highlight -*/
710-
let state = React.useContext(ListStateContext)!;
710+
let state = React.useContext(ListStateContext);
711711
/*- end highlight -*/
712-
let selected = state.selectionManager.selectedKeys.size;
712+
let selected = state?.selectionManager.selectedKeys.size ?? 0;
713713
return <small>{selected} tags selected.</small>;
714714
}
715715

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,8 @@ export const GridListItem = /*#__PURE__*/ createLeafComponent('item', function G
394394
description: descriptionProps
395395
}
396396
}],
397-
[CollectionRendererContext, DefaultCollectionRenderer]
397+
[CollectionRendererContext, DefaultCollectionRenderer],
398+
[ListStateContext, null]
398399
]}>
399400
{renderProps.children}
400401
</Provider>

packages/react-aria-components/stories/GridList.stories.tsx

+26-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13-
import {Button, Checkbox, CheckboxProps, DropIndicator, UNSTABLE_GridLayout as GridLayout, GridList, GridListItem, GridListItemProps, UNSTABLE_ListLayout as ListLayout, useDragAndDrop, UNSTABLE_Virtualizer as Virtualizer} from 'react-aria-components';
13+
import {Button, Checkbox, CheckboxProps, DropIndicator, UNSTABLE_GridLayout as GridLayout, GridList, GridListItem, GridListItemProps, UNSTABLE_ListLayout as ListLayout, Tag, TagGroup, TagList, useDragAndDrop, UNSTABLE_Virtualizer as Virtualizer} from 'react-aria-components';
1414
import {classNames} from '@react-spectrum/utils';
1515
import React, {useMemo} from 'react';
1616
import {Size} from '@react-stately/virtualizer';
@@ -174,3 +174,28 @@ export function VirtualizedGridListGrid() {
174174
</Virtualizer>
175175
);
176176
}
177+
178+
export function TagGroupInsideGridList() {
179+
return (
180+
<GridList
181+
className={styles.menu}
182+
aria-label="Grid list with tag group"
183+
style={{
184+
width: 300,
185+
height: 300
186+
}}>
187+
<MyGridListItem textValue="Tags">
188+
1,1
189+
<TagGroup aria-label="Tag group">
190+
<TagList style={{display: 'flex', gap: 10}}>
191+
<Tag key="1">Tag 1</Tag>
192+
<Tag key="2">Tag 2</Tag>
193+
<Tag key="3">Tag 3</Tag>
194+
</TagList>
195+
</TagGroup>
196+
</MyGridListItem>
197+
<MyGridListItem>1,2 <Button>Actions</Button></MyGridListItem>
198+
<MyGridListItem>1,3 <Button>Actions</Button></MyGridListItem>
199+
</GridList>
200+
);
201+
}

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

+43
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ import {
2020
GridListItem,
2121
UNSTABLE_ListLayout as ListLayout,
2222
RouterProvider,
23+
Tag,
24+
TagGroup,
25+
TagList,
2326
useDragAndDrop,
2427
UNSTABLE_Virtualizer as Virtualizer
2528
} from '../';
@@ -415,6 +418,46 @@ describe('GridList', () => {
415418
expect(rows.map(r => r.textContent)).toEqual(['Item 7', 'Item 8', 'Item 9', 'Item 10', 'Item 11', 'Item 12', 'Item 13', 'Item 14', 'Item 49']);
416419
});
417420

421+
it('should support rendering a TagGroup inside a GridListItem', async () => {
422+
let buttonRef = React.createRef();
423+
let {getAllByRole} = render(
424+
<GridList aria-label="Test">
425+
<GridListItem data-test-id="grid-list" id="tags" textValue="tags">
426+
<TagGroup aria-label="Tag group">
427+
<TagList>
428+
<Tag key="1">Tag 1</Tag>
429+
<Tag key="2">Tag 2</Tag>
430+
<Tag key="3">Tag 3</Tag>
431+
</TagList>
432+
</TagGroup>
433+
</GridListItem>
434+
<GridListItem id="dog" textValue="Dog">Dog <Button aria-label="Info" ref={buttonRef}></Button></GridListItem>
435+
<GridListItem id="kangaroo">Kangaroo</GridListItem>
436+
</GridList>
437+
);
438+
439+
let items = getAllByRole('grid')[0].children;
440+
let tags = within(items[0]).getAllByRole('row');
441+
442+
await user.tab();
443+
expect(document.activeElement).toBe(items[0]);
444+
445+
await user.keyboard('{ArrowRight}');
446+
expect(document.activeElement).toBe(tags[0]);
447+
448+
await user.keyboard('{ArrowRight}');
449+
expect(document.activeElement).toBe(tags[1]);
450+
451+
await user.keyboard('{ArrowDown}');
452+
expect(document.activeElement).toBe(items[1]);
453+
454+
await user.tab();
455+
expect(document.activeElement).toBe(buttonRef.current);
456+
457+
await user.tab();
458+
expect(document.activeElement).toBe(document.body);
459+
});
460+
418461
describe('drag and drop', () => {
419462
it('should support drag button slot', () => {
420463
let {getAllByRole} = render(<DraggableGridList />);

0 commit comments

Comments
 (0)