-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathcollapsible.ts
134 lines (117 loc) · 3.7 KB
/
collapsible.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import type { PropType, Ref } from 'vue'
import { computed, defineComponent, h, toRefs, useModel } from 'vue'
import type { ElementType, PrimitiveProps } from '@oku-ui/primitive'
import { createProvideScope } from '@oku-ui/provide'
import { Primitive, primitiveProps } from '@oku-ui/primitive'
import { useControllable, useForwardRef, useId } from '@oku-ui/use-composable'
import type { ScopeCollapsible } from './utils'
import { getState, scopeCollapsibleProps } from './utils'
export type CollapsibleIntrinsicElement = ElementType<'div'>
export type CollapsibleElement = HTMLDivElement
export type CollapsibleProvideValue = {
contentId: Ref<string>
disabled?: Ref<boolean | undefined>
open: Ref<boolean>
onOpenToggle(): void
}
export interface CollapsibleProps extends PrimitiveProps {
defaultOpen?: boolean
open?: boolean
disabled?: boolean
}
export type CollapsibleEmits = {
'update:modelValue': [open: boolean]
'openChange': [open: boolean]
}
export const collapsibleProps = {
props: {
modelValue: {
type: [Boolean, undefined] as PropType<
boolean
>,
default: undefined,
},
defaultOpen: {
type: Boolean as PropType<boolean | undefined>,
default: undefined,
},
open: {
type: Boolean as PropType<boolean | undefined>,
default: undefined,
},
disabled: {
type: Boolean as PropType<boolean | undefined>,
default: undefined,
},
},
emits: {
// eslint-disable-next-line unused-imports/no-unused-vars
'update:modelValue': (open: boolean) => true,
// eslint-disable-next-line unused-imports/no-unused-vars
'openChange': (open: boolean) => true,
},
}
const COLLAPSIBLE_NAME = 'OkuCollapsible'
export const [createCollapsibleProvide, createCollapsibleScope] = createProvideScope(COLLAPSIBLE_NAME)
export const [collapsibleProvider, useCollapsibleInject]
= createCollapsibleProvide<CollapsibleProvideValue>(COLLAPSIBLE_NAME)
const collapsible = defineComponent({
name: COLLAPSIBLE_NAME,
inheritAttrs: false,
props: {
...collapsibleProps.props,
...primitiveProps,
...scopeCollapsibleProps,
},
emits: collapsibleProps.emits,
setup(props, { attrs, slots, emit }) {
const { ...collapsibleAttr } = attrs as CollapsibleIntrinsicElement
const { disabled, open, defaultOpen } = toRefs(props)
const modelValue = useModel(props, 'modelValue')
const forwardedRef = useForwardRef()
const proxyOpen = computed({
get: () => modelValue.value !== undefined
? modelValue.value
: open.value !== undefined ? open.value : undefined,
set: () => {
},
})
const { state, updateValue } = useControllable({
prop: computed(() => proxyOpen.value),
defaultProp: computed(() => defaultOpen.value),
onChange: (open) => {
emit('update:modelValue', open as boolean)
emit('openChange', open as boolean)
},
initialValue: false,
})
collapsibleProvider({
contentId: computed(() => useId()),
disabled,
onOpenToggle() {
updateValue(!state.value)
},
scope: props.scopeOkuCollapsible,
open: computed(() => state.value || false),
})
const originalReturn = () => h(
Primitive.div,
{
'data-state': getState(state.value),
'data-disabled': disabled.value ? '' : undefined,
'ref': forwardedRef,
'asChild': props.asChild,
...collapsibleAttr,
},
{
default: () => slots.default?.(),
},
)
return originalReturn
},
})
// TODO: https://github.com/vuejs/core/pull/7444 after delete
export const OkuCollapsible = collapsible as typeof collapsible &
(new () => {
$props: ScopeCollapsible<Partial<CollapsibleElement>>
})