-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
label.ts
48 lines (39 loc) · 1.45 KB
/
label.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
import type { ComponentPublicInstance } from 'vue'
import { computed, defineComponent, h, ref } from 'vue'
import type { ComponentPropsWithoutRef, ElementRef } from '@oku-ui/primitive'
import { Primitive } from '@oku-ui/primitive'
import type { MergeProps } from '@oku-ui/utils'
type PrimitiveLabelProps = ComponentPropsWithoutRef<typeof Primitive.label>
type LabelElement = ElementRef<typeof Primitive.label>
type LabelProps = MergeProps<typeof label, PrimitiveLabelProps>
const NAME = 'Label'
const label = defineComponent({
name: NAME,
inheritAttrs: false,
setup(props, { attrs, slots, expose }) {
const innerRef = ref<ComponentPublicInstance>()
const { ...restAttrs } = attrs as LabelProps
expose({
innerRef: computed(() => innerRef.value?.$el),
})
const originalReturn = () => h(Primitive.label, {
...restAttrs,
ref: innerRef,
onMousedown: (event: MouseEvent) => {
restAttrs.onMousedown?.(event)
// prevent text selection when double clicking label
if (!event.defaultPrevented && event.detail > 1)
event.preventDefault()
},
},
() => slots.default?.(),
)
return originalReturn as unknown as {
innerRef: LabelElement
}
},
})
const OkuLabel = label as typeof label & (new () => { $props: LabelProps })
type OkuLabelElement = Omit<InstanceType<typeof label>, keyof ComponentPublicInstance>
export { OkuLabel }
export type { LabelProps, OkuLabelElement }