11import * as React from "react"
22
33import type {
4+ DataListAllDescriptors ,
45 DataListDescriptor ,
5- DataListDescriptors as DataListDescriptorsType ,
6+ DataListDescriptorIndexes ,
7+ DescriptorId ,
68} from "./data-list-types"
79import {
810 DataListDescriptorContextProvider ,
@@ -13,46 +15,70 @@ import {
1315export interface DataListDescriptorsProps < TRenderItem >
1416 extends Omit <
1517 DataListDescriptorContextProviderContext ,
16- "children" | "attachDescriptors" | "updateDescriptorIndex "
18+ "children" | "attachDescriptors" | "markForIndex "
1719 > {
18- descriptors : React . ReactNode
19- setDescriptors : React . Dispatch < React . SetStateAction < DataListDescriptorsType < TRenderItem > > >
20+ rowChildren : React . ReactNode
21+ setAllDescriptors : React . Dispatch < React . SetStateAction < DataListAllDescriptors < TRenderItem > > >
22+ setDescriptorIndexes : React . Dispatch < React . SetStateAction < DataListDescriptorIndexes > >
2023}
2124
2225export function DataListDescriptors < TRenderItem > ( {
23- descriptors,
24- setDescriptors,
26+ rowChildren,
27+ setAllDescriptors,
28+ setDescriptorIndexes : outerSetDescriptorIndexes ,
2529} : DataListDescriptorsProps < TRenderItem > ) {
2630 const attachDescriptors : DataListDescriptorContextProviderContext [ "attachDescriptors" ] =
2731 React . useCallback (
28- ( index : number , additionalDescriptors : Array < DataListDescriptor < TRenderItem > > ) => {
29- setDescriptors ( ( prevDescriptors ) => {
30- const newDescriptors = new Map ( prevDescriptors )
31- newDescriptors . set ( index , additionalDescriptors )
32+ ( id : DescriptorId , rowDescriptors : Array < DataListDescriptor < TRenderItem > > ) => {
33+ setAllDescriptors ( ( prevAllDescriptors ) => {
34+ const newDescriptors = new Map ( prevAllDescriptors )
35+ newDescriptors . set ( id , rowDescriptors )
3236 return newDescriptors
3337 } )
3438
35- // console.log(
36- // "attachDescriptors",
37- // index,
38- // additionalDescriptors.map((v) => v.item)
39- // )
40-
4139 return ( ) => {
42- setDescriptors ( ( prevDescriptors ) => {
43- const newDescriptors = new Map ( prevDescriptors )
44- newDescriptors . delete ( index )
40+ setAllDescriptors ( ( prevAllDescriptors ) => {
41+ const newDescriptors = new Map ( prevAllDescriptors )
42+ newDescriptors . delete ( id )
4543 return newDescriptors
4644 } )
4745 }
4846 } ,
49- [ setDescriptors ]
47+ [ setAllDescriptors ]
48+ )
49+
50+ const markForIndex : DataListDescriptorContextProviderContext [ "markForIndex" ] =
51+ React . useCallback (
52+ ( id : DescriptorId , index : number ) => {
53+ outerSetDescriptorIndexes ( ( prevDescriptorIndexes ) => {
54+ const newDescriptorsIndex = new Map ( prevDescriptorIndexes )
55+ newDescriptorsIndex . set ( index , id )
56+ return newDescriptorsIndex
57+ } )
58+
59+ return ( ) => {
60+ outerSetDescriptorIndexes ( ( prevDescriptorIndexes ) => {
61+ const currentIdAtIndex = prevDescriptorIndexes . get ( index )
62+
63+ // Already used
64+ if ( currentIdAtIndex !== id ) return prevDescriptorIndexes
65+
66+ const newDescriptorsIndex = new Map ( prevDescriptorIndexes )
67+ newDescriptorsIndex . delete ( index )
68+ return newDescriptorsIndex
69+ } )
70+ }
71+ } ,
72+ [ outerSetDescriptorIndexes ]
5073 )
5174
5275 return (
53- < DataListDescriptorContextProvider attachDescriptors = { attachDescriptors } >
76+ < DataListDescriptorContextProvider
77+ attachDescriptors = { attachDescriptors }
78+ markForIndex = { markForIndex }
79+ >
5480 < DataListDescriptorDescendantContextProvider >
55- { descriptors }
81+ { rowChildren }
5682 </ DataListDescriptorDescendantContextProvider >
5783 </ DataListDescriptorContextProvider >
5884 )
0 commit comments