@@ -10,7 +10,7 @@ export interface CollectionContext {
1010 collectionRef : MutableRefObject < HTMLElement | undefined >
1111}
1212
13- export function createCollection < ItemElement extends HTMLElement , ItemData = object > ( name : string ) {
13+ export function createCollection < ItemElement extends HTMLElement , ItemData = Record < string , any > > ( name : string ) {
1414 const [ _provideCollectionContext , useCollectionContext ] = createContext < CollectionContext > ( `${ name } CollectionProvider` )
1515
1616 type CollectionItem = ItemElementWithData < ItemElement , ItemData >
@@ -26,23 +26,35 @@ export function createCollection<ItemElement extends HTMLElement, ItemData = obj
2626 return context
2727 }
2828
29- function useCollectionItem ( currentElement : ItemElement | undefined , attrs : ItemData ) {
29+ function useCollectionItem < K extends keyof ItemData > ( currentElement : ItemElement | undefined , attrs : ItemData [ K ] , key : K ) {
3030 const unrefElement = currentElement as CollectionItem | undefined
31- if ( ! unrefElement || '$$rcid' in unrefElement )
31+ if ( ! unrefElement )
3232 return
3333
34- ( unrefElement as any ) . $$rcid = attrs
34+ if ( '$$rcid' in unrefElement ) {
35+ if ( ! key )
36+ return
37+ if ( key in unrefElement )
38+ return
39+ }
40+
41+ if ( key ) {
42+ ( unrefElement as any ) . $$rcid = ( unrefElement as any ) . $$rcid || { }
43+ ; ( unrefElement as any ) . $$rcid [ key ] = attrs
44+ }
45+ else {
46+ ( unrefElement as any ) . $$rcid = attrs
47+ }
3548 }
3649
3750 function useCollection ( thereContext ?: CollectionContext ) {
3851 const context = thereContext || useCollectionContext ( `${ name } CollectionConsumer` )
3952
4053 function getItems ( ) : CollectionItem [ ] {
41- const collectionNode = context . collectionRef . current
42- if ( ! collectionNode )
54+ if ( ! context . collectionRef . current )
4355 return [ ]
4456
45- const orderedNodes = Array . from ( collectionNode . querySelectorAll ( `[${ DATA_COLLECTION_ITEM } ]` ) )
57+ const orderedNodes = Array . from ( context . collectionRef . current . querySelectorAll ( `[${ DATA_COLLECTION_ITEM } ]` ) )
4658
4759 return orderedNodes as CollectionItem [ ]
4860 }
0 commit comments