Commit 14a5f8df by chenwq

```

feat(SearchForm): 添加分页参数支持并优化重置逻辑

- 新增 SearchPagination 接口定义
- 在 SearchForm 组件中添加 pagination 属性,支持传入分页参数
- 修改 onSearch 和 onReset 回调函数签名,增加分页参数
- 优化重置功能,现在会将输入字段重置为空字符串而非 undefined
- 更新相关测试用例以验证新的分页参数传递逻辑
- 在 RoleManage 页面中集成新的分页参数处理机制
```
1 parent cbad90be
Pipeline #302 for 14a5f8df failed in 0 seconds
import { UButton, UDatePicker, UForm, UFormItem, UInput, URadio, USelect } from '@uj/ui';
import type { FieldSchema } from '../formSchema';
export interface SearchPagination {
current: number;
size: number;
}
export interface SearchFormProps<T extends Record<string, unknown>> {
fields: FieldSchema<T>[];
initialValues?: Partial<T>;
onSearch: (values: T) => void;
onReset?: (values: Partial<T>) => void;
/** 查询或重置时传出的分页参数,默认第 1 页、每页 20 条。 */
pagination?: Partial<SearchPagination>;
onSearch: (values: T, pagination: SearchPagination) => void;
onReset?: (values: Partial<T>, pagination: SearchPagination) => void;
}
export function SearchForm<T extends Record<string, unknown>>({
fields,
initialValues,
pagination,
onSearch,
onReset,
}: SearchFormProps<T>) {
const [form] = UForm.useForm();
const searchPagination: SearchPagination = {
current: pagination?.current ?? 1,
size: pagination?.size ?? 20,
};
const control = (field: FieldSchema<T>) => {
const common = {
disabled: typeof field.disabled === 'boolean' ? field.disabled : false,
......@@ -45,7 +57,7 @@ export function SearchForm<T extends Record<string, unknown>>({
form={form}
initialValues={initialValues}
layout="inline"
onFinish={(values) => onSearch(values as T)}
onFinish={(values) => onSearch(values as T, searchPagination)}
>
{fields.map((field) => (
<UFormItem key={field.name} name={field.name as string} label={field.label}>
......@@ -57,8 +69,15 @@ export function SearchForm<T extends Record<string, unknown>>({
<UButton
type="default"
onClick={() => {
form.setFieldsValue(initialValues);
onReset?.(initialValues ?? {});
const resetValues = fields.reduce<Record<string, unknown>>((values, field) => {
const initialValue = initialValues?.[field.name];
values[field.name] =
initialValue ??
(field.type === 'input' || field.type === 'textarea' ? '' : undefined);
return values;
}, {});
form.setFieldsValue(resetValues);
onReset?.(initialValues ?? {}, searchPagination);
}}
>
重置
......
import { fireEvent, render, screen } from '@testing-library/react';
import { fireEvent, render, screen, waitFor, within } from '@testing-library/react';
import { describe, expect, it, vi } from 'vitest';
import { SearchForm } from '../SearchForm';
......@@ -17,6 +17,24 @@ describe('SearchForm', () => {
fireEvent.change(input, { target: { value: '已修改' } });
fireEvent.click(screen.getByRole('button', { name: /重\s*置/ }));
expect(input).toHaveValue('初始值');
expect(onReset).toHaveBeenCalledWith({ name: '初始值' });
expect(onReset).toHaveBeenCalledWith({ name: '初始值' }, { current: 1, size: 20 });
});
it('查询时传出页面配置的分页参数', async () => {
const onSearch = vi.fn();
const { container } = render(
<SearchForm
fields={[{ name: 'keyword', label: '关键字', type: 'input' }]}
initialValues={{}}
pagination={{ current: 1, size: 50 }}
onSearch={onSearch}
/>,
);
fireEvent.click(within(container).getByRole('button', { name: /查\s*询/ }));
await waitFor(() => {
expect(onSearch).toHaveBeenCalledWith({ keyword: undefined }, { current: 1, size: 50 });
});
});
});
......@@ -2,7 +2,7 @@ export { Logo } from './Logo';
export { StatusTag } from './StatusTag';
export { default as PagePlaceholder } from './PagePlaceholder';
export { SearchForm } from './SearchForm';
export type { SearchFormProps } from './SearchForm';
export type { SearchFormProps, SearchPagination } from './SearchForm';
export type { ActionSchema, FieldOption, FieldSchema, FieldType } from './formSchema';
export { DataTable } from './DataTable';
export type { DataTableProps, RowActionSchema } from './DataTable';
......
import { useMemo, useState } from 'react';
import { useQuery } from '@tanstack/react-query';
import { PlusOutlined } from '@ant-design/icons';
import type { ColumnsType } from 'antd/es/table';
import { UAlert, UButton, UCard, UTag, UText, UTitle } from '@uj/ui';
import { DataTable, SearchForm, type FieldSchema, type RowActionSchema } from '@/components';
import { UAlert, UCard, UTag, UText, UTitle } from '@uj/ui';
import {
DataTable,
SearchForm,
type FieldSchema,
type RowActionSchema,
type SearchPagination,
} from '@/components';
import { P, PermButton } from '@/access';
import { role, type RolePageParams } from '@/api';
import { getBaseList } from './tools/constants';
......@@ -19,6 +24,21 @@ interface Row {
status?: number;
}
function createRolePageParams(
values: Record<string, unknown>,
pagination: SearchPagination,
): RolePageParams {
return {
current: pagination.current,
size: pagination.size,
keyword: values.keyword as string | undefined,
isBuiltin: values.type === '' || values.type === undefined ? undefined : Number(values.type),
status: values.status === '' || values.status === undefined ? undefined : Number(values.status),
defaultDataScope:
values.scope === '' || values.scope === undefined ? undefined : Number(values.scope),
};
}
export default function RoleManage() {
const [params, setParams] = useState<RolePageParams>({
current: 1,
......@@ -28,7 +48,6 @@ export default function RoleManage() {
defaultDataScope: undefined,
});
const [queryVersion, setQueryVersion] = useState(0);
const [page, setPage] = useState(1);
const {
data: result,
isLoading,
......@@ -125,21 +144,12 @@ export default function RoleManage() {
<SearchForm
fields={fields}
initialValues={{ type: '', status: '', scope: '' }}
onSearch={(v) => {
setPage(1);
setParams({
current: 1,
size: 20,
keyword: v.keyword as string,
isBuiltin: v.type === '' ? undefined : Number(v.type),
status: v.status === '' ? undefined : Number(v.status),
defaultDataScope: v.scope === '' ? undefined : Number(v.scope),
});
onSearch={(values, pagination) => {
setParams(createRolePageParams(values, pagination));
setQueryVersion((version) => version + 1);
}}
onReset={() => {
setPage(1);
setParams({ current: 1, size: 20 });
onReset={(values, pagination) => {
setParams(createRolePageParams(values, pagination));
setQueryVersion((version) => version + 1);
}}
/>
......@@ -157,11 +167,10 @@ export default function RoleManage() {
pageData={result ? { ...result, records: data } : undefined}
recordsKey="records"
pagination={{
current: page,
current: params.current ?? 1,
pageSize: params.size ?? 20,
total: result?.total ?? 0,
onChange: (current, size) => {
setPage(current);
setParams((prev) => ({ ...prev, current, size }));
setQueryVersion((version) => version + 1);
},
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!