Skip to content

Commit

Permalink
fix(FormGroup): hydration mismatch on inputId (#942)
Browse files Browse the repository at this point in the history
  • Loading branch information
romhml committed Nov 14, 2023
1 parent 3c71bf3 commit a3046aa
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
4 changes: 1 addition & 3 deletions src/runtime/components/forms/Checkbox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ 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'
Expand All @@ -50,8 +49,7 @@ export default defineComponent({
props: {
id: {
type: String,
// A default value is needed here to bind the label
default: () => uid()
default: () => null
},
value: {
type: [String, Number, Boolean, Object],
Expand Down
3 changes: 1 addition & 2 deletions src/runtime/components/forms/FormGroup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ import type { FormError, InjectedFormGroupValue, Strategy } from '../../types'
// @ts-expect-error
import appConfig from '#build/app.config'
import { formGroup } from '#ui/ui.config'
import { uid } from '../../utils/uid'
const config = mergeConfig<typeof formGroup>(appConfig.ui.strategy, appConfig.ui.formGroup, formGroup)
Expand Down Expand Up @@ -109,7 +108,7 @@ export default defineComponent({
})
const size = computed(() => ui.value.size[props.size ?? config.default.size])
const inputId = ref(uid())
const inputId = ref()
provide<InjectedFormGroupValue>('form-group', {
error,
Expand Down
19 changes: 13 additions & 6 deletions src/runtime/components/forms/Radio.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div :class="ui.wrapper">
<div class="flex items-center h-5">
<input
:id="id"
:id="inputId"
v-model="pick"
:name="name"
:required="required"
Expand All @@ -15,7 +15,7 @@
>
</div>
<div v-if="label || $slots.label" class="ms-3 flex flex-col">
<label :for="id" :class="ui.label">
<label :for="inputId" :class="ui.label">
<slot name="label">{{ label }}</slot>
<span v-if="required" :class="ui.required">*</span>
</label>
Expand All @@ -27,7 +27,7 @@
</template>

<script lang="ts">
import { computed, defineComponent, inject, toRef } from 'vue'
import { computed, defineComponent, inject, toRef, onMounted, ref } from 'vue'
import type { PropType } from 'vue'
import { twMerge, twJoin } from 'tailwind-merge'
import { useUI } from '../../composables/useUI'
Expand All @@ -47,8 +47,7 @@ export default defineComponent({
props: {
id: {
type: String,
// A default value is needed here to bind the label
default: () => uid()
default: () => null
},
value: {
type: [String, Number, Boolean],
Expand Down Expand Up @@ -103,7 +102,14 @@ export default defineComponent({
const { ui, attrs } = useUI('radio', toRef(props, 'ui'), config, toRef(props, 'class'))
const radioGroup = inject('radio-group', null)
const { emitFormChange, color, name } = radioGroup ?? useFormGroup(props, config)
const { emitFormChange, color, name } = radioGroup ?? useFormGroup(props, config)
const inputId = ref(props.id)
onMounted(() => {
if (!inputId.value) {
inputId.value = uid()
}
})
const pick = computed({
get () {
Expand All @@ -130,6 +136,7 @@ export default defineComponent({
})
return {
inputId,
// eslint-disable-next-line vue/no-dupe-keys
ui,
attrs,
Expand Down
4 changes: 3 additions & 1 deletion src/runtime/composables/useFormGroup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { inject, ref, computed, onMounted } from 'vue'
import { type UseEventBusReturn, useDebounceFn } from '@vueuse/core'
import type { FormEvent, FormEventType, InjectedFormGroupValue } from '../types/form'
import { uid } from '../utils/uid'

type InputProps = {
id?: string | null
Expand All @@ -18,7 +19,8 @@ export const useFormGroup = (inputProps?: InputProps, config?: any) => {
const inputId = ref(inputProps?.id)

onMounted(() => {
inputId.value = inputProps?.isFieldset ? null : inputProps?.id ?? formGroup?.inputId.value
inputId.value = inputProps?.isFieldset ? null : inputProps?.id ?? uid()

if (formGroup) {
// Updates for="..." attribute on label if inputProps.id is provided
formGroup.inputId.value = inputId.value
Expand Down

1 comment on commit a3046aa

@vercel
Copy link

@vercel vercel bot commented on a3046aa Nov 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ui – ./

ui-nuxt-js.vercel.app
ui-git-dev-nuxt-js.vercel.app
ui.nuxt.com

Please sign in to comment.