Skip to content

Commit 6ed9dcd

Browse files
feat(devtools-kit): enable editing native Map/Set in input field (#253)
1 parent 3a38cfd commit 6ed9dcd

File tree

2 files changed

+24
-2
lines changed
  • packages

2 files changed

+24
-2
lines changed

packages/devtools-kit/src/core/component/state/editor.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,30 @@ export class RefStateEditor {
101101
ref.value = value
102102
}
103103
else {
104+
// Edit on native type Set
105+
if (ref instanceof Set && Array.isArray(value)) {
106+
ref.clear()
107+
value.forEach((v: unknown) => ref.add(v))
108+
return
109+
}
110+
111+
const currentKeys = Object.keys(value)
112+
113+
// Edit on native type Map
114+
if (ref instanceof Map) {
115+
const previousKeysSet = new Set(ref.keys())
116+
currentKeys.forEach((key) => {
117+
ref.set(key, Reflect.get(value, key))
118+
previousKeysSet.delete(key)
119+
})
120+
previousKeysSet.forEach(key => ref.delete(key))
121+
return
122+
}
123+
104124
// if is reactive, then it must be object
105125
// to prevent loss reactivity, we should assign key by key
106126
const previousKeysSet = new Set(Object.keys(ref))
107-
const currentKeys = Object.keys(value)
127+
108128
// we should check the key diffs, if previous key is the longer
109129
// then remove the needless keys
110130
currentKeys.forEach((key) => {

packages/playground/basic/src/stores/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import { computed, ref } from 'vue'
33

44
export const useAppStore = defineStore('app', () => {
55
const count = ref(120)
6+
const map = ref(new Map([['a', 1], ['b', 2]]))
7+
const set = ref(new Set([1, 2, 3]))
68
function increment() {
79
count.value++
810
}
911
const doubledCount = computed(() => count.value * 2)
1012

11-
return { count, doubledCount, increment }
13+
return { count, doubledCount, increment, map, set }
1214
})
1315

1416
export const useCounterStore = defineStore('counter', () => {

0 commit comments

Comments
 (0)