chore(FormGroup): simplify bindings between input and form group p… (#704)

Co-authored-by: Benjamin Canac <canacb1@gmail.com>
This commit is contained in:
Romain Hamel
2023-09-21 23:22:55 +02:00
committed by GitHub
parent a94782d94b
commit 46879dc1b7
14 changed files with 81 additions and 65 deletions

View File

@@ -2,7 +2,7 @@
<div :class="ui.wrapper">
<div class="flex items-center h-5">
<input
:id="name"
:id="inputId"
v-model="toggle"
:name="name"
:required="required"
@@ -18,7 +18,7 @@
>
</div>
<div v-if="label || $slots.label" class="ms-3 text-sm">
<label :for="name" :class="ui.label">
<label :for="inputId" :class="ui.label">
<slot name="label">{{ label }}</slot>
<span v-if="required" :class="ui.required">*</span>
</label>
@@ -36,6 +36,7 @@ import { twMerge, twJoin } from 'tailwind-merge'
import { useUI } from '../../composables/useUI'
import { useFormGroup } from '../../composables/useFormGroup'
import { mergeConfig } from '../../utils'
import { uid } from '../../utils/uid'
import type { Strategy } from '../../types'
// @ts-expect-error
import appConfig from '#build/app.config'
@@ -47,6 +48,11 @@ const config = mergeConfig<typeof checkbox>(appConfig.ui.strategy, appConfig.ui.
export default defineComponent({
inheritAttrs: false,
props: {
id: {
type: String,
// A default value is needed here to bind the label
default: () => uid()
},
value: {
type: [String, Number, Boolean, Object],
default: null
@@ -103,8 +109,7 @@ export default defineComponent({
setup (props, { emit }) {
const { ui, attrs } = useUI('checkbox', props.ui, config, { mergeWrapper: true })
const { emitFormChange, formGroup } = useFormGroup()
const color = computed(() => formGroup?.error?.value ? 'red' : props.color)
const { emitFormChange, color, name, inputId } = useFormGroup(props)
const toggle = computed({
get () {
@@ -136,6 +141,9 @@ export default defineComponent({
ui,
attrs,
toggle,
inputId,
// eslint-disable-next-line vue/no-dupe-keys
name,
// eslint-disable-next-line vue/no-dupe-keys
inputClass,
onChange