feat(demo): 添加 Naive UI 组件表单演示功能
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
<script setup lang="ts">
|
||||
import { useDialog, useMessage, useModal } from 'naive-ui';
|
||||
import type { MessageType } from 'naive-ui';
|
||||
import { useDialog, useMessage } from 'naive-ui';
|
||||
import UseSafeNForm from './use-safe-n-form.vue';
|
||||
|
||||
definePage({ meta: {} });
|
||||
|
||||
const message = useMessage();
|
||||
const dialog = useDialog();
|
||||
const modal = useModal();
|
||||
|
||||
const messageTypes = ['info', 'success', 'warning', 'error', 'loading'] satisfies MessageType[];
|
||||
const dialogTypes = ['info', 'success', 'warning', 'error'] as const;
|
||||
@@ -38,11 +38,10 @@ const openDialog = (type: (typeof dialogTypes)[number]) => {
|
||||
};
|
||||
|
||||
const openModal = () => {
|
||||
modal.create({
|
||||
window.$nModal!.create({
|
||||
title: '命令式 Modal 示例',
|
||||
content: '这是一个命令式 API 创建的 Modal 示例,使用 preset="dialog"。',
|
||||
preset: 'dialog',
|
||||
maskClosable: false,
|
||||
onPositiveClick: () => {
|
||||
message.success('点击了确定');
|
||||
},
|
||||
@@ -62,7 +61,9 @@ const openModal = () => {
|
||||
<NAlert title="信息" type="info" :bordered="false">
|
||||
演示 Naive UI 各种组件的使用方法和功能特性
|
||||
</NAlert>
|
||||
|
||||
<n-card title="SafeNForm" mt-4>
|
||||
<UseSafeNForm />
|
||||
</n-card>
|
||||
<NCard title="Message 消息" class="mt-4">
|
||||
<NSpace>
|
||||
<NButton
|
||||
72
src/pages/demos/naive-ui-demo/use-safe-n-form.vue
Normal file
72
src/pages/demos/naive-ui-demo/use-safe-n-form.vue
Normal file
@@ -0,0 +1,72 @@
|
||||
<script setup lang="ts">
|
||||
const { formValue, SafeNForm, SafeNFormItem, formRef } = useSafeNForm({
|
||||
initialFormValue: {
|
||||
/* ⚠️:
|
||||
如果没使用`SafeNFormItem`,
|
||||
这里`user`对象没有手动初始化的话,将会报错:
|
||||
`can't access property "name", $setup.formValue.user is undefined`
|
||||
*/
|
||||
user: {
|
||||
name: '',
|
||||
age: 0,
|
||||
},
|
||||
phone: '',
|
||||
},
|
||||
});
|
||||
|
||||
function handleValidateClick() {
|
||||
formRef.value?.validate((errors) => {
|
||||
if (!errors) {
|
||||
window.$nMessage!.success('Valid');
|
||||
} else {
|
||||
console.log(errors);
|
||||
window.$nMessage!.error('Invalid');
|
||||
}
|
||||
});
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div border>
|
||||
<pre>formValue: {{ JSON.stringify(formValue, null, 2) }}</pre>
|
||||
</div>
|
||||
|
||||
<SafeNForm inline label-placement="left" label-width="auto" mt-4>
|
||||
<n-form-item
|
||||
label="姓名"
|
||||
path="user.name"
|
||||
:rule="{ required: true, message: '请输入姓名', trigger: ['input'] }"
|
||||
>
|
||||
<n-input v-model:value="formValue.user.name" placeholder="输入姓名" />
|
||||
</n-form-item>
|
||||
|
||||
<SafeNFormItem
|
||||
#default="{ value, setValue }"
|
||||
:rule="{ required: true, message: '请输入姓名', trigger: ['input'] }"
|
||||
label="姓名"
|
||||
path="user.name"
|
||||
>
|
||||
<NInput :value="value" placeholder="SafeNFormItem" @update:value="setValue" />
|
||||
</SafeNFormItem>
|
||||
|
||||
<n-form-item
|
||||
label="电话号码"
|
||||
path="phone"
|
||||
:rule="{ required: true, message: '请输入电话号码', trigger: ['blur'] }"
|
||||
>
|
||||
<n-input v-model:value="formValue.phone" placeholder="电话号码" />
|
||||
</n-form-item>
|
||||
|
||||
<SafeNFormItem
|
||||
label="电话号码"
|
||||
path="phone"
|
||||
:rule="{ required: true, message: '请输入电话号码', trigger: ['blur'] }"
|
||||
>
|
||||
<!-- 如果没有提供插槽,会默认渲染一个`<NInput>` -->
|
||||
</SafeNFormItem>
|
||||
|
||||
<n-form-item>
|
||||
<n-button attr-type="button" @click="handleValidateClick"> 验证 </n-button>
|
||||
</n-form-item>
|
||||
</SafeNForm>
|
||||
</template>
|
||||
Reference in New Issue
Block a user