index.tsx
3.74 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { UCascader, UButton, UDatePicker, UForm, UFormItem, UInput, URadio, USelect } from '@uj/ui';
import type { FieldSchema } from '../formSchema';
import { DEFAULT_PAGE_SIZE } from '../DataTable';
export interface SearchPagination {
current: number;
size: number;
}
export interface SearchFormProps<T extends Record<string, unknown>> {
fields: FieldSchema<T>[];
initialValues?: Partial<T>;
/** 查询或重置时传出的分页参数,默认第 1 页、每页 10 条。 */
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 = 'vertical',
className,
}: SearchFormProps<T>) {
const [form] = UForm.useForm();
const searchPagination: SearchPagination = {
current: pagination?.current ?? 1,
size: pagination?.size ?? DEFAULT_PAGE_SIZE,
};
const control = (field: FieldSchema<T>) => {
const common = {
disabled: typeof field.disabled === 'boolean' ? field.disabled : false,
placeholder: field.placeholder,
};
const width = field.width ?? '100%';
if (field.type === 'select' || field.type === 'selectMultiple')
return (
<USelect
{...common}
allowClear={false}
style={{ width }}
options={field.options}
mode={field.type === 'selectMultiple' ? 'multiple' : undefined}
{...field.selectProps}
/>
);
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} 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 === 'inline' ? 'vertical' : layout}
className={className}
style={searchFormStyle}
onFinish={(values) => onSearch(values as T, searchPagination)}
>
{fields.map((field) => (
<UFormItem
key={field.name}
name={field.name as string}
label={field.label}
style={{ marginBottom: 0, minWidth: 0 }}
>
{control(field)}
</UFormItem>
))}
<UFormItem style={searchActionsStyle}>
<UButton
type="default"
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" style={{ marginLeft: 8 }}>
查询
</UButton>
</UFormItem>
</UForm>
);
}
const searchFormStyle = {
columnGap: 16,
display: 'grid',
gridTemplateColumns: 'repeat(4, minmax(0, 1fr))',
rowGap: 12,
};
const searchActionsStyle = {
display: 'flex',
gridColumn: '1 / -1',
justifyContent: 'flex-end',
marginBottom: 0,
};