refactor: 优化 antd 模板代码生成示例提到的一些问题

pull/87/head
puhui999 2025-04-27 18:16:38 +08:00
parent 5c4b95b01e
commit 994b444319
4 changed files with 49 additions and 39 deletions

View File

@ -1,3 +1,5 @@
import type { Dayjs } from 'dayjs';
import type { PageParam, PageResult } from '@vben/request'; import type { PageParam, PageResult } from '@vben/request';
import { requestClient } from '#/api/request'; import { requestClient } from '#/api/request';
@ -7,8 +9,8 @@ export namespace Demo01ContactApi {
export interface Demo01Contact { export interface Demo01Contact {
id: number; // 编号 id: number; // 编号
name?: string; // 名字 name?: string; // 名字
sex?: number; // 性别 sex?: boolean; // 性别
birthday?: Date; // 出生年 birthday?: Dayjs | string; // 出生年
description?: string; // 简介 description?: string; // 简介
avatar: string; // 头像 avatar: string; // 头像
} }

View File

@ -3,7 +3,7 @@ import dayjs from 'dayjs';
// TODO @芋艿:后续整理下 迁移至 packages/core/base/shared/src/utils/date.ts,后续删除 使用 @vben/utils 的 getRangePickerDefaultProps // TODO @芋艿:后续整理下 迁移至 packages/core/base/shared/src/utils/date.ts,后续删除 使用 @vben/utils 的 getRangePickerDefaultProps
/** 时间段选择器拓展 */ /** 时间段选择器拓展 */
export function getRangePickerDefaultProps() { export function getRangePickerDefaultProps(): any {
return { return {
showTime: { showTime: {
format: 'HH:mm:ss', format: 'HH:mm:ss',

View File

@ -1,10 +1,12 @@
<script lang="ts" setup> <script lang="ts" setup>
import type { VxeTableInstance } from 'vxe-table';
import type { Demo01ContactApi } from '#/api/infra/demo/demo01'; import type { Demo01ContactApi } from '#/api/infra/demo/demo01';
import { h, onMounted, reactive, ref } from 'vue'; import { h, nextTick, onMounted, reactive, ref } from 'vue';
import { Page, useVbenModal } from '@vben/common-ui'; import { Page, useVbenModal } from '@vben/common-ui';
import { Download, Plus, RefreshCw, Search } from '@vben/icons'; import { Download, Plus } from '@vben/icons';
import { cloneDeep, formatDateTime } from '@vben/utils'; import { cloneDeep, formatDateTime } from '@vben/utils';
import { import {
@ -25,6 +27,7 @@ import {
} from '#/api/infra/demo/demo01'; } from '#/api/infra/demo/demo01';
import { ContentWrap } from '#/components/content-wrap'; import { ContentWrap } from '#/components/content-wrap';
import { DictTag } from '#/components/dict-tag'; import { DictTag } from '#/components/dict-tag';
import { TableToolbar } from '#/components/table-toolbar';
import { $t } from '#/locales'; import { $t } from '#/locales';
import { getRangePickerDefaultProps } from '#/utils/date'; import { getRangePickerDefaultProps } from '#/utils/date';
import { DICT_TYPE, getDictOptions } from '#/utils/dict'; import { DICT_TYPE, getDictOptions } from '#/utils/dict';
@ -118,9 +121,20 @@ async function onExport() {
} }
} }
/** 隐藏搜索栏 */
const hiddenSearchBar = ref(false);
const tableToolbarRef = ref<InstanceType<typeof TableToolbar>>();
const tableRef = ref<VxeTableInstance>();
/** 初始化 */ /** 初始化 */
onMounted(() => { onMounted(async () => {
getList(); await getList();
await nextTick();
// $toolbar
const $table = tableRef.value;
const tableToolbar = tableToolbarRef.value;
if ($table && tableToolbar) {
await $table.connect(tableToolbar.getToolbarRef()!);
}
}); });
</script> </script>
@ -128,15 +142,10 @@ onMounted(() => {
<Page auto-content-height> <Page auto-content-height>
<FormModal @success="getList" /> <FormModal @success="getList" />
<ContentWrap> <ContentWrap v-if="!hiddenSearchBar">
<!-- 搜索工作栏 --> <!-- 搜索工作栏 -->
<!-- TODO @puhui999貌似 -mb-15px 没效果可能和 ContentWrap 有关系 --> <!-- TODO @puhui999貌似 -mb-15px 没效果可能和 ContentWrap 有关系 -->
<Form <Form :model="queryParams" ref="queryFormRef" layout="inline">
class="-mb-15px"
:model="queryParams"
ref="queryFormRef"
layout="inline"
>
<Form.Item label="名字" name="name"> <Form.Item label="名字" name="name">
<!-- TODO @puhui999貌似不一定 240看着和 schema 还是不太一样 --> <!-- TODO @puhui999貌似不一定 240看着和 schema 还是不太一样 -->
<Input <Input
@ -144,7 +153,7 @@ onMounted(() => {
placeholder="请输入名字" placeholder="请输入名字"
allow-clear allow-clear
@press-enter="handleQuery" @press-enter="handleQuery"
class="!w-240px" class="w-full"
/> />
</Form.Item> </Form.Item>
<Form.Item label="性别" name="sex"> <Form.Item label="性别" name="sex">
@ -152,7 +161,7 @@ onMounted(() => {
v-model:value="queryParams.sex" v-model:value="queryParams.sex"
placeholder="请选择性别" placeholder="请选择性别"
allow-clear allow-clear
class="!w-240px" class="w-full"
> >
<Select.Option <Select.Option
v-for="dict in getDictOptions( v-for="dict in getDictOptions(
@ -166,23 +175,30 @@ onMounted(() => {
</Select> </Select>
</Form.Item> </Form.Item>
<Form.Item label="创建时间" name="createTime"> <Form.Item label="创建时间" name="createTime">
<!-- TODO @puhui999这里有个红色的告警看看有办法处理哇 -->
<RangePicker <RangePicker
v-model:value="queryParams.createTime" v-model:value="queryParams.createTime"
v-bind="getRangePickerDefaultProps()" v-bind="getRangePickerDefaultProps()"
class="!w-220px" class="w-full"
/> />
</Form.Item> </Form.Item>
<Form.Item> <Form.Item>
<!-- TODO @puhui999搜索和重置貌似样子和位置不太一样有木有办法一致 --> <!-- TODO @puhui999搜索和重置貌似样子和位置不太一样有木有办法一致 -->
<!-- TODO @puhui999收齐展开好弄哇 --> <!-- TODO @puhui999收齐展开好弄哇 -->
<Button class="ml-2" @click="handleQuery" :icon="h(Search)"> <Button class="ml-2" @click="resetQuery"> </Button>
<Button class="ml-2" @click="handleQuery" type="primary">
搜索 搜索
</Button> </Button>
<Button class="ml-2" @click="resetQuery" :icon="h(RefreshCw)"> </Form.Item>
重置 </Form>
</Button> </ContentWrap>
<!-- TODO @puhui999有办法放到 VxeTable 哪里么 -->
<!-- 列表 -->
<ContentWrap title="你好">
<template #extra>
<TableToolbar
ref="tableToolbarRef"
v-model:hidden-search="hiddenSearchBar"
>
<Button <Button
class="ml-2" class="ml-2"
:icon="h(Plus)" :icon="h(Plus)"
@ -202,14 +218,9 @@ onMounted(() => {
> >
{{ $t('ui.actionTitle.export') }} {{ $t('ui.actionTitle.export') }}
</Button> </Button>
</Form.Item> </TableToolbar>
</Form> </template>
</ContentWrap> <VxeTable ref="tableRef" :data="list" show-overflow :loading="loading">
<!-- 列表 -->
<!-- TODO @puhui999title 要不还是假起来 -->
<ContentWrap>
<VxeTable :data="list" show-overflow :loading="loading">
<VxeColumn field="id" title="编号" align="center" /> <VxeColumn field="id" title="编号" align="center" />
<VxeColumn field="name" title="名字" align="center" /> <VxeColumn field="name" title="名字" align="center" />
<VxeColumn field="sex" title="性别" align="center"> <VxeColumn field="sex" title="性别" align="center">

View File

@ -26,11 +26,9 @@ import { ImageUpload } from '#/components/upload';
import { $t } from '#/locales'; import { $t } from '#/locales';
import { DICT_TYPE, getDictOptions } from '#/utils/dict'; import { DICT_TYPE, getDictOptions } from '#/utils/dict';
const emit = defineEmits(['success']); // TODO @puhui999emit const emit = defineEmits(['success']);
const formRef = ref(); const formRef = ref();
// TODO @puhui999labelColwrapperCol
const labelCol = { span: 5 };
const wrapperCol = { span: 13 };
const formData = ref<Partial<Demo01ContactApi.Demo01Contact>>({ const formData = ref<Partial<Demo01ContactApi.Demo01Contact>>({
id: undefined, id: undefined,
name: undefined, name: undefined,
@ -90,8 +88,7 @@ const [Modal, modalApi] = useVbenModal({
resetForm(); resetForm();
return; return;
} }
//
// TODO @puhui999
let data = modalApi.getData<Demo01ContactApi.Demo01Contact>(); let data = modalApi.getData<Demo01ContactApi.Demo01Contact>();
if (!data) { if (!data) {
return; return;
@ -115,8 +112,8 @@ const [Modal, modalApi] = useVbenModal({
ref="formRef" ref="formRef"
:model="formData" :model="formData"
:rules="rules" :rules="rules"
:label-col="labelCol" :label-col="{ span: 5 }"
:wrapper-col="wrapperCol" :wrapper-col="{ span: 18 }"
> >
<Form.Item label="名字" name="name"> <Form.Item label="名字" name="name">
<Input v-model:value="formData.name" placeholder="请输入名字" /> <Input v-model:value="formData.name" placeholder="请输入名字" />