refactor(Form): input events (#99)

Co-authored-by: Benjamin Canac <canacb1@gmail.com>
This commit is contained in:
Romain Hamel
2024-07-01 20:37:57 +02:00
committed by GitHub
parent ca029a4b6c
commit bad2e49de9
22 changed files with 947 additions and 388 deletions

View File

@@ -45,6 +45,7 @@ export interface SwitchSlots {
export interface SwitchEmits {
(e: 'update:modelValue', payload: boolean): void
(e: 'change', payload: Event): void
}
</script>
@@ -56,14 +57,14 @@ import { useId, useAppConfig, useFormField } from '#imports'
const props = defineProps<SwitchProps>()
const slots = defineSlots<SwitchSlots>()
defineEmits<SwitchEmits>()
const emits = defineEmits<SwitchEmits>()
const modelValue = defineModel<boolean | undefined>({ default: undefined })
const appConfig = useAppConfig()
const rootProps = useForwardProps(reactivePick(props, 'as', 'required', 'value'))
const { id: _id, emitFormChange, size, color, name, disabled } = useFormField<SwitchProps>(props)
const { id: _id, emitFormChange, emitFormInput, size, color, name, disabled } = useFormField<SwitchProps>(props)
const id = _id.value ?? useId()
const ui = computed(() => tv({ extend: switchTv, slots: props.ui })({
@@ -73,6 +74,13 @@ const ui = computed(() => tv({ extend: switchTv, slots: props.ui })({
loading: props.loading,
disabled: disabled.value || props.loading
}))
function onUpdate(value: any) {
const event = new Event('change', { target: { value } })
emits('change', event)
emitFormChange()
emitFormInput()
}
</script>
<template>
@@ -86,7 +94,7 @@ const ui = computed(() => tv({ extend: switchTv, slots: props.ui })({
:name="name"
:disabled="disabled || loading"
:class="ui.base()"
@update:checked="emitFormChange()"
@update:checked="onUpdate"
>
<SwitchThumb :class="ui.thumb()">
<UIcon v-if="loading" :name="loadingIcon || appConfig.ui.icons.loading" :class="ui.icon({ checked: true, unchecked: true })" />