| 48 | * of items from props, and manages multiple selection state. |
| 49 | */ |
| 50 | export function useListState<T>(props: ListProps<T>): ListState<T> { |
| 51 | let {filter, layoutDelegate} = props; |
| 52 | |
| 53 | let selectionState = useMultipleSelectionState(props); |
| 54 | let disabledKeys = useMemo( |
| 55 | () => (props.disabledKeys ? new Set(props.disabledKeys) : new Set<Key>()), |
| 56 | [props.disabledKeys] |
| 57 | ); |
| 58 | |
| 59 | let factory = useCallback( |
| 60 | nodes => |
| 61 | filter ? new ListCollection(filter(nodes)) : new ListCollection(nodes as Iterable<Node<T>>), |
| 62 | [filter] |
| 63 | ); |
| 64 | let context = useMemo( |
| 65 | () => ({suppressTextValueWarning: props.suppressTextValueWarning}), |
| 66 | [props.suppressTextValueWarning] |
| 67 | ); |
| 68 | |
| 69 | let collection = useCollection(props, factory, context); |
| 70 | |
| 71 | let selectionManager = useMemo( |
| 72 | () => new SelectionManager(collection, selectionState, {layoutDelegate}), |
| 73 | [collection, selectionState, layoutDelegate] |
| 74 | ); |
| 75 | |
| 76 | useFocusedKeyReset(collection, selectionManager); |
| 77 | |
| 78 | return { |
| 79 | collection, |
| 80 | disabledKeys, |
| 81 | selectionManager |
| 82 | }; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Filters a collection using the provided filter function and returns a new ListState. |