2024-09-10 21:48:51 +08:00
|
|
|
|
<script lang="ts" setup>
|
2024-12-04 21:40:41 +08:00
|
|
|
|
import { ref } from 'vue';
|
|
|
|
|
|
2024-09-10 21:48:51 +08:00
|
|
|
|
import { Page } from '@vben/common-ui';
|
|
|
|
|
|
2024-12-04 21:40:41 +08:00
|
|
|
|
import { Button, Card, message, TabPane, Tabs } from 'ant-design-vue';
|
2024-09-10 21:48:51 +08:00
|
|
|
|
import dayjs from 'dayjs';
|
|
|
|
|
|
2024-10-14 22:53:23 +08:00
|
|
|
|
import { useVbenForm } from '#/adapter/form';
|
2024-12-04 22:56:29 +08:00
|
|
|
|
import { getAllMenusApi } from '#/api';
|
2024-09-10 21:48:51 +08:00
|
|
|
|
|
2024-10-04 23:05:28 +08:00
|
|
|
|
import DocButton from '../doc-button.vue';
|
|
|
|
|
|
2024-12-04 21:40:41 +08:00
|
|
|
|
const activeTab = ref('basic');
|
|
|
|
|
|
2024-09-10 21:48:51 +08:00
|
|
|
|
const [BaseForm, baseFormApi] = useVbenForm({
|
|
|
|
|
// 所有表单项共用,可单独在表单内覆盖
|
|
|
|
|
commonConfig: {
|
|
|
|
|
// 所有表单项
|
|
|
|
|
componentProps: {
|
|
|
|
|
class: 'w-full',
|
|
|
|
|
},
|
|
|
|
|
},
|
2024-11-09 15:00:59 +08:00
|
|
|
|
fieldMappingTime: [['rangePicker', ['startTime', 'endTime'], 'YYYY-MM-DD']],
|
2024-09-10 21:48:51 +08:00
|
|
|
|
// 提交函数
|
|
|
|
|
handleSubmit: onSubmit,
|
2024-11-09 15:00:59 +08:00
|
|
|
|
|
2024-09-10 21:48:51 +08:00
|
|
|
|
// 垂直布局,label和input在不同行,值为vertical
|
|
|
|
|
// 水平布局,label和input在同一行
|
2024-09-19 21:56:49 +08:00
|
|
|
|
layout: 'horizontal',
|
2024-09-10 21:48:51 +08:00
|
|
|
|
schema: [
|
|
|
|
|
{
|
|
|
|
|
// 组件需要在 #/adapter.ts内注册,并加上类型
|
|
|
|
|
component: 'Input',
|
|
|
|
|
// 对应组件的参数
|
|
|
|
|
componentProps: {
|
|
|
|
|
placeholder: '请输入用户名',
|
|
|
|
|
},
|
|
|
|
|
// 字段名
|
|
|
|
|
fieldName: 'username',
|
|
|
|
|
// 界面显示的label
|
|
|
|
|
label: '字符串',
|
|
|
|
|
},
|
2024-12-04 22:56:29 +08:00
|
|
|
|
{
|
|
|
|
|
// 组件需要在 #/adapter.ts内注册,并加上类型
|
|
|
|
|
component: 'ApiSelect',
|
|
|
|
|
// 对应组件的参数
|
|
|
|
|
componentProps: {
|
|
|
|
|
// 菜单接口转options格式
|
|
|
|
|
afterFetch: (data: { name: string; path: string }[]) => {
|
|
|
|
|
return data.map((item: any) => ({
|
|
|
|
|
label: item.name,
|
|
|
|
|
value: item.path,
|
|
|
|
|
}));
|
|
|
|
|
},
|
|
|
|
|
// 菜单接口
|
|
|
|
|
api: getAllMenusApi,
|
|
|
|
|
},
|
|
|
|
|
// 字段名
|
|
|
|
|
fieldName: 'api',
|
|
|
|
|
// 界面显示的label
|
|
|
|
|
label: 'ApiSelect',
|
|
|
|
|
},
|
2024-12-09 12:47:33 +08:00
|
|
|
|
{
|
|
|
|
|
component: 'ApiTreeSelect',
|
|
|
|
|
// 对应组件的参数
|
|
|
|
|
componentProps: {
|
|
|
|
|
// 菜单接口
|
|
|
|
|
api: getAllMenusApi,
|
|
|
|
|
childrenField: 'children',
|
|
|
|
|
// 菜单接口转options格式
|
|
|
|
|
labelField: 'name',
|
|
|
|
|
valueField: 'path',
|
|
|
|
|
},
|
|
|
|
|
// 字段名
|
|
|
|
|
fieldName: 'apiTree',
|
|
|
|
|
// 界面显示的label
|
|
|
|
|
label: 'ApiTreeSelect',
|
|
|
|
|
},
|
2024-09-10 21:48:51 +08:00
|
|
|
|
{
|
|
|
|
|
component: 'InputPassword',
|
|
|
|
|
componentProps: {
|
|
|
|
|
placeholder: '请输入密码',
|
|
|
|
|
},
|
|
|
|
|
fieldName: 'password',
|
|
|
|
|
label: '密码',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
component: 'InputNumber',
|
|
|
|
|
componentProps: {
|
|
|
|
|
placeholder: '请输入',
|
|
|
|
|
},
|
|
|
|
|
fieldName: 'number',
|
|
|
|
|
label: '数字(带后缀)',
|
|
|
|
|
suffix: () => '¥',
|
|
|
|
|
},
|
2024-12-04 21:42:21 +08:00
|
|
|
|
{
|
|
|
|
|
component: 'IconPicker',
|
|
|
|
|
fieldName: 'icon',
|
|
|
|
|
label: '图标',
|
|
|
|
|
},
|
2024-09-10 21:48:51 +08:00
|
|
|
|
{
|
|
|
|
|
component: 'Select',
|
|
|
|
|
componentProps: {
|
|
|
|
|
allowClear: true,
|
|
|
|
|
filterOption: true,
|
|
|
|
|
options: [
|
|
|
|
|
{
|
|
|
|
|
label: '选项1',
|
|
|
|
|
value: '1',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: '选项2',
|
|
|
|
|
value: '2',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
placeholder: '请选择',
|
|
|
|
|
showSearch: true,
|
|
|
|
|
},
|
|
|
|
|
fieldName: 'options',
|
|
|
|
|
label: '下拉选',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
component: 'RadioGroup',
|
|
|
|
|
componentProps: {
|
|
|
|
|
options: [
|
|
|
|
|
{
|
|
|
|
|
label: '选项1',
|
|
|
|
|
value: '1',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: '选项2',
|
|
|
|
|
value: '2',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
fieldName: 'radioGroup',
|
|
|
|
|
label: '单选组',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
component: 'Radio',
|
|
|
|
|
fieldName: 'radio',
|
|
|
|
|
label: '',
|
|
|
|
|
renderComponentContent: () => {
|
|
|
|
|
return {
|
|
|
|
|
default: () => ['Radio'],
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
component: 'CheckboxGroup',
|
|
|
|
|
componentProps: {
|
|
|
|
|
name: 'cname',
|
|
|
|
|
options: [
|
|
|
|
|
{
|
|
|
|
|
label: '选项1',
|
|
|
|
|
value: '1',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: '选项2',
|
|
|
|
|
value: '2',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
fieldName: 'checkboxGroup',
|
|
|
|
|
label: '多选组',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
component: 'Checkbox',
|
|
|
|
|
fieldName: 'checkbox',
|
|
|
|
|
label: '',
|
|
|
|
|
renderComponentContent: () => {
|
|
|
|
|
return {
|
|
|
|
|
default: () => ['我已阅读并同意'],
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
component: 'Mentions',
|
|
|
|
|
componentProps: {
|
|
|
|
|
options: [
|
|
|
|
|
{
|
|
|
|
|
label: 'afc163',
|
|
|
|
|
value: 'afc163',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: 'zombieJ',
|
|
|
|
|
value: 'zombieJ',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
placeholder: '请输入',
|
|
|
|
|
},
|
|
|
|
|
fieldName: 'mentions',
|
|
|
|
|
label: '提及',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
component: 'Rate',
|
|
|
|
|
fieldName: 'rate',
|
|
|
|
|
label: '评分',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
component: 'Switch',
|
|
|
|
|
componentProps: {
|
|
|
|
|
class: 'w-auto',
|
|
|
|
|
},
|
|
|
|
|
fieldName: 'switch',
|
|
|
|
|
label: '开关',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
component: 'DatePicker',
|
|
|
|
|
fieldName: 'datePicker',
|
|
|
|
|
label: '日期选择框',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
component: 'RangePicker',
|
|
|
|
|
fieldName: 'rangePicker',
|
|
|
|
|
label: '范围选择器',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
component: 'TimePicker',
|
|
|
|
|
fieldName: 'timePicker',
|
|
|
|
|
label: '时间选择框',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
component: 'TreeSelect',
|
|
|
|
|
componentProps: {
|
|
|
|
|
allowClear: true,
|
|
|
|
|
placeholder: '请选择',
|
|
|
|
|
showSearch: true,
|
|
|
|
|
treeData: [
|
|
|
|
|
{
|
|
|
|
|
label: 'root 1',
|
|
|
|
|
value: 'root 1',
|
|
|
|
|
children: [
|
|
|
|
|
{
|
|
|
|
|
label: 'parent 1',
|
|
|
|
|
value: 'parent 1',
|
|
|
|
|
children: [
|
|
|
|
|
{
|
|
|
|
|
label: 'parent 1-0',
|
|
|
|
|
value: 'parent 1-0',
|
|
|
|
|
children: [
|
|
|
|
|
{
|
|
|
|
|
label: 'my leaf',
|
|
|
|
|
value: 'leaf1',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: 'your leaf',
|
|
|
|
|
value: 'leaf2',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: 'parent 1-1',
|
|
|
|
|
value: 'parent 1-1',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: 'parent 2',
|
|
|
|
|
value: 'parent 2',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
treeNodeFilterProp: 'label',
|
|
|
|
|
},
|
|
|
|
|
fieldName: 'treeSelect',
|
|
|
|
|
label: '树选择',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
// 大屏一行显示3个,中屏一行显示2个,小屏一行显示1个
|
|
|
|
|
wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3',
|
|
|
|
|
});
|
|
|
|
|
|
2024-09-30 09:47:16 +08:00
|
|
|
|
const [CustomLayoutForm] = useVbenForm({
|
|
|
|
|
// 所有表单项共用,可单独在表单内覆盖
|
|
|
|
|
commonConfig: {
|
|
|
|
|
// 所有表单项
|
|
|
|
|
componentProps: {
|
|
|
|
|
class: 'w-full',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
layout: 'horizontal',
|
|
|
|
|
schema: [
|
|
|
|
|
{
|
2024-10-03 13:04:19 +08:00
|
|
|
|
component: 'Select',
|
2024-09-30 09:47:16 +08:00
|
|
|
|
fieldName: 'field1',
|
|
|
|
|
label: '字符串',
|
|
|
|
|
},
|
|
|
|
|
{
|
2024-10-03 13:04:19 +08:00
|
|
|
|
component: 'TreeSelect',
|
2024-09-30 09:47:16 +08:00
|
|
|
|
fieldName: 'field2',
|
|
|
|
|
label: '字符串',
|
|
|
|
|
},
|
|
|
|
|
{
|
2024-10-03 13:04:19 +08:00
|
|
|
|
component: 'Mentions',
|
2024-09-30 09:47:16 +08:00
|
|
|
|
fieldName: 'field3',
|
|
|
|
|
label: '字符串',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
component: 'Input',
|
|
|
|
|
fieldName: 'field4',
|
|
|
|
|
label: '字符串',
|
|
|
|
|
},
|
|
|
|
|
{
|
2024-10-03 13:04:19 +08:00
|
|
|
|
component: 'InputNumber',
|
2024-09-30 09:47:16 +08:00
|
|
|
|
fieldName: 'field5',
|
|
|
|
|
// 从第三列开始 相当于中间空了一列
|
|
|
|
|
formItemClass: 'col-start-3',
|
|
|
|
|
label: '前面空了一列',
|
|
|
|
|
},
|
|
|
|
|
{
|
2024-10-03 13:04:19 +08:00
|
|
|
|
component: 'Textarea',
|
2024-09-30 09:47:16 +08:00
|
|
|
|
fieldName: 'field6',
|
2024-10-03 13:04:19 +08:00
|
|
|
|
// 占满三列空间 基线对齐
|
|
|
|
|
formItemClass: 'col-span-3 items-baseline',
|
2024-09-30 09:47:16 +08:00
|
|
|
|
label: '占满三列',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
component: 'Input',
|
|
|
|
|
fieldName: 'field7',
|
|
|
|
|
// 占满2列空间 从第二列开始 相当于前面空了一列
|
|
|
|
|
formItemClass: 'col-span-2 col-start-2',
|
|
|
|
|
label: '占满2列',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
component: 'Input',
|
|
|
|
|
fieldName: 'field8',
|
|
|
|
|
// 左右留空
|
|
|
|
|
formItemClass: 'col-start-2',
|
|
|
|
|
label: '左右留空',
|
|
|
|
|
},
|
|
|
|
|
{
|
2024-10-03 13:04:19 +08:00
|
|
|
|
component: 'InputPassword',
|
2024-09-30 09:47:16 +08:00
|
|
|
|
fieldName: 'field9',
|
|
|
|
|
formItemClass: 'col-start-1',
|
2024-10-03 13:04:19 +08:00
|
|
|
|
label: '字符串',
|
2024-09-30 09:47:16 +08:00
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
// 一共三列
|
2024-10-03 13:04:19 +08:00
|
|
|
|
wrapperClass: 'grid-cols-3',
|
2024-09-30 09:47:16 +08:00
|
|
|
|
});
|
|
|
|
|
|
2024-09-10 21:48:51 +08:00
|
|
|
|
function onSubmit(values: Record<string, any>) {
|
|
|
|
|
message.success({
|
|
|
|
|
content: `form values: ${JSON.stringify(values)}`,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleSetFormValue() {
|
|
|
|
|
/**
|
|
|
|
|
* 设置表单值(多个)
|
|
|
|
|
*/
|
|
|
|
|
baseFormApi.setValues({
|
|
|
|
|
checkboxGroup: ['1'],
|
|
|
|
|
datePicker: dayjs('2022-01-01'),
|
|
|
|
|
mentions: '@afc163',
|
|
|
|
|
number: 3,
|
|
|
|
|
options: '1',
|
|
|
|
|
password: '2',
|
|
|
|
|
radioGroup: '1',
|
|
|
|
|
rangePicker: [dayjs('2022-01-01'), dayjs('2022-01-02')],
|
|
|
|
|
rate: 3,
|
|
|
|
|
switch: true,
|
|
|
|
|
timePicker: dayjs('2022-01-01 12:00:00'),
|
|
|
|
|
treeSelect: 'leaf1',
|
|
|
|
|
username: '1',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 设置单个表单值
|
|
|
|
|
baseFormApi.setFieldValue('checkbox', true);
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<template>
|
|
|
|
|
<Page
|
2024-09-30 09:47:16 +08:00
|
|
|
|
content-class="flex flex-col gap-4"
|
2024-09-10 21:48:51 +08:00
|
|
|
|
description="表单组件基础示例,请注意,该页面用到的参数代码会添加一些简单注释,方便理解,请仔细查看。"
|
2024-12-04 21:40:41 +08:00
|
|
|
|
header-class="pb-0"
|
2024-09-10 21:48:51 +08:00
|
|
|
|
title="表单组件"
|
|
|
|
|
>
|
2024-12-04 21:40:41 +08:00
|
|
|
|
<template #description>
|
|
|
|
|
<div class="text-muted-foreground">
|
|
|
|
|
<p>
|
|
|
|
|
表单组件基础示例,请注意,该页面用到的参数代码会添加一些简单注释,方便理解,请仔细查看。
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
<Tabs v-model:active-key="activeTab" :tab-bar-style="{ marginBottom: 0 }">
|
|
|
|
|
<TabPane key="basic" tab="基础示例" />
|
|
|
|
|
<TabPane key="layout" tab="自定义布局" />
|
|
|
|
|
</Tabs>
|
|
|
|
|
</template>
|
2024-10-04 23:05:28 +08:00
|
|
|
|
<template #extra>
|
|
|
|
|
<DocButton path="/components/common-ui/vben-form" />
|
|
|
|
|
</template>
|
2024-12-04 21:40:41 +08:00
|
|
|
|
<Card v-show="activeTab === 'basic'" title="基础示例">
|
2024-09-10 21:48:51 +08:00
|
|
|
|
<template #extra>
|
|
|
|
|
<Button type="primary" @click="handleSetFormValue">设置表单值</Button>
|
|
|
|
|
</template>
|
|
|
|
|
<BaseForm />
|
|
|
|
|
</Card>
|
2024-12-04 21:40:41 +08:00
|
|
|
|
<Card v-show="activeTab === 'layout'" title="使用tailwind自定义布局">
|
2024-09-30 09:47:16 +08:00
|
|
|
|
<CustomLayoutForm />
|
|
|
|
|
</Card>
|
2024-09-10 21:48:51 +08:00
|
|
|
|
</Page>
|
|
|
|
|
</template>
|