|
9 | 9 | * OF ANY KIND, either express or implied. See the License for the specific language
|
10 | 10 | * governing permissions and limitations under the License.
|
11 | 11 | */
|
12 |
| -import {AriaGridListProps, DraggableItemResult, DragPreviewRenderer, DropIndicatorAria, DroppableCollectionResult, FocusScope, ListKeyboardDelegate, mergeProps, useCollator, useFocusRing, useGridList, useGridListItem, useGridListSelectionCheckbox, useHover, useLocale, useVisuallyHidden} from 'react-aria'; |
| 12 | +import {AriaGridListProps, DraggableItemResult, DragPreviewRenderer, DropIndicatorAria, DroppableCollectionResult, FocusScope, ListKeyboardDelegate, mergeProps, useCollator, useFocusRing, useGridList, useGridListItem, useGridListSection, useGridListSelectionCheckbox, useHover, useLocale, useVisuallyHidden} from 'react-aria'; |
13 | 13 | import {ButtonContext} from './Button';
|
14 | 14 | import {CheckboxContext} from './RSPContexts';
|
15 |
| -import {Collection, CollectionBuilder, createLeafComponent} from '@react-aria/collections'; |
16 |
| -import {CollectionProps, CollectionRendererContext, DefaultCollectionRenderer, ItemRenderProps} from './Collection'; |
| 15 | +import {Collection, CollectionBuilder, createBranchComponent, createLeafComponent} from '@react-aria/collections'; |
| 16 | +import {CollectionProps, CollectionRendererContext, DefaultCollectionRenderer, ItemRenderProps, SectionProps} from './Collection'; |
17 | 17 | import {ContextValue, DEFAULT_SLOT, Provider, RenderProps, SlotProps, StyleProps, StyleRenderProps, useContextProps, useRenderProps} from './utils';
|
18 | 18 | import {DragAndDropContext, DropIndicatorContext, DropIndicatorProps, useDndPersistedKeys, useRenderDropIndicator} from './DragAndDrop';
|
19 | 19 | import {DragAndDropHooks} from './useDragAndDrop';
|
20 | 20 | import {DraggableCollectionState, DroppableCollectionState, Collection as ICollection, ListState, Node, SelectionBehavior, useListState} from 'react-stately';
|
21 | 21 | import {filterDOMProps, inertValue, LoadMoreSentinelProps, useLoadMoreSentinel, useObjectRef} from '@react-aria/utils';
|
22 | 22 | import {forwardRefType, GlobalDOMAttributes, HoverEvents, Key, LinkDOMProps, PressEvents, RefObject} from '@react-types/shared';
|
| 23 | +import {HeaderContext} from './Header'; |
23 | 24 | import {ListStateContext} from './ListBox';
|
24 | 25 | import React, {createContext, ForwardedRef, forwardRef, HTMLAttributes, JSX, ReactNode, useContext, useEffect, useMemo, useRef} from 'react';
|
25 | 26 | import {TextContext} from './Text';
|
@@ -561,3 +562,58 @@ export const GridListLoadMoreItem = createLeafComponent('loader', function GridL
|
561 | 562 | </>
|
562 | 563 | );
|
563 | 564 | });
|
| 565 | + |
| 566 | +export interface GridListSectionProps<T> extends SectionProps<T> {} |
| 567 | + |
| 568 | +/** |
| 569 | + * A GridListSection represents a section within a GridList. |
| 570 | + */ |
| 571 | +export const GridListSection = /*#__PURE__*/ createBranchComponent('section', <T extends object>(props: GridListSectionProps<T>, ref: ForwardedRef<HTMLElement>, item: Node<T>) => { |
| 572 | + let state = useContext(ListStateContext)!; |
| 573 | + let {CollectionBranch} = useContext(CollectionRendererContext); |
| 574 | + let headingRef = useRef(null); |
| 575 | + ref = useObjectRef<HTMLElement>(ref); |
| 576 | + let {rowHeaderProps, rowProps, rowGroupProps} = useGridListSection({ |
| 577 | + 'aria-label': props['aria-label'] ?? undefined |
| 578 | + }, state, ref); |
| 579 | + let renderProps = useRenderProps({ |
| 580 | + defaultClassName: 'react-aria-GridListSection', |
| 581 | + className: props.className, |
| 582 | + style: props.style, |
| 583 | + values: {} |
| 584 | + }); |
| 585 | + |
| 586 | + let DOMProps = filterDOMProps(props as any, {global: true}); |
| 587 | + delete DOMProps.id; |
| 588 | + |
| 589 | + return ( |
| 590 | + <section |
| 591 | + {...mergeProps(DOMProps, renderProps, rowGroupProps)} |
| 592 | + ref={ref}> |
| 593 | + <Provider |
| 594 | + values={[ |
| 595 | + [HeaderContext, {...rowProps, ref: headingRef}], |
| 596 | + [GridListHeaderContext, {...rowHeaderProps}] |
| 597 | + ]}> |
| 598 | + <CollectionBranch |
| 599 | + collection={state.collection} |
| 600 | + parent={item} /> |
| 601 | + </Provider> |
| 602 | + </section> |
| 603 | + ); |
| 604 | +}); |
| 605 | + |
| 606 | +const GridListHeaderContext = createContext<HTMLAttributes<HTMLElement> | null>(null); |
| 607 | + |
| 608 | +export const GridListHeader = /*#__PURE__*/ createLeafComponent('header', function Header(props: HTMLAttributes<HTMLElement>, ref: ForwardedRef<HTMLElement>) { |
| 609 | + [props, ref] = useContextProps(props, ref, HeaderContext); |
| 610 | + let rowHeaderProps = useContext(GridListHeaderContext); |
| 611 | + |
| 612 | + return ( |
| 613 | + <header {...props} ref={ref}> |
| 614 | + <div {...rowHeaderProps} style={{display: 'contents'}}> |
| 615 | + {props.children} |
| 616 | + </div> |
| 617 | + </header> |
| 618 | + ); |
| 619 | +}); |
0 commit comments