-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
SwitchThumb.ts
73 lines (60 loc) · 1.83 KB
/
SwitchThumb.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
import type {
ElementType,
MergeProps,
PrimitiveProps,
} from '@oku-ui/primitive'
import { Primitive } from '@oku-ui/primitive'
import type { Scope } from '@oku-ui/provide'
import { useForwardRef } from '@oku-ui/use-composable'
import type { PropType } from 'vue'
import { defineComponent, h, toRefs, toValue } from 'vue'
import { useSwitchContext } from './Switch'
import { getState } from './util'
const THUMB_NAME = 'SwitchThumb'
type SwitchThumbElement = ElementType<'span'>
export type _SwitchThumbEl = HTMLSpanElement
type SwitchThumbProps = SwitchThumbElement &
PrimitiveProps & {
scopeSwitch?: Scope
}
const SwitchThumb = defineComponent({
name: THUMB_NAME,
inheritAttrs: false,
props: {
scopeSwitch: {
type: Object as unknown as PropType<Scope>,
required: false,
},
asChild: {
type: Boolean,
default: false,
},
},
setup(props, { attrs, slots, expose }) {
const { scopeSwitch } = toRefs(props)
const { ...thumbAttrs } = attrs as SwitchThumbProps
const forwardedRef = useForwardRef()
const context = toValue(useSwitchContext(THUMB_NAME, scopeSwitch.value))
const originalReturn = () =>
h(
Primitive.span,
{
'data-disabled': context.disabled?.value ? '' : undefined,
'data-state': getState(context.checked.value),
'ref': forwardedRef,
'asChild': props.asChild,
...thumbAttrs,
},
{
default: () => slots.default?.(),
},
)
return originalReturn
},
})
type _SwitchThumb = MergeProps<SwitchThumbProps, SwitchThumbElement>
type InstanceSwitchThumbType = InstanceType<typeof SwitchThumb>
const OkuSwitchThumb = SwitchThumb as typeof SwitchThumb &
(new () => { $props: _SwitchThumb })
export { OkuSwitchThumb }
export type { SwitchThumbProps, InstanceSwitchThumbType }