Skip to content

Commit 7bc736f

Browse files
feat: Collection
1 parent 6233e32 commit 7bc736f

File tree

1 file changed

+39
-24
lines changed

1 file changed

+39
-24
lines changed

packages/vue-primitives/src/collection/Collection.ts

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type ShallowReactive, type ShallowRef, onBeforeUnmount, onBeforeUpdate, onMounted, shallowReactive } from 'vue'
1+
import { type ShallowReactive, type ShallowRef, shallowReactive, watchEffect } from 'vue'
22
import { createContext } from '../hooks/createContext.ts'
33

44
export const ITEM_DATA_ATTR = 'data-radix-collection-item'
@@ -23,35 +23,35 @@ export function createCollection<ItemElement extends HTMLElement, ItemData = obj
2323
function useCollectionItem(currentElement: ShallowRef<ItemElement | undefined>, attrs: Record<string, unknown> = {}) {
2424
const { itemMap } = useCollectionContext()
2525

26-
let unrefElement: ItemElement | undefined
26+
// let unrefElement: ItemElement | undefined
2727

28-
onMounted(() => {
29-
unrefElement = currentElement.value
30-
if (!unrefElement)
31-
return
28+
// onMounted(() => {
29+
// unrefElement = currentElement.value
30+
// if (!unrefElement)
31+
// return
3232

33-
itemMap.set(unrefElement, {
34-
ref: unrefElement,
35-
attrs: attrs as ItemData,
36-
})
37-
})
33+
// itemMap.set(unrefElement, {
34+
// ref: unrefElement,
35+
// attrs: attrs as ItemData,
36+
// })
37+
// })
3838

39-
onBeforeUpdate(() => {
40-
if (!unrefElement)
41-
return
39+
// onBeforeUpdate(() => {
40+
// if (!unrefElement)
41+
// return
4242

43-
itemMap.set(unrefElement, {
44-
ref: unrefElement,
45-
attrs: attrs as ItemData,
46-
})
47-
})
43+
// itemMap.set(unrefElement, {
44+
// ref: unrefElement,
45+
// attrs: attrs as ItemData,
46+
// })
47+
// })
4848

49-
onBeforeUnmount(() => {
50-
if (!unrefElement)
51-
return
49+
// onBeforeUnmount(() => {
50+
// if (!unrefElement)
51+
// return
5252

53-
itemMap.delete(unrefElement)
54-
})
53+
// itemMap.delete(unrefElement)
54+
// })
5555

5656
// TODO: watch attrs -> onBeforeUpdate
5757
// watch([currentElement, attrs], (_, __, onClean) => {
@@ -69,6 +69,21 @@ export function createCollection<ItemElement extends HTMLElement, ItemData = obj
6969
// })
7070
// })
7171

72+
watchEffect((onClean) => {
73+
const unrefElement = currentElement.value
74+
if (!unrefElement)
75+
return
76+
77+
itemMap.set(unrefElement, {
78+
ref: unrefElement,
79+
attrs: attrs as ItemData,
80+
})
81+
82+
onClean(() => {
83+
itemMap.delete(unrefElement)
84+
})
85+
})
86+
7287
return {
7388
itemMap,
7489
}

0 commit comments

Comments
 (0)