refactor(product): 搜索表单改用useVbenForm,优化flex布局结构

This commit is contained in:
小蟑螂
2026-04-28 09:12:38 +08:00
parent a838b92fec
commit f03e9a0e07
@@ -9,18 +9,9 @@ import 'vue-waterfall-plugin-next/dist/style.css';
import { Page } from '@vben/common-ui';
import { createIconifyIcon } from '@vben/icons';
import {
Button,
Card,
Empty,
Flex,
Form,
Input,
message,
Space,
Spin,
} from 'ant-design-vue';
import { Button, Card, Empty, message, Spin } from 'ant-design-vue';
import { useVbenForm } from '#/adapter/form';
import { deleteProductApi, getProductPageApi } from '#/api/ecommerce/product';
import ProductCard from './components/ProductCard.vue';
@@ -39,6 +30,98 @@ const searchForm = ref({
specName: '',
});
const searchFormSubmitRef = ref<null | (() => Promise<unknown>)>(null);
const [SearchForm, formApi] = useVbenForm({
commonConfig: {
componentProps: {
class: 'w-full',
},
},
handleReset: handleSearchFormReset,
handleSubmit: handleSearchFormSubmit,
layout: 'horizontal',
schema: [
{
component: 'Input',
componentProps: {
placeholder: '请输入商品名称(回车搜索)',
allowClear: true,
onPressEnter: () => void searchFormSubmitRef.value?.(),
},
defaultValue: '',
fieldName: 'name',
label: '商品名称',
},
{
component: 'Input',
componentProps: {
placeholder: '请输入商品编码(回车搜索)',
allowClear: true,
onPressEnter: () => void searchFormSubmitRef.value?.(),
},
defaultValue: '',
fieldName: 'productCode',
label: '商品编码',
},
{
component: 'Input',
componentProps: {
placeholder: '请输入规格编码(回车搜索)',
allowClear: true,
onPressEnter: () => void searchFormSubmitRef.value?.(),
},
defaultValue: '',
fieldName: 'specCode',
label: '规格编码',
},
{
component: 'Input',
componentProps: {
placeholder: '请输入规格名称(回车搜索)',
allowClear: true,
onPressEnter: () => void searchFormSubmitRef.value?.(),
},
defaultValue: '',
fieldName: 'specName',
label: '规格名称',
},
],
showCollapseButton: false,
showDefaultActions: true,
submitButtonOptions: {
content: '搜索',
},
resetButtonOptions: {
content: '重置',
},
submitOnEnter: true,
wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3',
});
searchFormSubmitRef.value = () => formApi.validateAndSubmitForm();
async function handleSearchFormSubmit(values: Record<string, any>) {
searchForm.value = {
name: String(values.name ?? '').trim(),
productCode: String(values.productCode ?? '').trim(),
specCode: String(values.specCode ?? '').trim(),
specName: String(values.specName ?? '').trim(),
};
runSearch();
}
async function handleSearchFormReset() {
await formApi.resetForm();
searchForm.value = {
name: '',
productCode: '',
specCode: '',
specName: '',
};
runSearch();
}
const loading = ref(false);
const productList = ref<ProductVO.Info[]>([]);
const total = ref(0);
@@ -87,22 +170,12 @@ const loadProductList = async (reset = false) => {
}
};
const handleSearch = () => {
function runSearch() {
current.value = 1;
productList.value = [];
hasMore.value = true;
loadProductList(true);
};
const handleReset = () => {
searchForm.value = {
name: '',
productCode: '',
specCode: '',
specName: '',
};
handleSearch();
};
}
const handleScroll = () => {
const container = scrollContainerRef.value;
@@ -170,130 +243,98 @@ onUnmounted(() => {
</script>
<template>
<Page :auto-content-height="true" content-class="flex-col dark:bg-[#0f1623]">
<Page
auto-content-height
content-class="flex min-h-0 flex-1 flex-col dark:bg-[#0f1623]"
>
<div class="flex min-h-0 flex-1 flex-col dark:bg-[#0f1623]">
<div class="flex min-h-0 flex-1 flex-col">
<Card class="mb-4 shrink-0" :body-style="{ padding: '16px' }">
<Form :model="searchForm" layout="inline" class="w-full">
<Flex class="w-full" justify="end" align="center" gap="4">
<Form.Item label="商品名称" class="mb-0">
<Input
v-model:value="searchForm.name"
placeholder="请输入商品名称"
style="width: 200px"
allow-clear
@press-enter="handleSearch"
/>
</Form.Item>
<Form.Item label="商品编码" class="mb-0">
<Input
v-model:value="searchForm.productCode"
placeholder="请输入商品编码"
style="width: 150px"
allow-clear
@press-enter="handleSearch"
/>
</Form.Item>
<Form.Item label="规格编码" class="mb-0">
<Input
v-model:value="searchForm.specCode"
placeholder="请输入规格编码"
style="width: 150px"
allow-clear
@press-enter="handleSearch"
/>
</Form.Item>
<Form.Item label="规格名称" class="mb-0">
<Input
v-model:value="searchForm.specName"
placeholder="请输入规格名称"
style="width: 150px"
allow-clear
@press-enter="handleSearch"
/>
</Form.Item>
<Form.Item class="mb-0">
<Space>
<Button type="primary" @click="handleSearch">搜索</Button>
<Button @click="handleReset">重置</Button>
</Space>
</Form.Item>
<div class="ml-auto text-sm text-gray-500 dark:text-gray-400">
共 {{ total }} 个商品
</div>
</Flex>
</Form>
<SearchForm />
</Card>
<!-- 主体占满剩余高度且不向外溢出;自身不滚动 -->
<div
ref="scrollContainerRef"
class="min-h-0 flex-1 overflow-y-auto"
class="flex min-h-0 min-w-0 flex-1 flex-col overflow-hidden"
>
<Card
class="flex min-h-full flex-col dark:bg-[#111827] dark:border-[#374151]"
:body-style="{
padding: '16px',
display: 'flex',
flexDirection: 'column',
minHeight: '100%',
}"
>
<div class="mb-3 flex shrink-0 justify-end">
<Button type="primary" @click="handleAdd">
<PlusIcon class="mr-2 h-4 w-4" />
添加商品
</Button>
</div>
<Spin :spinning="loading && productList.length === 0">
<Card
class="flex min-h-0 min-w-0 flex-1 flex-col overflow-hidden dark:bg-[#111827] dark:border-[#374151]"
:body-style="{
padding: '16px',
display: 'flex',
flexDirection: 'column',
flex: '1 1 0',
minHeight: 0,
overflow: 'hidden',
}"
>
<div
v-if="!loading && productList.length === 0"
class="flex h-full items-center justify-center py-8"
class="mb-3 flex shrink-0 items-center justify-between gap-3"
>
<Empty />
<span class="text-sm text-gray-500 dark:text-gray-400">
共 {{ total }} 个商品
</span>
<Button type="primary" @click="handleAdd">
<PlusIcon class="mr-2 h-4 w-4" />
添加商品
</Button>
</div>
<Waterfall
v-else-if="productList.length > 0"
ref="waterfallRef"
:list="productList"
row-key="id"
:width="700"
:breakpoints="{
2700: { rowPerView: 3 },
2000: { rowPerView: 2 },
1400: { rowPerView: 2 },
900: { rowPerView: 1 },
500: { rowPerView: 1 },
}"
:gutter="20"
class="product-waterfall"
<!-- 仅列表区域滚动,用于瀑布流与触底加载 -->
<div
ref="scrollContainerRef"
class="flex min-h-0 min-w-0 flex-1 flex-col overflow-y-auto"
>
<template #default="{ item }">
<div class="w-full">
<ProductCard
:product="item"
@updated="(p) => refreshTable(p)"
@delete="handleDelete"
@editing-change="handleEditingChange"
/>
<Spin :spinning="loading && productList.length === 0">
<div
v-if="!loading && productList.length === 0"
class="flex min-h-[200px] items-center justify-center py-8"
>
<Empty />
</div>
</template>
</Waterfall>
<div
v-if="productList.length > 0"
class="mt-4 flex justify-center py-4"
>
<Spin v-if="loading" />
<div
v-else-if="!hasMore"
class="text-sm text-gray-500 dark:text-gray-400"
>
没有更多商品了
</div>
<Waterfall
v-else-if="productList.length > 0"
ref="waterfallRef"
:list="productList"
row-key="id"
:width="700"
:breakpoints="{
2700: { rowPerView: 3 },
2000: { rowPerView: 2 },
1400: { rowPerView: 2 },
900: { rowPerView: 1 },
500: { rowPerView: 1 },
}"
:gutter="20"
class="product-waterfall"
>
<template #default="{ item }">
<div class="w-full">
<ProductCard
:product="item"
@updated="(p) => refreshTable(p)"
@delete="handleDelete"
@editing-change="handleEditingChange"
/>
</div>
</template>
</Waterfall>
<div
v-if="productList.length > 0"
class="mt-4 flex justify-center py-4"
>
<Spin v-if="loading" />
<div
v-else-if="!hasMore"
class="text-sm text-gray-500 dark:text-gray-400"
>
没有更多商品了
</div>
</div>
</Spin>
</div>
</Spin>
</Card>
</Card>
</div>
</div>
</div>