ant-design-vue/components/form/demo/form-context.vue
tangjinzhou 7aae6f675c
doc: form add demo (#5070)
* doc: add form demo

* fix: form dynamic error

* doc: add form demo

* revert: formitem auto validate #4955

* fix: input not update when set undefined

* doc: add form demo

* fix: form validate for number tyope, close #5064

* fix: form validateMessages not work

* doc: add form demo
2021-12-23 22:00:14 +08:00

149 lines
3.9 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<docs>
---
order: 8
title:
zh-CN: 多表单联动
en-US: Control between forms
---
## zh-CN
本例子中Modal 的确认按钮在 Form 之外通过 `form.submit` 方法调用表单提交功能反之则推荐使用 `<Button htmlType="submit" />` 调用 web 原生提交逻辑
## en-US
In this case, submit button is in the Modal which is out of Form. You can use `form.submit` to submit form. Besides, we recommend native `<Button htmlType="submit" />` to submit a form.
</docs>
<template>
<a-form ref="formRef" :model="formState" name="form_context" v-bind="layout" @finish="onFinish">
<a-form-item
name="group"
label="Group Name"
:rules="[{ required: true, message: 'Please input group name!' }]"
>
<a-input v-model:value="formState.group" />
</a-form-item>
<a-form-item label="User List">
<template v-if="formState.users.length">
<ul>
<template v-for="user in formState.users" :key="user.key">
<li class="user">
<a-avatar>
<template #icon><UserOutlined /></template>
</a-avatar>
{{ user.name }} - {{ user.age }}
</li>
</template>
</ul>
</template>
<template v-else>
<a-typography-text class="ant-form-text" type="secondary">
(
<SmileOutlined />
No user yet. )
</a-typography-text>
</template>
</a-form-item>
<a-form-item v-bind="tailLayout">
<a-button html-type="submit" type="primary">Submit</a-button>
<a-button html-type="button" style="margin: 0 8px" @click="visible = true">Add User</a-button>
</a-form-item>
</a-form>
<a-modal v-model:visible="visible" title="Basic Drawer" @ok="onOk">
<a-form ref="modalFormRef" :model="modalFormState" layout="vertical" name="userForm">
<a-form-item name="name" label="User Name" :rules="[{ required: true }]">
<a-input v-model:value="modalFormState.name" />
</a-form-item>
<a-form-item name="age" label="User Age" :rules="[{ required: true }]">
<a-input-number v-model:value="modalFormState.age" />
</a-form-item>
</a-form>
</a-modal>
</template>
<script lang="ts">
import { defineComponent, reactive, ref, watch, toRaw } from 'vue';
import type { FormInstance } from 'ant-design-vue';
import { SmileOutlined, UserOutlined } from '@ant-design/icons-vue';
interface UserType {
name?: string;
age?: number;
key?: number;
}
interface FormState {
group: string;
users: UserType[];
}
export default defineComponent({
components: {
SmileOutlined,
UserOutlined,
},
setup() {
const formRef = ref<FormInstance>();
const modalFormRef = ref<FormInstance>();
const visible = ref(false);
const formState = reactive<FormState>({
group: '',
users: [],
});
const modalFormState = ref<UserType>({});
watch(
visible,
() => {
modalFormState.value = {};
},
{ flush: 'post' },
);
const onOk = () => {
modalFormRef.value.validateFields().then(() => {
formState.users.push({ ...modalFormState.value, key: Date.now() });
visible.value = false;
});
};
const onFinish = () => {
console.log('Finish:', toRaw(formState));
};
const layout = {
labelCol: { span: 8 },
wrapperCol: { span: 16 },
};
const tailLayout = {
wrapperCol: { offset: 8, span: 16 },
};
return {
formState,
layout,
tailLayout,
formRef,
modalFormRef,
visible,
modalFormState,
onOk,
onFinish,
};
},
});
</script>
<style>
#components-form-demo-form-context .user {
margin-bottom: 8px;
}
#components-form-demo-form-context .user .ant-avatar {
margin-right: 8px;
}
.ant-row-rtl #components-form-demo-form-context .user .ant-avatar {
margin-right: 0;
margin-left: 8px;
}
</style>