mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-14 20:19:34 +01:00
39 lines
984 B
TypeScript
39 lines
984 B
TypeScript
import { inject, ref } from 'vue'
|
|
import { type UseEventBusReturn, useDebounceFn } from '@vueuse/core'
|
|
import type { FormEvent, FormEventType } from '../types'
|
|
|
|
export const useFormGroup = () => {
|
|
const formBus = inject<UseEventBusReturn<FormEvent, string> | undefined>('form-events', undefined)
|
|
const formGroup = inject('form-group', undefined)
|
|
|
|
const blurred = ref(false)
|
|
|
|
function emitFormEvent (type: FormEventType, path: string) {
|
|
if (formBus) {
|
|
formBus.emit({ type, path })
|
|
}
|
|
}
|
|
|
|
function emitFormBlur () {
|
|
emitFormEvent('blur', formGroup?.name.value)
|
|
blurred.value = true
|
|
}
|
|
|
|
function emitFormChange () {
|
|
emitFormEvent('change', formGroup?.name.value)
|
|
}
|
|
|
|
const emitFormInput = useDebounceFn(() => {
|
|
if (blurred.value) {
|
|
emitFormEvent('input', formGroup?.name.value)
|
|
}
|
|
}, 300)
|
|
|
|
return {
|
|
emitFormBlur,
|
|
emitFormInput,
|
|
emitFormChange,
|
|
formGroup
|
|
}
|
|
}
|