Skip to content

Commit 1a0d7a3

Browse files
authored
feat(Form): add attach prop to opt-out of nested form attachement (#3939)
1 parent c31bffa commit 1a0d7a3

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

docs/app/components/content/examples/form/FormExampleNested.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ async function onSubmit(event: FormSubmitEvent<Schema>) {
3939
<UCheckbox v-model="state.news" name="news" label="Register to our newsletter" @update:model-value="state.email = undefined" />
4040
</div>
4141

42-
<UForm v-if="state.news" :state="state" :schema="nestedSchema">
42+
<UForm v-if="state.news" :state="state" :schema="nestedSchema" attach>
4343
<UFormField label="Email" name="email">
4444
<UInput v-model="state.email" placeholder="[email protected]" />
4545
</UFormField>

docs/app/components/content/examples/form/FormExampleNestedList.vue

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,14 @@ async function onSubmit(event: FormSubmitEvent<Schema>) {
5151
<UInput v-model="state.customer" placeholder="Wonka Industries" />
5252
</UFormField>
5353

54-
<UForm v-for="item, count in state.items" :key="count" :state="item" :schema="itemSchema" class="flex gap-2">
54+
<UForm
55+
v-for="item, count in state.items"
56+
:key="count"
57+
:state="item"
58+
:schema="itemSchema"
59+
attach
60+
class="flex gap-2"
61+
>
5562
<UFormField :label="!count ? 'Description' : undefined" name="description">
5663
<UInput v-model="item.description" />
5764
</UFormField>

src/runtime/components/Form.vue

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ export interface FormProps<T extends object> {
3636
* @defaultValue `true`
3737
*/
3838
transform?: boolean
39+
40+
/**
41+
* If true, this form will attach to its parent Form (if any) and validate at the same time.
42+
* @defaultValue `true`
43+
*/
44+
attach?: boolean
45+
3946
/**
4047
* When `true`, all form elements will be disabled on `@submit` event.
4148
* This will cause any focused input elements to lose their focus state.
@@ -70,6 +77,7 @@ const props = withDefaults(defineProps<FormProps<T>>(), {
7077
return ['input', 'blur', 'change'] as FormInputEvents[]
7178
},
7279
validateOnInputDelay: 300,
80+
attach: true,
7381
transform: true,
7482
loadingAuto: true
7583
})
@@ -84,7 +92,7 @@ const ui = computed(() => tv({ extend: tv(theme), ...(appConfig.ui?.form || {})
8492
const formId = props.id ?? useId() as string
8593
8694
const bus = useEventBus<FormEvent<T>>(`form-${formId}`)
87-
const parentBus = inject(
95+
const parentBus = props.attach && inject(
8896
formBusInjectionKey,
8997
undefined
9098
)

0 commit comments

Comments
 (0)