|
| 1 | +import type { ReactNode } from 'react' |
| 2 | +import React, { useRef, useLayoutEffect, useState } from 'react' |
| 3 | +import { typedMemo } from './fake.js' |
| 4 | +import { useMultiComboBoxContext } from './fake' |
| 5 | +import { List, Item } from './fake.js' |
| 6 | + |
| 7 | +type ItemsProps<T> = { |
| 8 | + children?: (props: { item: T; index: number; isSelected: boolean }) => ReactNode |
| 9 | +} |
| 10 | + |
| 11 | +const Other = function Other(props) { |
| 12 | + return ( |
| 13 | + <ul> |
| 14 | + {props.items.map(item => { |
| 15 | + ;<li>{item}</li> |
| 16 | + })} |
| 17 | + </ul> |
| 18 | + ) |
| 19 | +} |
| 20 | +const Items = typedMemo(function Items<T>({ children }: ItemsProps<T>) { |
| 21 | + const { |
| 22 | + getMenuProps, |
| 23 | + getItemProps, |
| 24 | + filteredItems, |
| 25 | + customItemsList, |
| 26 | + selectedItems, |
| 27 | + itemToString, |
| 28 | + setItemsList, |
| 29 | + loadItems, |
| 30 | + inputValue, |
| 31 | + isOpen, |
| 32 | + } = useMultiComboBoxContext<T>() |
| 33 | + const customLength = customItemsList.length |
| 34 | + const ref = useRef<HTMLUListElement>(null) |
| 35 | + const [hasMore, setHasMore] = useState(true) |
| 36 | + |
| 37 | + useLayoutEffect(() => { |
| 38 | + if (ref.current && isOpen && loadItems) { |
| 39 | + const root = ref.current |
| 40 | + const target = root.querySelector(':last-child') |
| 41 | + const rootMargin = target ? `${target.clientHeight}px 0px 0px 0px` : '0px' |
| 42 | + const observer = new IntersectionObserver( |
| 43 | + async entries => { |
| 44 | + for (const entry of entries) { |
| 45 | + if (entry.isIntersecting && hasMore) { |
| 46 | + const { items, hasMore } = await loadItems(inputValue) |
| 47 | + |
| 48 | + setHasMore(hasMore) |
| 49 | + setItemsList(prevItems => Array.from(new Set([...prevItems, ...items]))) |
| 50 | + } |
| 51 | + } |
| 52 | + }, |
| 53 | + { |
| 54 | + root, |
| 55 | + rootMargin, |
| 56 | + threshold: 0, |
| 57 | + }, |
| 58 | + ) |
| 59 | + |
| 60 | + if (target) { |
| 61 | + observer.observe(target) |
| 62 | + } |
| 63 | + |
| 64 | + return () => { |
| 65 | + observer.disconnect() |
| 66 | + } |
| 67 | + } |
| 68 | + }, [ |
| 69 | + ref, |
| 70 | + isOpen, |
| 71 | + // Create a new observer after filtering items |
| 72 | + inputValue, |
| 73 | + filteredItems, |
| 74 | + hasMore, |
| 75 | + setItemsList, |
| 76 | + loadItems, |
| 77 | + ]) |
| 78 | + |
| 79 | + return ( |
| 80 | + <List {...getMenuProps({ ref })}> |
| 81 | + {isOpen && |
| 82 | + customItemsList.map((item, index) => ( |
| 83 | + <Item |
| 84 | + isLastCustom={index === customItemsList.length - 1} |
| 85 | + isSelected={selectedItems.some( |
| 86 | + selectedItem => itemToString(selectedItem) === item, |
| 87 | + )} |
| 88 | + key={`${item}-${index}`} |
| 89 | + {...getItemProps({ |
| 90 | + item, |
| 91 | + index, |
| 92 | + })} |
| 93 | + > |
| 94 | + {itemToString(item)} |
| 95 | + </Item> |
| 96 | + ))} |
| 97 | + {isOpen && |
| 98 | + filteredItems.map((item, index) => { |
| 99 | + const isSelected = selectedItems.some( |
| 100 | + selectedItem => itemToString(selectedItem) === itemToString(item), |
| 101 | + ) |
| 102 | + |
| 103 | + return ( |
| 104 | + <Item |
| 105 | + isSelected={isSelected} |
| 106 | + key={`${itemToString(item)}-${index}`} |
| 107 | + {...getItemProps({ |
| 108 | + item, |
| 109 | + index: customLength + index, |
| 110 | + })} |
| 111 | + > |
| 112 | + {children ? children({ item, index, isSelected }) : itemToString(item)} |
| 113 | + </Item> |
| 114 | + ) |
| 115 | + })} |
| 116 | + </List> |
| 117 | + ) |
| 118 | +}) |
| 119 | + |
| 120 | +export type { ItemsProps } |
| 121 | +export { Items, Other } |
0 commit comments