import React, { useEffect, useRef } from 'react' import { Cell, Row } from 'react-sticky-table' import Checkbox from '../Form/Checkbox' import styles from './_treeListBox.module.scss' import Button from '../Button/Button' import Icon from '../Icons/Icon' import ListLoading from '../Loaders/ListLoading' import { type CheckboxListProps, CheckboxState, } from '../Form/NestedCheckboxHelper' const GroupScrollContainer: React.FC<{ maxHeight: number | string hasMore: boolean isFetching: boolean fetchNextPage?: () => void children: React.ReactNode }> = ({ maxHeight, hasMore, isFetching, fetchNextPage, children }) => { const sentinelRef = useRef(null) useEffect(() => { if (!isFetching && sentinelRef.current && hasMore) { const observer = new IntersectionObserver( (entries) => { entries.forEach((entry) => { if (entry.isIntersecting) { fetchNextPage?.() } }) }, { threshold: 0.3 }, ) observer.observe(sentinelRef.current) return () => observer.disconnect() } }, [hasMore, fetchNextPage, isFetching]) return (
{children} {isFetching ? : null} {hasMore ?
: null}
) } const CheckboxList: React.FC = ({ items, getStateForId, idsToRender = [], onClick = () => {}, expandedItems = {}, onToggleExpand = () => {}, indentLevel = 0, tableConfig, idKey, parentKey, displayKey, pathKey, className = '', groupScrollConfig, }) => { const indentSize = 24 if (!idsToRender.length) { const uniqueIds = new Set( items.filter((i) => !i[parentKey]).map((i) => i[idKey]), ) idsToRender = Array.from(uniqueIds) } const getChildNodes = (parentId: string | number) => { const seenIds = new Set() const nodeItems = items.filter((i) => { return ( i[parentKey] === parentId && !seenIds.has(i[idKey]) && seenIds.add(i[idKey]) ) }) const groupCfg = groupScrollConfig?.groups?.[parentId] if (!nodeItems.length) { if (groupScrollConfig && groupCfg !== undefined && groupCfg.isFetching) { return (
) } return null } const childList = ( i[idKey])} onClick={onClick} expandedItems={expandedItems} onToggleExpand={onToggleExpand} getStateForId={getStateForId} indentLevel={indentLevel + 1} tableConfig={tableConfig} idKey={idKey} parentKey={parentKey} displayKey={displayKey} pathKey={pathKey} className={className} groupScrollConfig={groupScrollConfig} /> ) if (groupScrollConfig && groupCfg !== undefined) { const maxHeight = groupScrollConfig.maxHeight ?? '200px' return ( {childList} ) } return childList } return ( <> {idsToRender.map((id) => { const item = items.find((i) => i[idKey] === id)! const checkboxState = getStateForId(id) const isExpanded = expandedItems[id] const hasChildren = items.some((i) => i[parentKey] === id) return ( {tableConfig.map((column, index) => ( {column.dataKey === displayKey ? (
{ onClick(item[idKey]) }} radioSize='12' partialChecked={checkboxState === CheckboxState.PARTIAL} /> {column.children(item, index)}
) : null} {column.dataKey !== displayKey ? column.children(item, index) : null}
))} {hasChildren ? ( ) : null}
{isExpanded ? getChildNodes(item[idKey]) : null}
) })} ) } export default CheckboxList