-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathGridList.tsx
More file actions
687 lines (628 loc) · 25.6 KB
/
GridList.tsx
File metadata and controls
687 lines (628 loc) · 25.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
/*
* Copyright 2022 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import {AriaGridListProps, DraggableItemResult, DragPreviewRenderer, DropIndicatorAria, DroppableCollectionResult, FocusScope, ListKeyboardDelegate, mergeProps, useCollator, useFocusRing, useGridList, useGridListItem, useGridListSection, useGridListSelectionCheckbox, useHover, useLocale, useVisuallyHidden} from 'react-aria';
import {ButtonContext} from './Button';
import {CheckboxContext, FieldInputContext, SelectableCollectionContext, SelectableCollectionContextValue} from './RSPContexts';
import {
ClassNameOrFunction,
ContextValue,
DEFAULT_SLOT,
dom,
DOMProps,
DOMRenderProps,
Provider,
RenderProps,
SlotProps,
StyleProps,
StyleRenderProps,
useContextProps,
useRenderProps
} from './utils';
import {Collection, CollectionBuilder, createBranchComponent, createLeafComponent, HeaderNode, ItemNode, LoaderNode, SectionNode} from '@react-aria/collections';
import {CollectionProps, CollectionRendererContext, DefaultCollectionRenderer, ItemRenderProps, SectionProps} from './Collection';
import {DragAndDropContext, DropIndicatorContext, DropIndicatorProps, useDndPersistedKeys, useRenderDropIndicator} from './DragAndDrop';
import {DragAndDropHooks} from './useDragAndDrop';
import {DraggableCollectionState, DroppableCollectionState, Collection as ICollection, ListState, Node, SelectionBehavior, UNSTABLE_useFilteredListState, useListState} from 'react-stately';
import {filterDOMProps, inertValue, LoadMoreSentinelProps, useLoadMoreSentinel, useObjectRef} from '@react-aria/utils';
import {forwardRefType, GlobalDOMAttributes, HoverEvents, Key, LinkDOMProps, PressEvents, RefObject} from '@react-types/shared';
import {ListStateContext} from './ListBox';
import React, {createContext, ForwardedRef, forwardRef, HTMLAttributes, JSX, ReactNode, useContext, useEffect, useMemo, useRef} from 'react';
import {SelectionIndicatorContext} from './SelectionIndicator';
import {SharedElementTransition} from './SharedElementTransition';
import {TextContext} from './Text';
export interface GridListRenderProps {
/**
* Whether the list has no items and should display its empty state.
* @selector [data-empty]
*/
isEmpty: boolean,
/**
* Whether the grid list is currently focused.
* @selector [data-focused]
*/
isFocused: boolean,
/**
* Whether the grid list is currently keyboard focused.
* @selector [data-focus-visible]
*/
isFocusVisible: boolean,
/**
* Whether the grid list is currently the active drop target.
* @selector [data-drop-target]
*/
isDropTarget: boolean,
/**
* Whether the items are arranged in a stack or grid.
* @selector [data-layout="stack | grid"]
*/
layout: 'stack' | 'grid',
/**
* Where pointer dragging can start.
* @selector [data-pointer-drag-source="item | dragButton"]
*/
pointerDragSource?: 'item' | 'dragButton',
/**
* State of the grid list.
*/
state: ListState<unknown>
}
export interface GridListProps<T> extends Omit<AriaGridListProps<T>, 'children'>, CollectionProps<T>, StyleRenderProps<GridListRenderProps>, SlotProps, GlobalDOMAttributes<HTMLDivElement> {
/**
* The CSS [className](https://developer.mozilla.org/en-US/docs/Web/API/Element/className) for the element. A function may be provided to compute the class based on component state.
* @default 'react-aria-GridList'
*/
className?: ClassNameOrFunction<GridListRenderProps>,
/**
* Whether typeahead navigation is disabled.
* @default false
*/
disallowTypeAhead?: boolean,
/**
* How multiple selection should behave in the collection.
* @default "toggle"
*/
selectionBehavior?: SelectionBehavior,
/** The drag and drop hooks returned by `useDragAndDrop` used to enable drag and drop behavior for the GridList. */
dragAndDropHooks?: DragAndDropHooks<NoInfer<T>>,
/** Provides content to display when there are no items in the list. */
renderEmptyState?: (props: GridListRenderProps) => ReactNode,
/**
* Whether the items are arranged in a stack or grid.
* @default 'stack'
*/
layout?: 'stack' | 'grid'
}
export const GridListContext = createContext<ContextValue<GridListProps<any>, HTMLDivElement>>(null);
/**
* A grid list displays a list of interactive items, with support for keyboard navigation,
* single or multiple selection, and row actions.
*/
export const GridList = /*#__PURE__*/ (forwardRef as forwardRefType)(function GridList<T extends object>(props: GridListProps<T>, ref: ForwardedRef<HTMLDivElement>) {
// Render the portal first so that we have the collection by the time we render the DOM in SSR.
[props, ref] = useContextProps(props, ref, GridListContext);
return (
<CollectionBuilder content={<Collection {...props} />}>
{collection => <GridListInner props={props} collection={collection} gridListRef={ref} />}
</CollectionBuilder>
);
});
interface GridListInnerProps<T extends object> {
props: GridListProps<T> & SelectableCollectionContextValue<T>,
collection: ICollection<Node<object>>,
gridListRef: RefObject<HTMLElement | null>
}
function GridListInner<T extends object>({props, collection, gridListRef: ref}: GridListInnerProps<T>) {
[props, ref] = useContextProps(props, ref, SelectableCollectionContext);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
let {shouldUseVirtualFocus, filter, disallowTypeAhead, ...DOMCollectionProps} = props;
let {dragAndDropHooks, keyboardNavigationBehavior = 'arrow', layout = 'stack'} = props;
let {CollectionRoot, isVirtualized, layoutDelegate, dropTargetDelegate: ctxDropTargetDelegate} = useContext(CollectionRendererContext);
let gridlistState = useListState({
...DOMCollectionProps,
collection,
children: undefined,
layoutDelegate
});
let filteredState = UNSTABLE_useFilteredListState(gridlistState as ListState<T>, filter);
let collator = useCollator({usage: 'search', sensitivity: 'base'});
let {disabledBehavior, disabledKeys} = filteredState.selectionManager;
let {direction} = useLocale();
let keyboardDelegate = useMemo(() => (
new ListKeyboardDelegate({
collection: filteredState.collection,
collator,
ref,
disabledKeys,
disabledBehavior,
layoutDelegate,
layout,
direction
})
), [filteredState.collection, ref, layout, disabledKeys, disabledBehavior, layoutDelegate, collator, direction]);
let {gridProps} = useGridList({
...DOMCollectionProps,
keyboardDelegate,
// Only tab navigation is supported in grid layout.
keyboardNavigationBehavior: layout === 'grid' ? 'tab' : keyboardNavigationBehavior,
isVirtualized,
shouldSelectOnPressUp: props.shouldSelectOnPressUp,
disallowTypeAhead
}, filteredState, ref);
let selectionManager = filteredState.selectionManager;
let isListDraggable = !!dragAndDropHooks?.useDraggableCollectionState;
let isListDroppable = !!dragAndDropHooks?.useDroppableCollectionState;
let dragHooksProvided = useRef(isListDraggable);
let dropHooksProvided = useRef(isListDroppable);
useEffect(() => {
if (process.env.NODE_ENV === 'production') {
return;
}
if (dragHooksProvided.current !== isListDraggable) {
console.warn('Drag hooks were provided during one render, but not another. This should be avoided as it may produce unexpected behavior.');
}
if (dropHooksProvided.current !== isListDroppable) {
console.warn('Drop hooks were provided during one render, but not another. This should be avoided as it may produce unexpected behavior.');
}
}, [isListDraggable, isListDroppable]);
let dragState: DraggableCollectionState | undefined = undefined;
let dropState: DroppableCollectionState | undefined = undefined;
let droppableCollection: DroppableCollectionResult | undefined = undefined;
let isRootDropTarget = false;
let dragPreview: JSX.Element | null = null;
let preview = useRef<DragPreviewRenderer>(null);
if (isListDraggable && dragAndDropHooks) {
dragState = dragAndDropHooks.useDraggableCollectionState!({
collection: filteredState.collection,
selectionManager,
preview: dragAndDropHooks.renderDragPreview ? preview : undefined
});
dragAndDropHooks.useDraggableCollection!({}, dragState, ref);
let DragPreview = dragAndDropHooks.DragPreview!;
dragPreview = dragAndDropHooks.renderDragPreview
? <DragPreview ref={preview}>{dragAndDropHooks.renderDragPreview}</DragPreview>
: null;
}
if (isListDroppable && dragAndDropHooks) {
dropState = dragAndDropHooks.useDroppableCollectionState!({
collection: filteredState.collection,
selectionManager
});
let keyboardDelegate = new ListKeyboardDelegate({
collection: filteredState.collection,
disabledKeys: selectionManager.disabledKeys,
disabledBehavior: selectionManager.disabledBehavior,
ref
});
let dropTargetDelegate = dragAndDropHooks.dropTargetDelegate || ctxDropTargetDelegate || new dragAndDropHooks.ListDropTargetDelegate(collection, ref, {layout, direction});
droppableCollection = dragAndDropHooks.useDroppableCollection!({
keyboardDelegate,
dropTargetDelegate
}, dropState, ref);
isRootDropTarget = dropState.isDropTarget({type: 'root'});
}
let pointerDragSource = (isListDraggable && !dragState?.isDisabled) ? dragAndDropHooks?.pointerDragSource : undefined;
let {focusProps, isFocused, isFocusVisible} = useFocusRing();
let isEmpty = filteredState.collection.size === 0;
let renderValues = {
isDropTarget: isRootDropTarget,
isEmpty,
isFocused,
isFocusVisible,
layout,
pointerDragSource,
state: filteredState
};
let renderProps = useRenderProps({
...props,
children: undefined,
defaultClassName: 'react-aria-GridList',
values: renderValues
});
let emptyState: ReactNode = null;
let emptyStatePropOverrides: HTMLAttributes<HTMLElement> | null = null;
if (isEmpty && props.renderEmptyState) {
let content = props.renderEmptyState(renderValues);
emptyState = (
<div role="row" aria-rowindex={1} style={{display: 'contents'}}>
<div role="gridcell" style={{display: 'contents'}}>
{content}
</div>
</div>
);
}
let DOMProps = filterDOMProps(props, {global: true});
return (
<FocusScope>
<dom.div
{...mergeProps(DOMProps, renderProps, gridProps, focusProps, droppableCollection?.collectionProps, emptyStatePropOverrides)}
ref={ref as RefObject<HTMLDivElement>}
slot={props.slot || undefined}
onScroll={props.onScroll}
data-drop-target={isRootDropTarget || undefined}
data-empty={isEmpty || undefined}
data-focused={isFocused || undefined}
data-focus-visible={isFocusVisible || undefined}
data-layout={layout}
data-pointer-drag-source={pointerDragSource}>
<Provider
values={[
[ListStateContext, filteredState],
[DragAndDropContext, {dragAndDropHooks, dragState, dropState}],
[DropIndicatorContext, {render: GridListDropIndicatorWrapper}]
]}>
{isListDroppable && <RootDropIndicator />}
<SharedElementTransition>
<CollectionRoot
collection={filteredState.collection}
scrollRef={ref}
persistedKeys={useDndPersistedKeys(selectionManager, dragAndDropHooks, dropState)}
renderDropIndicator={useRenderDropIndicator(dragAndDropHooks, dropState)} />
</SharedElementTransition>
</Provider>
{emptyState}
{dragPreview}
</dom.div>
</FocusScope>
);
}
export interface GridListItemRenderProps extends ItemRenderProps {
/** The unique id of the item. */
id?: Key,
/**
* State of the grid list.
*/
state: ListState<unknown>
}
export interface GridListItemProps<T = object> extends RenderProps<GridListItemRenderProps>, LinkDOMProps, HoverEvents, PressEvents, Omit<GlobalDOMAttributes<HTMLDivElement>, 'onClick'> {
/**
* The CSS [className](https://developer.mozilla.org/en-US/docs/Web/API/Element/className) for the element. A function may be provided to compute the class based on component state.
* @default 'react-aria-GridListItem'
*/
className?: ClassNameOrFunction<GridListItemRenderProps>,
/** The unique id of the item. */
id?: Key,
/** The object value that this item represents. When using dynamic collections, this is set automatically. */
value?: T,
/** A string representation of the item's contents, used for features like typeahead. */
textValue?: string,
/** Whether the item is disabled. */
isDisabled?: boolean,
/**
* Handler that is called when a user performs an action on the item. The exact user event depends on
* the collection's `selectionBehavior` prop and the interaction modality.
*/
onAction?: () => void
}
/**
* A GridListItem represents an individual item in a GridList.
*/
export const GridListItem = /*#__PURE__*/ createLeafComponent(ItemNode, function GridListItem<T extends object>(props: GridListItemProps<T>, forwardedRef: ForwardedRef<HTMLDivElement>, item: Node<T>) {
let state = useContext(ListStateContext)!;
let {dragAndDropHooks, dragState, dropState} = useContext(DragAndDropContext);
let ref = useObjectRef<HTMLDivElement>(forwardedRef);
let {isVirtualized} = useContext(CollectionRendererContext);
let {rowProps, gridCellProps, descriptionProps, ...states} = useGridListItem(
{
node: item,
shouldSelectOnPressUp: !!dragState,
isVirtualized
},
state,
ref
);
let {hoverProps, isHovered} = useHover({
isDisabled: !states.allowsSelection && !states.hasAction,
onHoverStart: item.props.onHoverStart,
onHoverChange: item.props.onHoverChange,
onHoverEnd: item.props.onHoverEnd
});
let {isFocusVisible, focusProps} = useFocusRing();
let {checkboxProps} = useGridListSelectionCheckbox(
{key: item.key},
state
);
let buttonProps = state.selectionManager.disabledBehavior === 'all' && states.isDisabled ? {isDisabled: true} : {};
let draggableItem: DraggableItemResult | null = null;
if (dragState && dragAndDropHooks) {
draggableItem = dragAndDropHooks.useDraggableItem!({key: item.key, hasDragButton: true}, dragState);
}
let dropIndicator: DropIndicatorAria | null = null;
let dropIndicatorRef = useRef<HTMLDivElement>(null);
let {visuallyHiddenProps} = useVisuallyHidden();
if (dropState && dragAndDropHooks) {
dropIndicator = dragAndDropHooks.useDropIndicator!({
target: {type: 'item', key: item.key, dropPosition: 'on'}
}, dropState, dropIndicatorRef);
}
let isDragging = dragState && dragState.isDragging(item.key);
let renderProps = useRenderProps({
...props,
id: undefined,
children: item.rendered,
defaultClassName: 'react-aria-GridListItem',
values: {
...states,
isHovered,
isFocusVisible,
selectionMode: state.selectionManager.selectionMode,
selectionBehavior: state.selectionManager.selectionBehavior,
allowsDragging: !!dragState,
isDragging,
isDropTarget: dropIndicator?.isDropTarget,
id: item.key,
state
}
});
let dragButtonRef = useRef<HTMLButtonElement>(null);
useEffect(() => {
if (dragState && !dragButtonRef.current) {
console.warn('Draggable items in a GridList must contain a <Button slot="drag"> element so that keyboard and screen reader users can drag them.');
}
// eslint-disable-next-line
}, []);
useEffect(() => {
if (!item.textValue && process.env.NODE_ENV !== 'production') {
console.warn('A `textValue` prop is required for <GridListItem> elements with non-plain text children in order to support accessibility features such as type to select.');
}
}, [item.textValue]);
let DOMProps = filterDOMProps(props as any, {global: true});
delete DOMProps.id;
delete DOMProps.onClick;
let dragButtonStyle: React.CSSProperties | undefined = dragAndDropHooks?.pointerDragSource === 'dragButton'
? undefined
: {pointerEvents: 'none'};
return (
<>
{dropIndicator && !dropIndicator.isHidden &&
<div role="row" style={{position: 'absolute'}}>
<div role="gridcell">
<div role="button" {...visuallyHiddenProps} {...dropIndicator?.dropIndicatorProps} ref={dropIndicatorRef} />
</div>
</div>
}
<dom.div
{...mergeProps(DOMProps, renderProps, rowProps, focusProps, hoverProps, draggableItem?.dragProps)}
ref={ref}
data-selected={states.isSelected || undefined}
data-disabled={states.isDisabled || undefined}
data-hovered={isHovered || undefined}
data-focused={states.isFocused || undefined}
data-focus-visible={isFocusVisible || undefined}
data-pressed={states.isPressed || undefined}
data-allows-dragging={!!dragState || undefined}
data-dragging={isDragging || undefined}
data-drop-target={dropIndicator?.isDropTarget || undefined}
data-selection-mode={state.selectionManager.selectionMode === 'none' ? undefined : state.selectionManager.selectionMode}>
<div {...gridCellProps} style={{display: 'contents'}}>
<Provider
values={[
[CheckboxContext, {
slots: {
[DEFAULT_SLOT]: {},
selection: checkboxProps
}
}],
[ButtonContext, {
slots: {
[DEFAULT_SLOT]: buttonProps,
drag: {
...draggableItem?.dragButtonProps,
ref: dragButtonRef,
style: dragButtonStyle
}
}
}],
[TextContext, {
slots: {
[DEFAULT_SLOT]: {},
description: descriptionProps
}
}],
[CollectionRendererContext, DefaultCollectionRenderer],
[ListStateContext, null],
[SelectableCollectionContext, null],
[FieldInputContext, null],
[SelectionIndicatorContext, {isSelected: states.isSelected}]
]}>
{renderProps.children}
</Provider>
</div>
</dom.div>
</>
);
});
function GridListDropIndicatorWrapper(props: DropIndicatorProps, ref: ForwardedRef<HTMLElement>) {
ref = useObjectRef(ref);
let {dragAndDropHooks, dropState} = useContext(DragAndDropContext);
let buttonRef = useRef<HTMLDivElement>(null);
let {dropIndicatorProps, isHidden, isDropTarget} = dragAndDropHooks!.useDropIndicator!(
props,
dropState!,
buttonRef
);
if (isHidden) {
return null;
}
return (
<GridListDropIndicatorForwardRef {...props} dropIndicatorProps={dropIndicatorProps} isDropTarget={isDropTarget} buttonRef={buttonRef} ref={ref} />
);
}
interface GridListDropIndicatorProps extends DropIndicatorProps {
dropIndicatorProps: React.HTMLAttributes<HTMLElement>,
isDropTarget: boolean,
buttonRef: RefObject<HTMLDivElement | null>
}
function GridListDropIndicator(props: GridListDropIndicatorProps, ref: ForwardedRef<HTMLElement>) {
let {
dropIndicatorProps,
isDropTarget,
buttonRef,
...otherProps
} = props;
let {visuallyHiddenProps} = useVisuallyHidden();
let renderProps = useRenderProps({
...otherProps,
defaultClassName: 'react-aria-DropIndicator',
values: {
isDropTarget
}
});
return (
<dom.div
{...renderProps}
role="row"
ref={ref as RefObject<HTMLDivElement | null>}
data-drop-target={isDropTarget || undefined}>
<div role="gridcell">
<div {...visuallyHiddenProps} role="button" {...dropIndicatorProps} ref={buttonRef} />
{renderProps.children}
</div>
</dom.div>
);
}
const GridListDropIndicatorForwardRef = forwardRef(GridListDropIndicator);
function RootDropIndicator() {
let {dragAndDropHooks, dropState} = useContext(DragAndDropContext);
let ref = useRef<HTMLDivElement>(null);
let {dropIndicatorProps} = dragAndDropHooks!.useDropIndicator!({
target: {type: 'root'}
}, dropState!, ref);
let isDropTarget = dropState!.isDropTarget({type: 'root'});
let {visuallyHiddenProps} = useVisuallyHidden();
if (!isDropTarget && dropIndicatorProps['aria-hidden']) {
return null;
}
return (
<div role="row" aria-hidden={dropIndicatorProps['aria-hidden']} style={{position: 'absolute'}}>
<div role="gridcell">
<div role="button" {...visuallyHiddenProps} {...dropIndicatorProps} ref={ref} />
</div>
</div>
);
}
export interface GridListLoadMoreItemProps extends Omit<LoadMoreSentinelProps, 'collection'>, StyleProps, DOMRenderProps<'div', undefined>, GlobalDOMAttributes<HTMLDivElement> {
/**
* The CSS [className](https://developer.mozilla.org/en-US/docs/Web/API/Element/className) for the element.
* @default 'react-aria-GridListLoadMoreItem'
*/
className?: string,
/**
* The load more spinner to render when loading additional items.
*/
children?: ReactNode,
/**
* Whether or not the loading spinner should be rendered or not.
*/
isLoading?: boolean
}
export const GridListLoadMoreItem = createLeafComponent(LoaderNode, function GridListLoadingIndicator(props: GridListLoadMoreItemProps, ref: ForwardedRef<HTMLDivElement>, item: Node<object>) {
let state = useContext(ListStateContext)!;
let {isVirtualized} = useContext(CollectionRendererContext);
let {isLoading, onLoadMore, scrollOffset, ...otherProps} = props;
let sentinelRef = useRef(null);
let memoedLoadMoreProps = useMemo(() => ({
onLoadMore,
collection: state?.collection,
sentinelRef,
scrollOffset
}), [onLoadMore, scrollOffset, state?.collection]);
useLoadMoreSentinel(memoedLoadMoreProps, sentinelRef);
let renderProps = useRenderProps({
...otherProps,
id: undefined,
children: item.rendered,
defaultClassName: 'react-aria-GridListLoadingIndicator',
values: undefined
});
// For now don't include aria-posinset and aria-setsize on loader since they aren't keyboard focusable
// Arguably shouldn't include them ever since it might be confusing to the user to include the loaders as part of the
// item count
return (
<>
{/* Alway render the sentinel. For now onus is on the user for styling when using flex + gap (this would introduce a gap even though it doesn't take room) */}
{/* @ts-ignore - compatibility with React < 19 */}
<div style={{position: 'relative', width: 0, height: 0}} inert={inertValue(true)} >
<div data-testid="loadMoreSentinel" ref={sentinelRef} style={{position: 'absolute', height: 1, width: 1}} />
</div>
{isLoading && renderProps.children && (
<dom.div
{...renderProps}
{...filterDOMProps(props, {global: true})}
role="row"
ref={ref}>
<div
aria-colindex={isVirtualized ? 1 : undefined}
role="gridcell">
{renderProps.children}
</div>
</dom.div>
)}
</>
);
});
export interface GridListSectionProps<T> extends SectionProps<T>, DOMRenderProps<'div', undefined> {
/**
* The CSS [className](https://developer.mozilla.org/en-US/docs/Web/API/Element/className) for the element.
* @default 'react-aria-GridListSection'
*/
className?: string
}
/**
* A GridListSection represents a section within a GridList.
*/
export const GridListSection = /*#__PURE__*/ createBranchComponent(SectionNode, <T extends object>(props: GridListSectionProps<T>, ref: ForwardedRef<HTMLDivElement>, item: Node<T>) => {
let state = useContext(ListStateContext)!;
let {CollectionBranch} = useContext(CollectionRendererContext);
let headingRef = useRef(null);
ref = useObjectRef<HTMLDivElement>(ref);
let {rowHeaderProps, rowProps, rowGroupProps} = useGridListSection({
'aria-label': props['aria-label'] ?? undefined
}, state, ref);
let renderProps = useRenderProps({
...props,
id: undefined,
children: undefined,
defaultClassName: 'react-aria-GridListSection',
values: undefined
});
let DOMProps = filterDOMProps(props as any, {global: true});
delete DOMProps.id;
return (
<dom.div
{...mergeProps(DOMProps, renderProps, rowGroupProps)}
ref={ref}>
<Provider
values={[
[GridListHeaderContext, {...rowProps, ref: headingRef}],
[GridListHeaderInnerContext, {...rowHeaderProps}]
]}>
<CollectionBranch
collection={state.collection}
parent={item} />
</Provider>
</dom.div>
);
});
export interface GridListHeaderProps extends DOMRenderProps<'div', undefined>, DOMProps, GlobalDOMAttributes<HTMLElement> {}
export const GridListHeaderContext = createContext<ContextValue<GridListHeaderProps, HTMLDivElement>>({});
export const GridListHeaderInnerContext = createContext<HTMLAttributes<HTMLElement> | null>(null);
export const GridListHeader = /*#__PURE__*/ createLeafComponent(HeaderNode, function Header(props: GridListHeaderProps, ref: ForwardedRef<HTMLDivElement>) {
[props, ref] = useContextProps(props, ref, GridListHeaderContext);
let rowHeaderProps = useContext(GridListHeaderInnerContext);
return (
<dom.div render={props.render} className="react-aria-GridListHeader" ref={ref} {...props}>
<div {...rowHeaderProps} style={{display: 'contents'}}>
{props.children}
</div>
</dom.div>
);
});