-
-
Notifications
You must be signed in to change notification settings - Fork 173
/
Copy pathselect.ts
64 lines (56 loc) · 1.33 KB
/
select.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import type { Ref } from 'vue'
import { ref } from 'vue'
export function createSelectContext(id: string) {
const selected = ref<string>('')
provide<Ref<string>>(`SelectedSymbol${id}`, selected)
return {
selected,
id,
}
}
function useSelectContext(id: string) {
const selected = inject<Ref<string>>(`SelectedSymbol${id}`)!
return {
selected,
id,
}
}
export function useSelect(groupId: string, id: string, onSelect?: (id: string) => void) {
const { selected } = useSelectContext(groupId)
const isSelected = computed(() => selected.value === id)
const toggleSelected = (id: string) => {
selected.value = id
onSelect?.(id)
}
return {
isSelected,
toggleSelected,
}
}
export function useSelectWithContext(groupId: string, id: string, onSelect?: (id: string) => void) {
const { selected } = useSelectContext(groupId)
const isSelected = computed(() => selected.value === id)
const toggleSelected = (id: string) => {
selected.value = id
onSelect?.(id)
}
return {
isSelected,
toggleSelected,
}
}
export function useDefaultSelect() {
const router = useRouter()
const route = useRoute()
function saveSelectedId(id: string) {
router.push({
params: {
id,
},
})
}
return {
saveSelectedId,
savedSelectedId: route.params.id as string,
}
}