@hsu-react/ui 0.0.9 → 0.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/es/components/FormItem/ItemContainer/index.d.ts +2 -0
- package/es/components/FormItem/ItemContainer/index.js +4 -15
- package/es/components/Search/{_components → SearchAdvanced/_components}/AdvancedFiltersDrawer/index.d.ts +3 -3
- package/es/components/Search/{_components → SearchAdvanced/_components}/AdvancedFiltersDrawer/index.js +3 -3
- package/es/components/Search/SearchAdvanced/index.js +26 -148
- package/es/components/Search/SearchCard/index.js +1 -1
- package/es/components/Search/{_components → SearchCollapsible/_components}/CollapseToggle/index.js +2 -2
- package/es/components/Search/SearchCollapsible/index.js +17 -138
- package/es/components/Search/SearchWithFilter/index.js +4 -143
- package/es/components/Search/SearchWithMore/index.js +11 -142
- package/es/components/Search/_components/FilterDropdown/index.js +5 -6
- package/es/components/Search/_components/SearchBase/index.d.ts +32 -0
- package/es/components/Search/_components/SearchBase/index.js +185 -0
- package/es/components/Search/_hooks/useSearchCommon.js +5 -11
- package/es/components/Search/_hooks/useSearchExpand.js +4 -13
- package/es/components/Search/_utils/calculateButtonGroupAndColumnWidth.js +3 -10
- package/es/components/Search/_utils/calculateVisibleItems.js +4 -5
- package/es/components/Search/_utils/index.d.ts +2 -0
- package/es/components/Search/_utils/index.js +3 -1
- package/es/components/Search/_utils/measureButtonGroupWidth.d.ts +6 -0
- package/es/components/Search/_utils/measureButtonGroupWidth.js +18 -0
- package/es/components/Search/_utils/shouldDisplayExtraItem.d.ts +11 -0
- package/es/components/Search/_utils/shouldDisplayExtraItem.js +13 -0
- package/es/components/Search/index.d.ts +4 -0
- package/es/components/Search/index.js +6 -133
- package/es/components/Table/_hooks/useTableColumns.js +11 -0
- package/lib/components/FormItem/ItemContainer/index.d.ts +2 -0
- package/lib/components/FormItem/ItemContainer/index.js +2 -5
- package/lib/components/Search/{_components → SearchAdvanced/_components}/AdvancedFiltersDrawer/index.d.ts +3 -3
- package/lib/components/Search/{_components → SearchAdvanced/_components}/AdvancedFiltersDrawer/index.js +3 -3
- package/lib/components/Search/SearchAdvanced/index.js +19 -134
- package/lib/components/Search/SearchCard/index.js +1 -1
- package/lib/components/Search/{_components → SearchCollapsible/_components}/CollapseToggle/index.js +2 -2
- package/lib/components/Search/SearchCollapsible/index.js +12 -124
- package/lib/components/Search/SearchWithFilter/index.js +5 -129
- package/lib/components/Search/SearchWithMore/index.js +7 -126
- package/lib/components/Search/_components/FilterDropdown/index.js +5 -6
- package/lib/components/Search/_components/SearchBase/index.d.ts +32 -0
- package/lib/components/Search/_components/SearchBase/index.js +169 -0
- package/lib/components/Search/_hooks/useSearchCommon.js +4 -6
- package/lib/components/Search/_hooks/useSearchExpand.js +2 -13
- package/lib/components/Search/_utils/calculateButtonGroupAndColumnWidth.js +2 -10
- package/lib/components/Search/_utils/calculateVisibleItems.js +3 -5
- package/lib/components/Search/_utils/index.d.ts +2 -0
- package/lib/components/Search/_utils/index.js +15 -1
- package/lib/components/Search/_utils/measureButtonGroupWidth.d.ts +6 -0
- package/lib/components/Search/_utils/measureButtonGroupWidth.js +24 -0
- package/lib/components/Search/_utils/shouldDisplayExtraItem.d.ts +11 -0
- package/lib/components/Search/_utils/shouldDisplayExtraItem.js +19 -0
- package/lib/components/Search/index.d.ts +4 -0
- package/lib/components/Search/index.js +8 -119
- package/lib/components/Table/_hooks/useTableColumns.js +11 -0
- package/package.json +1 -1
- package/src/components/FormItem/ItemContainer/index.tsx +4 -7
- package/src/components/Search/{_components → SearchAdvanced/_components}/AdvancedFiltersDrawer/index.tsx +6 -6
- package/src/components/Search/SearchAdvanced/index.tsx +25 -164
- package/src/components/Search/SearchCard/index.tsx +1 -1
- package/src/components/Search/{_components → SearchCollapsible/_components}/CollapseToggle/index.tsx +2 -2
- package/src/components/Search/SearchCollapsible/index.tsx +13 -162
- package/src/components/Search/SearchWithFilter/index.tsx +2 -164
- package/src/components/Search/SearchWithMore/index.tsx +9 -161
- package/src/components/Search/_components/FilterDropdown/index.tsx +5 -6
- package/src/components/Search/_components/SearchBase/index.tsx +224 -0
- package/src/components/Search/_hooks/useSearchCommon.ts +10 -13
- package/src/components/Search/_hooks/useSearchExpand.ts +3 -13
- package/src/components/Search/_utils/calculateButtonGroupAndColumnWidth.ts +2 -10
- package/src/components/Search/_utils/calculateVisibleItems.ts +10 -11
- package/src/components/Search/_utils/index.ts +2 -0
- package/src/components/Search/_utils/measureButtonGroupWidth.ts +23 -0
- package/src/components/Search/_utils/shouldDisplayExtraItem.ts +23 -0
- package/src/components/Search/index.tsx +7 -153
- package/src/components/Table/_hooks/useTableColumns.tsx +17 -0
- /package/es/components/Search/{_components → SearchCollapsible/_components}/CollapseToggle/index.d.ts +0 -0
- /package/lib/components/Search/{_components → SearchCollapsible/_components}/CollapseToggle/index.d.ts +0 -0
|
@@ -1,15 +1,7 @@
|
|
|
1
1
|
import React from "react";
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import { CSSProperties } from "react";
|
|
5
|
-
import classNames from "classnames";
|
|
6
|
-
import styles from "../index.module.scss";
|
|
7
|
-
import { useSearchCommon } from "../_hooks";
|
|
2
|
+
import { FormItemProps } from "../../FormItem";
|
|
3
|
+
import SearchBase from "../_components/SearchBase";
|
|
8
4
|
import { ChakraButtonProps } from "../../Button";
|
|
9
|
-
import { ButtonGroup } from "../_components/ButtonGroup";
|
|
10
|
-
import { SearchButtons } from "../_components/SearchButtons";
|
|
11
|
-
import { FilterDropdown } from "../_components/FilterDropdown";
|
|
12
|
-
import { ExpandButton } from "../_components/ExpandButton";
|
|
13
5
|
import { SearchPropsWithFilter } from "../_types";
|
|
14
6
|
|
|
15
7
|
export interface SearchWithMoreProps
|
|
@@ -26,159 +18,15 @@ export interface SearchWithMoreProps
|
|
|
26
18
|
* - 适合有大量搜索字段的场景
|
|
27
19
|
*/
|
|
28
20
|
const SearchWithMore: React.FC<SearchWithMoreProps> = (props) => {
|
|
29
|
-
const {
|
|
30
|
-
searchItems = [],
|
|
31
|
-
moreSearchItems,
|
|
32
|
-
className,
|
|
33
|
-
onSearch,
|
|
34
|
-
onReset,
|
|
35
|
-
externalForm,
|
|
36
|
-
hasPermi,
|
|
37
|
-
columnNum = 4,
|
|
38
|
-
beforeButtonGroup,
|
|
39
|
-
affterButtonGroup,
|
|
40
|
-
searchData,
|
|
41
|
-
setFilter,
|
|
42
|
-
minLabelWidth,
|
|
43
|
-
defaultExpanded = false,
|
|
44
|
-
onExpandChange,
|
|
45
|
-
autoAdaptWidth = true,
|
|
46
|
-
baseWidth,
|
|
47
|
-
onValuesChange,
|
|
48
|
-
searchDisabled = false,
|
|
49
|
-
searchText,
|
|
50
|
-
resetText,
|
|
51
|
-
onFilterChange,
|
|
52
|
-
columnOffsetWidth = 0,
|
|
53
|
-
} = props;
|
|
54
|
-
|
|
55
|
-
const [form] = AntdForm.useForm(externalForm);
|
|
56
|
-
|
|
57
|
-
const {
|
|
58
|
-
containerRef,
|
|
59
|
-
buttonGroupRef,
|
|
60
|
-
cls,
|
|
61
|
-
getLabelWidth,
|
|
62
|
-
processedSearchItems,
|
|
63
|
-
setSearchItems,
|
|
64
|
-
currentSearchItems,
|
|
65
|
-
totalColumnNum,
|
|
66
|
-
expand,
|
|
67
|
-
toggleExpand,
|
|
68
|
-
showExpandButton,
|
|
69
|
-
onSearchClick,
|
|
70
|
-
onResetClick,
|
|
71
|
-
shouldRender,
|
|
72
|
-
permitted,
|
|
73
|
-
} = useSearchCommon({
|
|
74
|
-
form,
|
|
75
|
-
searchItems,
|
|
76
|
-
moreSearchItems,
|
|
77
|
-
searchData,
|
|
78
|
-
hasPermi,
|
|
79
|
-
beforeButtonGroup,
|
|
80
|
-
affterButtonGroup,
|
|
81
|
-
columnNum,
|
|
82
|
-
autoAdaptWidth,
|
|
83
|
-
defaultExpanded,
|
|
84
|
-
onExpandChange,
|
|
85
|
-
showAllSearchItems: true, // SearchWithMore 始终显示所有搜索项
|
|
86
|
-
minLabelWidth,
|
|
87
|
-
baseWidth,
|
|
88
|
-
onSearch,
|
|
89
|
-
onReset,
|
|
90
|
-
columnOffsetWidth,
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
if (!shouldRender) {
|
|
94
|
-
return null;
|
|
95
|
-
}
|
|
21
|
+
const { setFilter, ...rest } = props;
|
|
96
22
|
|
|
97
23
|
return (
|
|
98
|
-
<
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
"--column-offset-width": `${columnOffsetWidth}px`,
|
|
105
|
-
} as CSSProperties
|
|
106
|
-
}
|
|
107
|
-
>
|
|
108
|
-
<div
|
|
109
|
-
className={classNames(styles.searchOption, styles[expand.toString()])}
|
|
110
|
-
>
|
|
111
|
-
<AntdForm
|
|
112
|
-
form={form}
|
|
113
|
-
className={classNames(styles.option, cls)}
|
|
114
|
-
onValuesChange={onValuesChange}
|
|
115
|
-
>
|
|
116
|
-
{permitted &&
|
|
117
|
-
currentSearchItems?.map((item, idx) => {
|
|
118
|
-
// 计算同一列的项(用于计算标签宽度)
|
|
119
|
-
const visibleCurrentItems = currentSearchItems.filter(
|
|
120
|
-
(i) => i.visible !== false
|
|
121
|
-
);
|
|
122
|
-
const columnIndex = idx % totalColumnNum;
|
|
123
|
-
const sameColumnItems = visibleCurrentItems.filter(
|
|
124
|
-
(_, _idx) => _idx % totalColumnNum === columnIndex
|
|
125
|
-
);
|
|
126
|
-
|
|
127
|
-
return (
|
|
128
|
-
<FormItem
|
|
129
|
-
{...item}
|
|
130
|
-
key={item.name}
|
|
131
|
-
className={classNames(styles.item, item.className, {
|
|
132
|
-
[styles.unFill]: item.width !== undefined,
|
|
133
|
-
})}
|
|
134
|
-
labelWidth={
|
|
135
|
-
item.width !== undefined
|
|
136
|
-
? undefined
|
|
137
|
-
: getLabelWidth(sameColumnItems, undefined, minLabelWidth)
|
|
138
|
-
}
|
|
139
|
-
/>
|
|
140
|
-
);
|
|
141
|
-
})}
|
|
142
|
-
{(beforeButtonGroup?.length ||
|
|
143
|
-
affterButtonGroup?.length ||
|
|
144
|
-
permitted) && (
|
|
145
|
-
<ButtonGroup
|
|
146
|
-
beforeButtonGroup={beforeButtonGroup}
|
|
147
|
-
affterButtonGroup={affterButtonGroup}
|
|
148
|
-
expandButton={
|
|
149
|
-
<ExpandButton
|
|
150
|
-
expand={expand}
|
|
151
|
-
toggleExpand={toggleExpand}
|
|
152
|
-
advancedFilters={false}
|
|
153
|
-
showExpandButton={showExpandButton}
|
|
154
|
-
showAllSearchItems={true}
|
|
155
|
-
/>
|
|
156
|
-
}
|
|
157
|
-
permitted={permitted}
|
|
158
|
-
ref={buttonGroupRef}
|
|
159
|
-
>
|
|
160
|
-
{currentSearchItems.length > 0 && (
|
|
161
|
-
<SearchButtons
|
|
162
|
-
onSearch={onSearchClick}
|
|
163
|
-
onReset={onResetClick}
|
|
164
|
-
searchDisabled={searchDisabled}
|
|
165
|
-
searchText={searchText}
|
|
166
|
-
resetText={resetText}
|
|
167
|
-
/>
|
|
168
|
-
)}
|
|
169
|
-
{setFilter && (
|
|
170
|
-
<FilterDropdown
|
|
171
|
-
searchItems={processedSearchItems}
|
|
172
|
-
originalSearchItems={searchItems}
|
|
173
|
-
setSearchItems={setSearchItems}
|
|
174
|
-
onFilterChange={onFilterChange}
|
|
175
|
-
/>
|
|
176
|
-
)}
|
|
177
|
-
</ButtonGroup>
|
|
178
|
-
)}
|
|
179
|
-
</AntdForm>
|
|
180
|
-
</div>
|
|
181
|
-
</div>
|
|
24
|
+
<SearchBase
|
|
25
|
+
{...rest}
|
|
26
|
+
// SearchWithMore 始终显示所有搜索项
|
|
27
|
+
showAllSearchItems
|
|
28
|
+
showFilter={!!setFilter}
|
|
29
|
+
/>
|
|
182
30
|
);
|
|
183
31
|
};
|
|
184
32
|
|
|
@@ -22,16 +22,15 @@ export const FilterDropdown: React.FC<FilterDropdownProps> = ({
|
|
|
22
22
|
const [open, setOpen] = useState(false);
|
|
23
23
|
|
|
24
24
|
useEffect(() => {
|
|
25
|
-
|
|
25
|
+
const handleDocumentClick = () => {
|
|
26
26
|
setOpen(false);
|
|
27
|
-
}
|
|
27
|
+
};
|
|
28
|
+
document.addEventListener("click", handleDocumentClick);
|
|
28
29
|
|
|
29
30
|
return () => {
|
|
30
|
-
document.removeEventListener("click",
|
|
31
|
-
setOpen(false);
|
|
32
|
-
});
|
|
31
|
+
document.removeEventListener("click", handleDocumentClick);
|
|
33
32
|
};
|
|
34
|
-
}, [
|
|
33
|
+
}, []);
|
|
35
34
|
|
|
36
35
|
return (
|
|
37
36
|
<Dropdown
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import React, { CSSProperties, ReactNode, useMemo } from "react";
|
|
2
|
+
import { Form as AntdForm, FormInstance } from "antd";
|
|
3
|
+
import classNames from "classnames";
|
|
4
|
+
import FormItem, { FormItemProps } from "../../../FormItem";
|
|
5
|
+
import type useLabelWidth from "../../../../hooks/useLabelWidth";
|
|
6
|
+
import styles from "../../index.module.scss";
|
|
7
|
+
import { useSearchCommon } from "../../_hooks";
|
|
8
|
+
import { ButtonGroup } from "../ButtonGroup";
|
|
9
|
+
import { SearchButtons } from "../SearchButtons";
|
|
10
|
+
import { ExpandButton } from "../ExpandButton";
|
|
11
|
+
import { FilterDropdown } from "../FilterDropdown";
|
|
12
|
+
import { SearchPropsWithFilter } from "../../_types";
|
|
13
|
+
|
|
14
|
+
/** 暴露给变体扩展渲染(如高级筛选抽屉)的上下文 */
|
|
15
|
+
export interface SearchBaseContext {
|
|
16
|
+
form: FormInstance;
|
|
17
|
+
expand: boolean;
|
|
18
|
+
setExpand?: (expand: boolean) => void;
|
|
19
|
+
processedSearchItems: FormItemProps[];
|
|
20
|
+
getLabelWidth: ReturnType<typeof useLabelWidth>[1];
|
|
21
|
+
onSearchClick: () => void;
|
|
22
|
+
onResetClick: () => void;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface SearchBaseProps extends SearchPropsWithFilter {
|
|
26
|
+
/** 高级筛选模式:搜索项不显示 label,展开按钮显示为"高级筛选" */
|
|
27
|
+
advancedFilters?: boolean;
|
|
28
|
+
/** 是否渲染 FilterDropdown(筛选器设置下拉) */
|
|
29
|
+
showFilter?: boolean;
|
|
30
|
+
/** 表单区域之后、容器之内的附加内容(如折叠开关),仅在有权限时渲染 */
|
|
31
|
+
afterForm?: ReactNode;
|
|
32
|
+
/** 容器之外的附加内容(如高级筛选抽屉),仅在有权限时渲染 */
|
|
33
|
+
renderOutside?: (ctx: SearchBaseContext) => ReactNode;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Search 各变体共享的基础实现:
|
|
38
|
+
* 表单渲染、列布局、展开/收起、按钮组、权限控制。
|
|
39
|
+
* 变体只负责自身差异(筛选器、折叠开关、高级筛选抽屉等)。
|
|
40
|
+
*/
|
|
41
|
+
const SearchBase: React.FC<SearchBaseProps> = (props) => {
|
|
42
|
+
const {
|
|
43
|
+
searchItems = [],
|
|
44
|
+
moreSearchItems = [],
|
|
45
|
+
className,
|
|
46
|
+
onSearch,
|
|
47
|
+
onReset,
|
|
48
|
+
externalForm,
|
|
49
|
+
hasPermi,
|
|
50
|
+
columnNum = 4,
|
|
51
|
+
beforeButtonGroup,
|
|
52
|
+
affterButtonGroup,
|
|
53
|
+
searchData,
|
|
54
|
+
minLabelWidth,
|
|
55
|
+
defaultExpanded = false,
|
|
56
|
+
onExpandChange,
|
|
57
|
+
autoAdaptWidth = true,
|
|
58
|
+
baseWidth,
|
|
59
|
+
onValuesChange,
|
|
60
|
+
searchDisabled = false,
|
|
61
|
+
showAllSearchItems = false,
|
|
62
|
+
searchText,
|
|
63
|
+
resetText,
|
|
64
|
+
onFilterChange,
|
|
65
|
+
columnOffsetWidth = 0,
|
|
66
|
+
advancedFilters = false,
|
|
67
|
+
showFilter = false,
|
|
68
|
+
afterForm,
|
|
69
|
+
renderOutside,
|
|
70
|
+
} = props;
|
|
71
|
+
|
|
72
|
+
const [form] = AntdForm.useForm(externalForm);
|
|
73
|
+
|
|
74
|
+
const {
|
|
75
|
+
containerRef,
|
|
76
|
+
buttonGroupRef,
|
|
77
|
+
cls,
|
|
78
|
+
getLabelWidth,
|
|
79
|
+
processedSearchItems,
|
|
80
|
+
setSearchItems,
|
|
81
|
+
currentSearchItems,
|
|
82
|
+
totalColumnNum,
|
|
83
|
+
expand,
|
|
84
|
+
setExpand,
|
|
85
|
+
toggleExpand,
|
|
86
|
+
showExpandButton,
|
|
87
|
+
onSearchClick,
|
|
88
|
+
onResetClick,
|
|
89
|
+
shouldRender,
|
|
90
|
+
permitted,
|
|
91
|
+
} = useSearchCommon({
|
|
92
|
+
form,
|
|
93
|
+
searchItems,
|
|
94
|
+
moreSearchItems,
|
|
95
|
+
searchData,
|
|
96
|
+
hasPermi,
|
|
97
|
+
beforeButtonGroup,
|
|
98
|
+
affterButtonGroup,
|
|
99
|
+
columnNum,
|
|
100
|
+
autoAdaptWidth,
|
|
101
|
+
defaultExpanded,
|
|
102
|
+
onExpandChange,
|
|
103
|
+
showAllSearchItems,
|
|
104
|
+
minLabelWidth,
|
|
105
|
+
baseWidth,
|
|
106
|
+
onSearch,
|
|
107
|
+
onReset,
|
|
108
|
+
columnOffsetWidth,
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
// 按列分组预计算(用于对齐同列 label 宽度),避免在 map 中逐项重复 filter
|
|
112
|
+
const columnGroups = useMemo(() => {
|
|
113
|
+
const groups: FormItemProps[][] = Array.from(
|
|
114
|
+
{ length: totalColumnNum },
|
|
115
|
+
() => []
|
|
116
|
+
);
|
|
117
|
+
currentSearchItems.forEach((item, idx) => {
|
|
118
|
+
groups[idx % totalColumnNum].push(item);
|
|
119
|
+
});
|
|
120
|
+
return groups;
|
|
121
|
+
}, [currentSearchItems, totalColumnNum]);
|
|
122
|
+
|
|
123
|
+
if (!shouldRender) {
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return (
|
|
128
|
+
<>
|
|
129
|
+
<div
|
|
130
|
+
ref={containerRef}
|
|
131
|
+
className={classNames(styles.Search, className)}
|
|
132
|
+
style={
|
|
133
|
+
{
|
|
134
|
+
"--column-number": totalColumnNum,
|
|
135
|
+
"--column-offset-width": `${columnOffsetWidth}px`,
|
|
136
|
+
} as CSSProperties
|
|
137
|
+
}
|
|
138
|
+
>
|
|
139
|
+
<div
|
|
140
|
+
className={classNames(styles.searchOption, styles[expand.toString()])}
|
|
141
|
+
>
|
|
142
|
+
<AntdForm
|
|
143
|
+
form={form}
|
|
144
|
+
className={classNames(styles.option, cls)}
|
|
145
|
+
onValuesChange={onValuesChange}
|
|
146
|
+
>
|
|
147
|
+
{permitted &&
|
|
148
|
+
currentSearchItems.map((item, idx) => (
|
|
149
|
+
<FormItem
|
|
150
|
+
{...item}
|
|
151
|
+
key={item.name}
|
|
152
|
+
className={classNames(styles.item, item.className, {
|
|
153
|
+
[styles.unFill]: item.width !== undefined,
|
|
154
|
+
})}
|
|
155
|
+
label={advancedFilters ? undefined : item.label}
|
|
156
|
+
labelWidth={
|
|
157
|
+
advancedFilters || item.width !== undefined
|
|
158
|
+
? undefined
|
|
159
|
+
: getLabelWidth(
|
|
160
|
+
columnGroups[idx % totalColumnNum],
|
|
161
|
+
undefined,
|
|
162
|
+
minLabelWidth
|
|
163
|
+
)
|
|
164
|
+
}
|
|
165
|
+
/>
|
|
166
|
+
))}
|
|
167
|
+
{(beforeButtonGroup?.length ||
|
|
168
|
+
affterButtonGroup?.length ||
|
|
169
|
+
permitted) && (
|
|
170
|
+
<ButtonGroup
|
|
171
|
+
beforeButtonGroup={beforeButtonGroup}
|
|
172
|
+
affterButtonGroup={affterButtonGroup}
|
|
173
|
+
expandButton={
|
|
174
|
+
<ExpandButton
|
|
175
|
+
expand={expand}
|
|
176
|
+
toggleExpand={toggleExpand}
|
|
177
|
+
advancedFilters={advancedFilters}
|
|
178
|
+
showExpandButton={showExpandButton}
|
|
179
|
+
showAllSearchItems={
|
|
180
|
+
showAllSearchItems || !!moreSearchItems?.length
|
|
181
|
+
}
|
|
182
|
+
/>
|
|
183
|
+
}
|
|
184
|
+
permitted={permitted}
|
|
185
|
+
ref={buttonGroupRef}
|
|
186
|
+
>
|
|
187
|
+
{currentSearchItems.length > 0 && (
|
|
188
|
+
<SearchButtons
|
|
189
|
+
onSearch={onSearchClick}
|
|
190
|
+
onReset={onResetClick}
|
|
191
|
+
searchDisabled={searchDisabled}
|
|
192
|
+
searchText={searchText}
|
|
193
|
+
resetText={resetText}
|
|
194
|
+
/>
|
|
195
|
+
)}
|
|
196
|
+
{showFilter && (
|
|
197
|
+
<FilterDropdown
|
|
198
|
+
searchItems={processedSearchItems}
|
|
199
|
+
originalSearchItems={searchItems}
|
|
200
|
+
setSearchItems={setSearchItems}
|
|
201
|
+
onFilterChange={onFilterChange}
|
|
202
|
+
/>
|
|
203
|
+
)}
|
|
204
|
+
</ButtonGroup>
|
|
205
|
+
)}
|
|
206
|
+
</AntdForm>
|
|
207
|
+
</div>
|
|
208
|
+
{permitted && afterForm}
|
|
209
|
+
</div>
|
|
210
|
+
{permitted &&
|
|
211
|
+
renderOutside?.({
|
|
212
|
+
form,
|
|
213
|
+
expand,
|
|
214
|
+
setExpand,
|
|
215
|
+
processedSearchItems,
|
|
216
|
+
getLabelWidth,
|
|
217
|
+
onSearchClick,
|
|
218
|
+
onResetClick,
|
|
219
|
+
})}
|
|
220
|
+
</>
|
|
221
|
+
);
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
export default SearchBase;
|
|
@@ -12,6 +12,7 @@ import {
|
|
|
12
12
|
cleanSearchData,
|
|
13
13
|
calculateVisibleItems,
|
|
14
14
|
calculateButtonGroupAndColumnWidth,
|
|
15
|
+
shouldDisplayExtraItem,
|
|
15
16
|
} from "../_utils";
|
|
16
17
|
import styles from "../index.module.scss";
|
|
17
18
|
import { ChakraButtonProps } from "../../Button";
|
|
@@ -119,12 +120,10 @@ export const useSearchCommon = (
|
|
|
119
120
|
}, [moreSearchItems]);
|
|
120
121
|
|
|
121
122
|
// 计算可见的搜索项数量
|
|
122
|
-
const visibleSearchItemCount =
|
|
123
|
-
return visibleSearchItems.length;
|
|
124
|
-
}, [visibleSearchItems]);
|
|
123
|
+
const visibleSearchItemCount = visibleSearchItems.length;
|
|
125
124
|
|
|
126
125
|
// 计算总列数(包括按钮组)
|
|
127
|
-
const baseTotalColumnNum =
|
|
126
|
+
const baseTotalColumnNum = columnNum + 1;
|
|
128
127
|
|
|
129
128
|
// 根据容器宽度动态调整列数
|
|
130
129
|
const totalColumnNum = useAdaptiveColumnNum(
|
|
@@ -138,10 +137,7 @@ export const useSearchCommon = (
|
|
|
138
137
|
);
|
|
139
138
|
|
|
140
139
|
// 计算调整后的搜索项列数(不包括按钮组)
|
|
141
|
-
const adaptiveColumnNum =
|
|
142
|
-
() => Math.max(1, totalColumnNum - 1),
|
|
143
|
-
[totalColumnNum],
|
|
144
|
-
);
|
|
140
|
+
const adaptiveColumnNum = Math.max(1, totalColumnNum - 1);
|
|
145
141
|
|
|
146
142
|
// 使用基于宽度的展开/收起控制
|
|
147
143
|
const { expand, setExpand, toggleExpand, visibleItemCount } = useSearchExpand(
|
|
@@ -241,11 +237,12 @@ export const useSearchCommon = (
|
|
|
241
237
|
|
|
242
238
|
// 判断按钮组宽度是否大于列宽,如果是,则显示 实际搜索项数量+1
|
|
243
239
|
if (
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
240
|
+
shouldDisplayExtraItem(
|
|
241
|
+
buttonGroupWidth,
|
|
242
|
+
columnWidth,
|
|
243
|
+
adaptiveColumnNum,
|
|
244
|
+
visibleSearchItemCount,
|
|
245
|
+
)
|
|
249
246
|
) {
|
|
250
247
|
return adaptiveColumnNum + 1;
|
|
251
248
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { useState, useEffect, useCallback } from "react";
|
|
2
2
|
import { FormItemProps } from "../../FormItem";
|
|
3
|
+
import { measureButtonGroupWidth } from "../_utils";
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* 基于宽度自适应控制展开/收起的Hook
|
|
@@ -46,19 +47,8 @@ export function useSearchExpand(
|
|
|
46
47
|
const availableWidth = containerWidth - 30; // 15px padding on each side
|
|
47
48
|
|
|
48
49
|
// 获取按钮组的实际宽度(计算所有子元素宽度总和)
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
if (buttonChildren.length > 0) {
|
|
52
|
-
// 计算所有子元素的宽度总和
|
|
53
|
-
let totalWidth = 0;
|
|
54
|
-
buttonChildren?.forEach((child) => {
|
|
55
|
-
totalWidth += (child as HTMLElement).offsetWidth;
|
|
56
|
-
});
|
|
57
|
-
// 加上子元素之间的间距(column-gap: 10px)
|
|
58
|
-
const childGap = 5;
|
|
59
|
-
totalWidth += (buttonChildren.length - 1) * childGap;
|
|
60
|
-
buttonGroupWidth = totalWidth;
|
|
61
|
-
}
|
|
50
|
+
const buttonGroupWidth =
|
|
51
|
+
measureButtonGroupWidth(buttonGroupRef.current) ?? 0;
|
|
62
52
|
|
|
63
53
|
// 获取所有搜索项元素
|
|
64
54
|
const items = container.querySelectorAll(`${itemClassName}`);
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { RefObject } from "react";
|
|
2
|
+
import { measureButtonGroupWidth } from "./measureButtonGroupWidth";
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* 计算按钮组宽度和列宽
|
|
@@ -26,16 +27,7 @@ export function calculateButtonGroupAndColumnWidth(
|
|
|
26
27
|
const gap = 10; // 项之间的间距
|
|
27
28
|
|
|
28
29
|
// 计算按钮组宽度
|
|
29
|
-
|
|
30
|
-
if (buttonChildren.length > 0) {
|
|
31
|
-
let totalWidth = 0;
|
|
32
|
-
buttonChildren?.forEach((child) => {
|
|
33
|
-
totalWidth += (child as HTMLElement).offsetWidth;
|
|
34
|
-
});
|
|
35
|
-
const childGap = 5;
|
|
36
|
-
totalWidth += (buttonChildren.length - 1) * childGap;
|
|
37
|
-
buttonGroupWidth = totalWidth;
|
|
38
|
-
}
|
|
30
|
+
buttonGroupWidth = measureButtonGroupWidth(buttonGroupRef.current);
|
|
39
31
|
|
|
40
32
|
// 计算列宽(单个搜索项的默认宽度)
|
|
41
33
|
if (adaptiveColumnNum > 0) {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { FormItemProps } from "../../FormItem";
|
|
2
|
+
import { shouldDisplayExtraItem } from "./shouldDisplayExtraItem";
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* 计算当前应该显示的搜索项
|
|
@@ -21,7 +22,7 @@ export function calculateVisibleItems(
|
|
|
21
22
|
columnWidth?: number,
|
|
22
23
|
): FormItemProps[] {
|
|
23
24
|
const visibleItems = searchItems.filter((item) => item.visible);
|
|
24
|
-
const hasCustomWidth = searchItems.
|
|
25
|
+
const hasCustomWidth = searchItems.some((item) => item.width !== undefined);
|
|
25
26
|
|
|
26
27
|
if (!expand) {
|
|
27
28
|
// 当有自定义宽度时,使用自适应计算
|
|
@@ -32,16 +33,14 @@ export function calculateVisibleItems(
|
|
|
32
33
|
} else if (!hasCustomWidth) {
|
|
33
34
|
// 没有自定义宽度时,使用 columnNum(实际搜索项数量)
|
|
34
35
|
// 判断按钮组宽度是否大于列宽,如果是,则显示 实际搜索项数量+1
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
displayCount = columnNum + 1;
|
|
44
|
-
}
|
|
36
|
+
const displayCount = shouldDisplayExtraItem(
|
|
37
|
+
buttonGroupWidth,
|
|
38
|
+
columnWidth,
|
|
39
|
+
columnNum,
|
|
40
|
+
visibleItems.length,
|
|
41
|
+
)
|
|
42
|
+
? columnNum + 1
|
|
43
|
+
: columnNum;
|
|
45
44
|
if (visibleItems.length >= displayCount) {
|
|
46
45
|
return visibleItems.slice(0, displayCount);
|
|
47
46
|
}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
export { cleanSearchData } from "./cleanSearchData";
|
|
2
2
|
export { calculateVisibleItems } from "./calculateVisibleItems";
|
|
3
3
|
export { calculateButtonGroupAndColumnWidth } from "./calculateButtonGroupAndColumnWidth";
|
|
4
|
+
export { measureButtonGroupWidth } from "./measureButtonGroupWidth";
|
|
5
|
+
export { shouldDisplayExtraItem } from "./shouldDisplayExtraItem";
|
|
4
6
|
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 计算按钮组的实际宽度(所有子元素宽度总和 + 子元素间距)
|
|
3
|
+
* @param buttonGroupEl 按钮组元素
|
|
4
|
+
* @returns 按钮组宽度;无子元素时返回 undefined
|
|
5
|
+
*/
|
|
6
|
+
export function measureButtonGroupWidth(
|
|
7
|
+
buttonGroupEl: HTMLElement | null
|
|
8
|
+
): number | undefined {
|
|
9
|
+
if (!buttonGroupEl) return undefined;
|
|
10
|
+
|
|
11
|
+
const buttonChildren = Array.from(buttonGroupEl.children);
|
|
12
|
+
if (buttonChildren.length === 0) return undefined;
|
|
13
|
+
|
|
14
|
+
let totalWidth = 0;
|
|
15
|
+
buttonChildren.forEach((child) => {
|
|
16
|
+
totalWidth += (child as HTMLElement).offsetWidth;
|
|
17
|
+
});
|
|
18
|
+
// 加上子元素之间的间距
|
|
19
|
+
const childGap = 5;
|
|
20
|
+
totalWidth += (buttonChildren.length - 1) * childGap;
|
|
21
|
+
|
|
22
|
+
return totalWidth;
|
|
23
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 判断收起状态下是否应多显示一个搜索项
|
|
3
|
+
*
|
|
4
|
+
* 规则:按钮组宽度大于列宽(按钮组独占更宽空间,首行有富余);
|
|
5
|
+
* 或按钮组宽度小于列宽且可见项刚好比列数多 1(此时收起只会藏起 1 项,不如直接放下)。
|
|
6
|
+
* @param buttonGroupWidth 按钮组宽度
|
|
7
|
+
* @param columnWidth 列宽
|
|
8
|
+
* @param columnNum 搜索项列数(不含按钮组)
|
|
9
|
+
* @param visibleCount 可见搜索项总数
|
|
10
|
+
*/
|
|
11
|
+
export function shouldDisplayExtraItem(
|
|
12
|
+
buttonGroupWidth: number | undefined,
|
|
13
|
+
columnWidth: number | undefined,
|
|
14
|
+
columnNum: number,
|
|
15
|
+
visibleCount: number
|
|
16
|
+
): boolean {
|
|
17
|
+
return (
|
|
18
|
+
buttonGroupWidth !== undefined &&
|
|
19
|
+
columnWidth !== undefined &&
|
|
20
|
+
(buttonGroupWidth > columnWidth ||
|
|
21
|
+
(buttonGroupWidth < columnWidth && columnNum + 1 === visibleCount))
|
|
22
|
+
);
|
|
23
|
+
}
|