index.tsx
6.88 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { useState } from 'react';
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;
}
/** 标签列:文字区 96px + 与控件之间 8px,控件因此收窄约 1/3 且左缘对齐。 */
const LABEL_COL = { style: { width: 104, paddingInlineEnd: 8 } };
export interface SearchFormProps<T extends Record<string, unknown>> {
/**
* 字段表。`essential: true` 的字段常显,其余收进「更多筛选」。
*
* 真源就在字段定义那一行 —— 哪个条件重要是一个业务判断,只有写它的人知道;
* 不在 SearchForm 上传 visibleCount,也不在别处维护第二份「常显字段」清单,
* 那样改一次要改两处、还容易漏。
*/
fields: FieldSchema<T>[];
initialValues?: Partial<T>;
/** 查询或重置时传出的分页参数,默认第 1 页、每页 10 条。 */
pagination?: Partial<SearchPagination>;
onSearch: (values: T, pagination: SearchPagination) => void;
onReset?: (values: Partial<T>, pagination: SearchPagination) => void;
className?: string;
/** 默认是否展开「更多筛选」,默认收起。 */
defaultExpanded?: boolean;
}
export function SearchForm<T extends Record<string, unknown>>({
fields,
initialValues,
pagination,
onSearch,
onReset,
className,
defaultExpanded = false,
}: SearchFormProps<T>) {
const [form] = UForm.useForm();
// 空数组 = 监听整张表单。用于「收起状态下已设条件」的角标计数:
// 收起了不等于没生效,用户必须看得见自己身上还挂着几个条件。
const values = UForm.useWatch([], form) as Partial<T> | undefined;
const [expanded, setExpanded] = useState(defaultExpanded);
const searchPagination: SearchPagination = {
current: pagination?.current ?? 1,
size: pagination?.size ?? DEFAULT_PAGE_SIZE,
};
// 一条 essential 都没有时保持原行为:全部常显、不出现「更多筛选」。
// 让页面可以逐批迁移,不会因为共享层升级就突然把条件藏起来。
const hasSplit = fields.some((field) => field.essential);
const primaryFields = hasSplit ? fields.filter((field) => field.essential) : fields;
const extraFields = hasSplit ? fields.filter((field) => !field.essential) : [];
const activeExtraCount = extraFields.filter((field) => isSet(values?.[field.name])).length;
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 }} />
);
};
const renderField = (field: FieldSchema<T>) => (
<UFormItem
key={field.name}
name={field.name as string}
label={field.label}
style={{ marginBottom: 0, minWidth: 0 }}
>
{control(field)}
</UFormItem>
);
return (
<UForm
form={form}
initialValues={initialValues}
// 标签在左、控件在右:标签列宽固定,四组控件的左缘落在同一条竖线上。
// 竖排标签会把筛选区抬高一整行,横向排列后整条筛选带只占控件本身的高度。
layout="horizontal"
labelCol={LABEL_COL}
colon={false}
className={className}
size="small"
onFinish={(submitValues) => onSearch(submitValues as T, searchPagination)}
>
{/* 字段与动作同处一行:动作贴最后一列底部,筛选区整体只占一条 72px 左右的横带,
而不是「一排字段 + 单独一排按钮」。窄屏由 ui.css 的媒体查询退回整行。 */}
<div className="uj-filter-grid">
{primaryFields.map(renderField)}
<div className="uj-filter-actions">
{extraFields.length ? (
<UButton
type="text"
size="small"
className="uj-filter-more-btn"
onClick={() => setExpanded((open) => !open)}
aria-expanded={expanded}
>
{expanded ? '收起' : '更多筛选'}
{!expanded && activeExtraCount > 0 ? (
<span className="uj-filter-more-count">{activeExtraCount}</span>
) : null}
<span className={`uj-filter-toggle-icon${expanded ? ' is-open' : ''}`} aria-hidden>
▾
</span>
</UButton>
) : null}
<UButton
type="default"
size="small"
onClick={() => {
const resetValues = fields.reduce<Record<string, unknown>>((acc, field) => {
const initialValue = initialValues?.[field.name];
acc[field.name] =
initialValue ??
(field.type === 'input' || field.type === 'textarea' ? '' : undefined);
return acc;
}, {});
form.setFieldsValue(resetValues);
onReset?.(initialValues ?? {}, searchPagination);
}}
>
重置
</UButton>
<UButton size="small" htmlType="submit">查询</UButton>
</div>
</div>
{/* 收起用 CSS 隐藏而不是卸载:antd 的 Form.Item 一旦卸载就把值从 store 里丢掉,
于是「设了条件 → 收起 → 查询」会静默变成不带该条件,属于最难排查的一类 bug。 */}
{extraFields.length ? (
<div className="uj-filter-more" style={expanded ? undefined : { display: 'none' }}>
{extraFields.map(renderField)}
</div>
) : null}
</UForm>
);
}
/** 一个条件算「已设」:非空、非 undefined,且不是只含空值的数组(级联的 [''] 属于没选)。 */
function isSet(value: unknown): boolean {
if (value === undefined || value === null || value === '') return false;
if (Array.isArray(value)) return value.some((item) => isSet(item));
return true;
}