Skip to content

Commit

Permalink
fix(Form): generate input uid on mounted to avoid hydration mismatch …
Browse files Browse the repository at this point in the history
…and collisions
  • Loading branch information
romhml committed Nov 14, 2023
1 parent 2c86f88 commit 4382f98
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
3 changes: 1 addition & 2 deletions src/runtime/components/forms/Checkbox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,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
16 changes: 12 additions & 4 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 Down Expand Up @@ -48,7 +48,7 @@ export default defineComponent({
id: {
type: String,
// A default value is needed here to bind the label
default: () => uid()
default: () => null,

Check failure on line 51 in src/runtime/components/forms/Radio.vue

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, 18)

Unexpected trailing comma
},
value: {
type: [String, Number, Boolean],
Expand Down Expand Up @@ -103,7 +103,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 +137,7 @@ export default defineComponent({
})
return {
inputId,
// eslint-disable-next-line vue/no-dupe-keys
ui,
attrs,
Expand Down

0 comments on commit 4382f98

Please sign in to comment.