index.tsx
3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { UCascader, 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>;
/** 查询或重置时传出的分页参数,默认第 1 页、每页 20 条。 */
pagination?: Partial<SearchPagination>;
onSearch: (values: T, pagination: SearchPagination) => void;
onReset?: (values: Partial<T>, pagination: SearchPagination) => void;
layout?: 'inline' | 'vertical';
className?: string;
}
export function SearchForm<T extends Record<string, unknown>>({
fields,
initialValues,
pagination,
onSearch,
onReset,
layout = 'inline',
className,
}: 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,
placeholder: field.placeholder,
size: 'small' as const,
};
const width = field.width ?? (field.type === 'input' || field.type === 'textarea' ? 260 : 160);
if (field.type === 'select' || field.type === 'selectMultiple')
return (
<USelect
{...common}
allowClear={false}
style={{ width }}
options={field.options}
mode={field.type === 'selectMultiple' ? 'multiple' : undefined}
/>
);
if (field.type === 'cascader')
return <UCascader {...common} style={{ width }} options={field.options} allowClear={false} />;
if (field.type === 'date') return <UDatePicker {...common} style={{ width }} />;
if (field.type === 'dateRange')
return (
<UDatePicker.RangePicker disabled={common.disabled} size={common.size} style={{ width }} />
);
if (field.type === 'radio') return <URadio.Group {...common} options={field.options} />;
return field.type === 'textarea' ? (
<UInput.TextArea {...common} style={{ width }} />
) : (
<UInput {...common} style={{ width }} />
);
};
return (
<UForm
form={form}
initialValues={initialValues}
layout={layout}
className={className}
onFinish={(values) => onSearch(values as T, searchPagination)}
>
{fields.map((field) => (
<UFormItem key={field.name} name={field.name as string} label={field.label}>
{control(field)}
</UFormItem>
))}
<UFormItem>
<UButton
type="default"
size="small"
onClick={() => {
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);
}}
>
重置
</UButton>
<UButton htmlType="submit" size="small" style={{ marginLeft: 8 }}>
查询
</UButton>
</UFormItem>
</UForm>
);
}