123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- import {Fragment, useMemo} from 'react';
- import type {AriaListBoxSectionProps} from '@react-aria/listbox';
- import {useListBoxSection} from '@react-aria/listbox';
- import {useSeparator} from '@react-aria/separator';
- import type {ListState} from '@react-stately/list';
- import type {Node} from '@react-types/shared';
- import type {FormSize} from 'sentry/utils/theme';
- import {
- SectionGroup,
- SectionHeader,
- SectionSeparator,
- SectionTitle,
- SectionWrap,
- } from '../styles';
- import type {SelectKey, SelectSection} from '../types';
- import {SectionToggle} from '../utils';
- import {ListBoxOption} from './option';
- interface ListBoxSectionProps extends AriaListBoxSectionProps {
- hiddenOptions: Set<SelectKey>;
- item: Node<any>;
- listState: ListState<any>;
- size: FormSize;
- onToggle?: (section: SelectSection<SelectKey>, type: 'select' | 'unselect') => void;
- }
- /**
- * A <li /> element that functions as a list box section (renders a nested <ul />
- * inside). https://react-spectrum.adobe.com/react-aria/useListBox.html
- */
- export function ListBoxSection({
- item,
- listState,
- onToggle,
- size,
- hiddenOptions,
- }: ListBoxSectionProps) {
- const {itemProps, headingProps, groupProps} = useListBoxSection({
- heading: item.rendered,
- 'aria-label': item['aria-label'],
- });
- const {separatorProps} = useSeparator({elementType: 'li'});
- const showToggleAllButton =
- listState.selectionManager.selectionMode === 'multiple' &&
- item.value.showToggleAllButton;
- const childItems = useMemo(
- () => [...item.childNodes].filter(child => !hiddenOptions.has(child.props.value)),
- [item.childNodes, hiddenOptions]
- );
- return (
- <Fragment>
- <SectionSeparator {...separatorProps} />
- <SectionWrap {...itemProps}>
- {(item.rendered || showToggleAllButton) && (
- <SectionHeader>
- {item.rendered && (
- <SectionTitle {...headingProps}>{item.rendered}</SectionTitle>
- )}
- {showToggleAllButton && (
- <SectionToggle item={item} listState={listState} onToggle={onToggle} />
- )}
- </SectionHeader>
- )}
- <SectionGroup {...groupProps}>
- {childItems.map(child => (
- <ListBoxOption
- key={child.key}
- item={child}
- listState={listState}
- size={size}
- />
- ))}
- </SectionGroup>
- </SectionWrap>
- </Fragment>
- );
- }
|