Skip to content

Commit ab4fd88

Browse files
committed
allow collection to accept state as prop
1 parent e69b468 commit ab4fd88

File tree

1 file changed

+53
-9
lines changed

1 file changed

+53
-9
lines changed

packages/react/collection/src/collection.tsx

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function createCollection<
4646
setItemMap: React.Dispatch<React.SetStateAction<ItemMap<ItemElement, ItemData>>>;
4747
};
4848

49-
const [CollectionProviderImpl, useCollectionContext] = createCollectionContext<ContextValue>(
49+
const [CollectionContextProvider, useCollectionContext] = createCollectionContext<ContextValue>(
5050
PROVIDER_NAME,
5151
{
5252
collectionElement: null,
@@ -57,14 +57,45 @@ function createCollection<
5757
}
5858
);
5959

60-
const CollectionProvider: React.FC<{ children?: React.ReactNode; scope: any }> = (props) => {
61-
const { scope, children } = props;
60+
type CollectionState = [
61+
ItemMap: ItemMap<ItemElement, ItemData>,
62+
SetItemMap: React.Dispatch<React.SetStateAction<ItemMap<ItemElement, ItemData>>>,
63+
];
64+
65+
const CollectionProvider: React.FC<{
66+
children?: React.ReactNode;
67+
scope: any;
68+
state?: CollectionState;
69+
}> = ({ state, ...props }) => {
70+
return state ? (
71+
<CollectionProviderImpl {...props} state={state} />
72+
) : (
73+
<CollectionInit {...props} />
74+
);
75+
};
76+
CollectionProvider.displayName = PROVIDER_NAME;
77+
78+
const CollectionInit: React.FC<{
79+
children?: React.ReactNode;
80+
scope: any;
81+
}> = (props) => {
82+
const state = useInitCollection();
83+
return <CollectionProviderImpl {...props} state={state} />;
84+
};
85+
CollectionInit.displayName = PROVIDER_NAME + 'Init';
86+
87+
const CollectionProviderImpl: React.FC<{
88+
children?: React.ReactNode;
89+
scope: any;
90+
state: CollectionState;
91+
}> = (props) => {
92+
const { scope, children, state } = props;
6293
const ref = React.useRef<CollectionElement>(null);
6394
const [collectionElement, setCollectionElement] = React.useState<CollectionElement | null>(
6495
null
6596
);
6697
const composeRefs = useComposedRefs(ref, setCollectionElement);
67-
const [itemMap, setItemMap] = React.useState<ItemMap<ItemElement, ItemData>>(new OrderedDict());
98+
const [itemMap, setItemMap] = state;
6899

69100
React.useEffect(() => {
70101
if (!collectionElement) return;
@@ -98,7 +129,7 @@ function createCollection<
98129
}, [collectionElement]);
99130

100131
return (
101-
<CollectionProviderImpl
132+
<CollectionContextProvider
102133
scope={scope}
103134
itemMap={itemMap}
104135
setItemMap={setItemMap}
@@ -107,11 +138,11 @@ function createCollection<
107138
collectionElement={collectionElement}
108139
>
109140
{children}
110-
</CollectionProviderImpl>
141+
</CollectionContextProvider>
111142
);
112143
};
113144

114-
CollectionProvider.displayName = PROVIDER_NAME;
145+
CollectionProviderImpl.displayName = PROVIDER_NAME + 'Impl';
115146

116147
/* -----------------------------------------------------------------------------------------------
117148
* CollectionSlot
@@ -198,6 +229,14 @@ function createCollection<
198229

199230
CollectionItemSlot.displayName = ITEM_SLOT_NAME;
200231

232+
/* -----------------------------------------------------------------------------------------------
233+
* useInitCollection
234+
* ---------------------------------------------------------------------------------------------*/
235+
236+
function useInitCollection() {
237+
return React.useState<ItemMap<ItemElement, ItemData>>(new OrderedDict());
238+
}
239+
201240
/* -----------------------------------------------------------------------------------------------
202241
* useCollection
203242
* ---------------------------------------------------------------------------------------------*/
@@ -208,10 +247,15 @@ function createCollection<
208247
return itemMap;
209248
}
210249

250+
const functions = {
251+
createCollectionScope,
252+
useCollection,
253+
useInitCollection,
254+
};
255+
211256
return [
212257
{ Provider: CollectionProvider, Slot: CollectionSlot, ItemSlot: CollectionItemSlot },
213-
useCollection,
214-
createCollectionScope,
258+
functions,
215259
] as const;
216260
}
217261

0 commit comments

Comments
 (0)