index.tsx
9.34 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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,也不在别处维护第二份「常显字段」清单,
* 那样改一次要改两处、还容易漏。
*
* 时间区间(dateRange)的常显规则例外:它默认进「更多筛选」(区间控件要跨 2 列才完整),
* 但「轻量表单」(常显非日期字段 ≤2 且整页条件 ≤3,如积分详情只有积分类型 + 积分变化时间)
* 或显式标了 essential:true 的区间会直接常显,避免「就两三个条件」的页还要点开更多。
* 具体阈值见组件内的 dateInline 启发式。
*/
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,
};
const hasAnyEssential = fields.some((field) => field.essential);
// 常显的非日期字段:标了 essential 的,或「全都不标 essential」时全部常显(门店列表这类没有
// 重点字段的页,非日期字段仍作常显锚点,避免常显行空着只剩按钮)。
const primaryNonDate = fields.filter(
(field) => field.type !== 'dateRange' && (field.essential === true || !hasAnyEssential),
);
const dateFields = fields.filter((field) => field.type === 'dateRange');
// 时间区间是否常显(不进「更多筛选」)的启发式:
// - 显式 essential:true 的日期区间强制常显(页面作者明确要它常显);
// - 否则只在「轻量表单」里常显 —— 常显的非日期字段很少(≤2)且整页条件总数也少(≤3),
// 即「就两三个查询条件」的页面(如积分详情:积分类型 + 积分变化时间),
// 区间直接常显更顺手,不必点开「更多筛选」;
// - 没有任何非日期常显字段时(纯日期区间表单),也常显,避免常显行空着却把日期收进更多;
// 其余(字段多 / 非日期常显已 ≥3)保持原行为:日期区间进「更多筛选」,
// 防止把常显行挤爆、把「查询 / 重置」挤到第二行。
// 区间控件要显示「开始 ~ 结束」,放进更多时会跨 2 列(见 renderField 的 span)才完整。
const lightForm = primaryNonDate.length <= 2 && fields.length <= 3;
const dateInline = dateFields.length > 0 && (primaryNonDate.length === 0 || lightForm);
const isPrimary = (field: FieldSchema<T>): boolean => {
if (field.type === 'dateRange') return field.essential === true || dateInline;
return field.essential === true || !hasAnyEssential;
};
const primaryFields = fields.filter(isPrimary);
const extraFields = fields.filter((field) => !isPrimary(field));
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>) => {
// 时间区间选择器默认跨 2 列(见 FieldSchema.span 注释);其余类型默认 1,
// 显式传 span 可覆盖。跨列只是「腾出空间」,再用 maxWidth 把控件卡在合理宽度:
// 既够显示「开始 ~ 结束」(约 236px 控件),又不会被宽屏的 auto-fit 撑到整行那么宽。
const span = field.span ?? (field.type === 'dateRange' ? 2 : 1);
const style: React.CSSProperties = { marginBottom: 0, minWidth: 0 };
if (span > 1) style.gridColumn = `span ${span}`;
if (field.type === 'dateRange') style.maxWidth = 340;
return (
<UFormItem key={field.name} name={field.name as string} label={field.label} style={style}>
{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;
}