chore: uniformize defineModel placement and emits

This commit is contained in:
Benjamin Canac
2024-04-27 22:16:41 +02:00
parent def5f7c10b
commit ce28e662d6
7 changed files with 21 additions and 22 deletions

View File

@@ -39,14 +39,14 @@ import { useId, useAppConfig, useFormField } from '#imports'
const props = defineProps<CheckboxProps>() const props = defineProps<CheckboxProps>()
defineSlots<CheckboxSlots>() defineSlots<CheckboxSlots>()
const modelValue = defineModel<boolean | undefined>({ default: undefined })
const rootProps = useForwardProps(reactivePick(props, 'as', 'required', 'value')) const rootProps = useForwardProps(reactivePick(props, 'as', 'required', 'value'))
const appConfig = useAppConfig() const appConfig = useAppConfig()
const { inputId: _inputId, emitFormChange, size, color, name, disabled } = useFormField<CheckboxProps>(props) const { inputId: _inputId, emitFormChange, size, color, name, disabled } = useFormField<CheckboxProps>(props)
const inputId = _inputId.value ?? useId() const inputId = _inputId.value ?? useId()
const modelValue = defineModel<boolean | undefined>({ default: undefined })
const indeterminate = computed(() => (modelValue.value === undefined && props.indeterminate)) const indeterminate = computed(() => (modelValue.value === undefined && props.indeterminate))
const checked = computed({ const checked = computed({

View File

@@ -32,10 +32,11 @@ import { computed, provide } from 'vue'
import { Primitive } from 'radix-vue' import { Primitive } from 'radix-vue'
import { useAvatarGroup } from '#imports' import { useAvatarGroup } from '#imports'
const show = defineModel<boolean>('show', { default: true })
const props = withDefaults(defineProps<ChipProps>(), { as: 'div' }) const props = withDefaults(defineProps<ChipProps>(), { as: 'div' })
defineSlots<ChipSlots>() defineSlots<ChipSlots>()
const show = defineModel<boolean>('show', { default: true })
const { size } = useAvatarGroup(props) const { size } = useAvatarGroup(props)
const ui = computed(() => tv({ extend: chip, slots: props.ui })({ const ui = computed(() => tv({ extend: chip, slots: props.ui })({

View File

@@ -43,7 +43,7 @@ const props = withDefaults(defineProps<FormProps<T>>(), {
}, },
validateOnInputDelay: 300 validateOnInputDelay: 300
}) })
const emit = defineEmits<FormEmits<T>>() const emits = defineEmits<FormEmits<T>>()
defineSlots<FormSlots>() defineSlots<FormSlots>()
const formId = props.id ?? useId() const formId = props.id ?? useId()
@@ -165,7 +165,7 @@ async function onSubmit(payload: Event) {
...event, ...event,
data: props.state data: props.state
} }
emit('submit', submitEvent) emits('submit', submitEvent)
} catch (error) { } catch (error) {
if (!(error instanceof FormValidationException)) { if (!(error instanceof FormValidationException)) {
throw error throw error
@@ -177,7 +177,7 @@ async function onSubmit(payload: Event) {
childrens: error.childrens childrens: error.childrens
} }
emit('error', errorEvent) emits('error', errorEvent)
} }
} }

View File

@@ -50,12 +50,11 @@ const props = withDefaults(defineProps<InputProps>(), {
type: 'text', type: 'text',
autofocusDelay: 100 autofocusDelay: 100
}) })
const emits = defineEmits<InputEmits>()
defineSlots<InputSlots>()
const [modelValue, modelModifiers] = defineModel<string | number>() const [modelValue, modelModifiers] = defineModel<string | number>()
const emit = defineEmits<InputEmits>()
defineSlots<InputSlots>()
const { emitFormBlur, emitFormInput, size, color, inputId, name, disabled } = useFormField<InputProps>(props) const { emitFormBlur, emitFormInput, size, color, inputId, name, disabled } = useFormField<InputProps>(props)
const { isLeading, isTrailing, leadingIconName, trailingIconName } = useComponentIcons(props) const { isLeading, isTrailing, leadingIconName, trailingIconName } = useComponentIcons(props)
// const { size: sizeButtonGroup, rounded } = useInjectButtonGroup({ ui, props }) // const { size: sizeButtonGroup, rounded } = useInjectButtonGroup({ ui, props })
@@ -115,7 +114,7 @@ function onChange(event: Event) {
function onBlur(event: FocusEvent) { function onBlur(event: FocusEvent) {
emitFormBlur() emitFormBlur()
emit('blur', event) emits('blur', event)
} }
onMounted(() => { onMounted(() => {

View File

@@ -47,6 +47,13 @@ const props = withDefaults(defineProps<RadioGroupProps<T>>(), { orientation: 've
const emits = defineEmits<RadioGroupEmits>() const emits = defineEmits<RadioGroupEmits>()
defineSlots<RadioGroupSlots<T>>() defineSlots<RadioGroupSlots<T>>()
const modelValue = defineModel<T>({
set(value) {
emits('change', value)
return value
}
})
const rootProps = useForwardPropsEmits(reactivePick(props, 'as', 'defaultValue', 'orientation', 'loop', 'required'), emits) const rootProps = useForwardPropsEmits(reactivePick(props, 'as', 'defaultValue', 'orientation', 'loop', 'required'), emits)
const { emitFormChange, color, name, size, inputId: _inputId, disabled } = useFormField<RadioGroupProps<T>>(props) const { emitFormChange, color, name, size, inputId: _inputId, disabled } = useFormField<RadioGroupProps<T>>(props)
@@ -80,13 +87,6 @@ const normalizedOptions = computed(() => {
return props.options.map(normalizeOption) return props.options.map(normalizeOption)
}) })
const modelValue = defineModel<T>({
set(value) {
emits('change', value)
return value
}
})
// FIXME: I think there's a race condition between this and the v-model event. // FIXME: I think there's a race condition between this and the v-model event.
// This must be triggered after the value updates, otherwise the form validates // This must be triggered after the value updates, otherwise the form validates
// the previous value. // the previous value.

View File

@@ -40,11 +40,11 @@ import { useId, useAppConfig, useFormField } from '#imports'
const props = defineProps<SwitchProps>() const props = defineProps<SwitchProps>()
defineSlots<SwitchSlots>() defineSlots<SwitchSlots>()
const modelValue = defineModel<boolean | undefined>({ default: undefined })
const appConfig = useAppConfig() const appConfig = useAppConfig()
const rootProps = useForwardProps(reactivePick(props, 'as', 'required', 'value')) const rootProps = useForwardProps(reactivePick(props, 'as', 'required', 'value'))
const modelValue = defineModel<boolean | undefined>({ default: undefined })
const { inputId: _inputId, emitFormChange, size, color, name, disabled } = useFormField<SwitchProps>(props) const { inputId: _inputId, emitFormChange, size, color, name, disabled } = useFormField<SwitchProps>(props)
const inputId = _inputId.value ?? useId() const inputId = _inputId.value ?? useId()

View File

@@ -49,9 +49,8 @@ const props = withDefaults(defineProps<TextareaProps>(), {
maxrows: 0, maxrows: 0,
autofocusDelay: 100 autofocusDelay: 100
}) })
const emit = defineEmits<TextareaEmits>()
defineSlots<TextareaSlots>() defineSlots<TextareaSlots>()
const emits = defineEmits<TextareaEmits>()
const [modelValue, modelModifiers] = defineModel<string | number>() const [modelValue, modelModifiers] = defineModel<string | number>()
@@ -108,7 +107,7 @@ function onChange(event: Event) {
function onBlur(event: FocusEvent) { function onBlur(event: FocusEvent) {
emitFormBlur() emitFormBlur()
emit('blur', event) emits('blur', event)
} }
onMounted(() => { onMounted(() => {