-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathsearchableTabsComponent.tsx
More file actions
210 lines (183 loc) · 6.75 KB
/
searchableTabsComponent.tsx
File metadata and controls
210 lines (183 loc) · 6.75 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
204
205
206
207
208
209
210
import React, { useEffect, useRef, useState, useCallback, useMemo } from 'react';
import { Tabs, Input, Empty } from 'antd';
import ParentProvider from '@/providers/parentProvider';
import ComponentsContainer from '@/components/formDesigner/containers/componentsContainer';
import { useStyles } from './style';
import { SearchOutlined } from '@ant-design/icons';
import { filterDynamicComponents } from './utils';
import { IPropertiesTabsComponentProps } from './models';
import { useFormStateOrUndefined, useFormActionsOrUndefined } from '@/providers/form';
import { useShaFormDataUpdate } from '@/providers/form/providers/shaFormProvider';
import { useFormDesignerActiveSettingsTabKey, useFormDesigner } from '@/providers/formDesigner';
interface SearchableTabsProps {
model: IPropertiesTabsComponentProps;
}
const SearchableTabs: React.FC<SearchableTabsProps> = ({ model }) => {
const { tabs } = model;
const [searchQuery, setSearchQuery] = useState('');
const searchRefs = useRef(new Map());
const { styles } = useStyles();
const formState = useFormStateOrUndefined();
const formActions = useFormActionsOrUndefined();
const formDesigner = useFormDesigner();
const persistedActiveTabKey = useFormDesignerActiveSettingsTabKey();
// Use persisted tab key if available, otherwise default to first tab
const activeTabKey = persistedActiveTabKey ?? '1';
useShaFormDataUpdate();
const handleSearchChange = (e: React.ChangeEvent<HTMLInputElement>): void => {
setSearchQuery(e.target.value);
};
const renderSearchInput = (options?: {
ref?: (el: any) => void;
className?: string;
style?: React.CSSProperties;
autoFocus?: boolean;
wrapperStyle?: React.CSSProperties;
}): React.JSX.Element => {
const input = (
<Input
type="search"
size="small"
allowClear
placeholder="Search properties"
value={searchQuery}
onChange={handleSearchChange}
suffix={<SearchOutlined style={{ color: 'rgba(0,0,0,.45)' }} />}
ref={options?.ref}
className={options?.className}
style={options?.style}
autoFocus={options?.autoFocus}
/>
);
return options?.wrapperStyle ? (
<div className={styles.searchField} style={options.wrapperStyle}>
{input}
</div>
) : input;
};
const focusActiveTabSearch = useCallback(() => {
const activeSearchInput = searchRefs.current.get(activeTabKey);
if (activeSearchInput) {
// Small delay to ensure the tab is rendered
setTimeout(() => {
activeSearchInput.focus();
}, 50);
}
}, [activeTabKey]);
const handleTabChange = (newActiveKey: string): void => {
formDesigner.setActiveSettingsTabKey(newActiveKey);
};
// Focus search input when search query changes and we have matching results
useEffect(() => {
if (searchQuery) {
focusActiveTabSearch();
}
}, [searchQuery, focusActiveTabSearch]);
// Focus search input when tab changes
useEffect(() => {
focusActiveTabSearch();
}, [activeTabKey, focusActiveTabSearch]);
const isComponentHidden = (component): boolean => {
if (formState.name === "modalSettings") {
if (component.inputs) {
const visibleInputs = component.inputs.filter((input) => {
if (!input.propertyName) return true;
return formActions.isComponentFiltered(input);
});
if (visibleInputs.length === 0) {
return false;
}
component.inputs = visibleInputs;
return visibleInputs.length > 0;
}
return formActions.isComponentFiltered(component);
} else {
return true;
}
};
const newFilteredTabs = useMemo(() => (tabs
.map((tab: any, index: number) => {
const filteredComponents = tab.children ?? filterDynamicComponents(tab.components, searchQuery);
const visibleComponents = Array.isArray(filteredComponents)
? filteredComponents.filter((comp) => isComponentHidden(comp))
: filteredComponents;
const hasVisibleComponents = Array.isArray(visibleComponents)
? visibleComponents.length > 0
: !!visibleComponents;
const tabKey = tab.key || (index + 1).toString();
return {
...tab,
key: tabKey,
label: tab.label ?? tab.title,
components: visibleComponents,
children: visibleComponents.length === 0
? <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} description="Properties not found" />
: (
<ParentProvider
name={`SearchableTab-${tabKey}`}
model={model}
>
{renderSearchInput({
ref: (el) => {
if (el) {
searchRefs.current.set(tabKey, el);
} else {
searchRefs.current.delete(tabKey);
}
},
className: styles.searchField,
})}
<ComponentsContainer
containerId={tab.id + tab.key}
dynamicComponents={visibleComponents}
/>
</ParentProvider>
),
forceRender: true,
hidden: tab.hidden || !hasVisibleComponents,
};
})
.filter((tab) => !tab.hidden)
), [model, searchQuery, tabs]);
// Auto-switch to the first tab that has visible components when searching
useEffect(() => {
if (searchQuery && newFilteredTabs.length > 0) {
const firstVisibleTab = newFilteredTabs.find((tab) =>
Array.isArray(tab.components) ? tab.components.length > 0 : !!tab.components,
);
if (firstVisibleTab && firstVisibleTab.key !== activeTabKey) {
formDesigner.setActiveSettingsTabKey(firstVisibleTab.key);
}
}
}, [searchQuery, newFilteredTabs, activeTabKey, formDesigner]);
// Ensure we have a valid active tab key
useEffect(() => {
if (newFilteredTabs.length > 0 && !newFilteredTabs.find((tab) => tab.key === activeTabKey)) {
formDesigner.setActiveSettingsTabKey(newFilteredTabs[0].key);
}
}, [newFilteredTabs, activeTabKey, formDesigner]);
const localTabs = useMemo(() => (
<Tabs
key="searchable-tabs"
activeKey={activeTabKey}
onChange={handleTabChange}
size={model.size}
type={model.tabType || 'card'}
tabPosition={model.position || 'top'}
items={newFilteredTabs}
className={styles.content}
/>
), [activeTabKey, handleTabChange, model.size, model.tabType, newFilteredTabs, styles.content, model.position]);
return (
<>
{newFilteredTabs.length === 0 &&
renderSearchInput({
autoFocus: true,
})}
{newFilteredTabs.length === 0 && searchQuery
? <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} description="Property Not Found" />
: localTabs}
</>
);
};
export default SearchableTabs;