@nuskin/foundation-ui-components 1.32.0 → 1.32.1-td-849.1
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/package.json
CHANGED
|
@@ -3,24 +3,28 @@ import React, { PropsWithChildren, ReactElement, useEffect, useRef, useState } f
|
|
|
3
3
|
import {
|
|
4
4
|
Autocomplete,
|
|
5
5
|
AutocompleteRenderInputParams,
|
|
6
|
-
ListSubheader,
|
|
7
6
|
Popper,
|
|
8
|
-
useMediaQuery,
|
|
9
|
-
useTheme,
|
|
10
7
|
} from '@mui/material';
|
|
11
8
|
|
|
12
9
|
import { LineLoader } from './helper/LineLoader';
|
|
13
10
|
import { RecentClearAll } from './helper/RecentClearAll';
|
|
14
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
renderGroupBy,
|
|
13
|
+
renderGroupName,
|
|
14
|
+
renderOptionLabel,
|
|
15
|
+
} from './helper/renderGroupName';
|
|
15
16
|
import { SeeMoreButton } from './helper/SeeMoreButton';
|
|
16
|
-
import {
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
import {
|
|
18
|
+
StyledPopper,
|
|
19
|
+
StyledTextField,
|
|
20
|
+
} from './NsAutocomplete.styled';
|
|
21
|
+
import {
|
|
22
|
+
ComponentBoxProps,
|
|
23
|
+
NsAutocompleteProps,
|
|
24
|
+
OptionsProps,
|
|
25
|
+
} from './types.d';
|
|
19
26
|
|
|
20
|
-
const LISTBOX_PADDING = 8;
|
|
21
27
|
const noop = () => {};
|
|
22
|
-
const OuterElementContext = React.createContext({});
|
|
23
|
-
|
|
24
28
|
const NsAutocomplete = ({
|
|
25
29
|
id = 'custom_autocomplete_dropdown',
|
|
26
30
|
options,
|
|
@@ -41,9 +45,9 @@ const NsAutocomplete = ({
|
|
|
41
45
|
isFilterEnabled = true,
|
|
42
46
|
fullWidth,
|
|
43
47
|
onKeypress,
|
|
44
|
-
}: PropsWithChildren<NsAutocompleteProps>):
|
|
45
|
-
const listboxRef = useRef<any | null>(null);
|
|
48
|
+
}: PropsWithChildren<NsAutocompleteProps>):ReactElement => {
|
|
46
49
|
const [scrollTopPosition, setScrollTopPosition] = useState(0);
|
|
50
|
+
const listboxRef = useRef<HTMLUListElement | null>(null);
|
|
47
51
|
|
|
48
52
|
useEffect(() => {
|
|
49
53
|
if (listboxRef.current && options.length > 0) {
|
|
@@ -51,13 +55,6 @@ const NsAutocomplete = ({
|
|
|
51
55
|
}
|
|
52
56
|
}, [options, scrollTopPosition]);
|
|
53
57
|
|
|
54
|
-
const OuterElementType = React.forwardRef((props: any, ref) => {
|
|
55
|
-
const outerProps = React.useContext(OuterElementContext);
|
|
56
|
-
return <ul ref={ref} {...props} {...outerProps} />;
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
OuterElementType.displayName = 'OuterElementType';
|
|
60
|
-
|
|
61
58
|
const renderInput = (params: AutocompleteRenderInputParams) => (
|
|
62
59
|
<StyledTextField
|
|
63
60
|
{...(params as any)}
|
|
@@ -80,132 +77,54 @@ const NsAutocomplete = ({
|
|
|
80
77
|
</li>
|
|
81
78
|
);
|
|
82
79
|
|
|
83
|
-
const
|
|
84
|
-
const { data, index, style } = props;
|
|
85
|
-
const dataSet = data[index];
|
|
86
|
-
const option = dataSet?.[1];
|
|
87
|
-
const inlineStyle = {
|
|
88
|
-
top: (style.top as number) + LISTBOX_PADDING,
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
if (Object.hasOwn(dataSet, 'group')) {
|
|
92
|
-
return (
|
|
93
|
-
<ListSubheader key={dataSet.key} component="div" style={inlineStyle}>
|
|
94
|
-
{dataSet.group}
|
|
95
|
-
</ListSubheader>
|
|
96
|
-
);
|
|
97
|
-
}
|
|
98
|
-
|
|
80
|
+
const renderListBoxComponent = ({ children, ...rest }: ComponentBoxProps): React.JSX.Element => {
|
|
99
81
|
return (
|
|
100
|
-
<
|
|
101
|
-
{
|
|
102
|
-
|
|
82
|
+
<ul
|
|
83
|
+
aria-labelledby={rest['aria-labelledby']}
|
|
84
|
+
onMouseDown={rest.onMouseDown}
|
|
85
|
+
role={rest.role}
|
|
86
|
+
id={rest.id}
|
|
87
|
+
className={rest.className}
|
|
88
|
+
ref={listboxRef}
|
|
89
|
+
>
|
|
90
|
+
<RecentClearAll
|
|
91
|
+
show={clear?.show ?? false}
|
|
92
|
+
text={clear?.text ?? 'Clear All'}
|
|
93
|
+
onClick={clear?.onClear ?? noop}
|
|
94
|
+
testId="autocomplete-clear-id"
|
|
95
|
+
recentSearchText={clear?.recentSearchText ?? 'Your Recent Searches'}
|
|
96
|
+
/>
|
|
97
|
+
{children}
|
|
98
|
+
<SeeMoreButton
|
|
99
|
+
onSeeMore={seeMore?.onSeeMore}
|
|
100
|
+
show={seeMore?.show}
|
|
101
|
+
text={seeMore?.text}
|
|
102
|
+
options={options}
|
|
103
|
+
setScrollTopPosition={setScrollTopPosition}
|
|
104
|
+
listboxRef={listboxRef}
|
|
105
|
+
/>
|
|
106
|
+
</ul>
|
|
103
107
|
);
|
|
104
108
|
};
|
|
105
|
-
|
|
106
|
-
const useResetCache = (data: any) => {
|
|
107
|
-
const ref = React.useRef<VariableSizeList>(null);
|
|
108
|
-
React.useEffect(() => {
|
|
109
|
-
if (ref.current != null) {
|
|
110
|
-
ref.current.resetAfterIndex(0, true);
|
|
111
|
-
}
|
|
112
|
-
}, [data]);
|
|
113
|
-
return ref;
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
const ListboxComponent = React.forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLElement>>(
|
|
117
|
-
function ListboxComponent(props, ref) {
|
|
118
|
-
const { children, ...other } = props;
|
|
119
|
-
const itemData: React.ReactElement[] = [];
|
|
120
|
-
(children as React.ReactElement[]).forEach(
|
|
121
|
-
(item: React.ReactElement & { children?: React.ReactElement[] }) => {
|
|
122
|
-
itemData.push(item);
|
|
123
|
-
itemData.push(...(item.children || []));
|
|
124
|
-
}
|
|
125
|
-
);
|
|
126
|
-
|
|
127
|
-
const theme = useTheme();
|
|
128
|
-
const smUp = useMediaQuery(theme.breakpoints.up('sm'), {
|
|
129
|
-
noSsr: true,
|
|
130
|
-
});
|
|
131
|
-
const itemCount = itemData.length;
|
|
132
|
-
const itemSize = smUp ? 36 : 48;
|
|
133
|
-
|
|
134
|
-
const getChildSize = (child: React.ReactElement) => {
|
|
135
|
-
if (Object.hasOwn(child, 'group')) {
|
|
136
|
-
return 48;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
return itemSize;
|
|
140
|
-
};
|
|
141
|
-
|
|
142
|
-
const getHeight = () => {
|
|
143
|
-
if (itemCount > 8) {
|
|
144
|
-
return 8 * itemSize;
|
|
145
|
-
}
|
|
146
|
-
return itemData.map(getChildSize).reduce((a, b) => a + b, 0);
|
|
147
|
-
};
|
|
148
|
-
|
|
149
|
-
const gridRef = useResetCache(itemCount);
|
|
150
|
-
|
|
151
|
-
return (
|
|
152
|
-
<>
|
|
153
|
-
<RecentClearAll
|
|
154
|
-
show={clear?.show ?? false}
|
|
155
|
-
text={clear?.text ?? 'Clear All'}
|
|
156
|
-
onClick={clear?.onClear ?? noop}
|
|
157
|
-
testId="autocomplete-clear-id"
|
|
158
|
-
recentSearchText={clear?.recentSearchText ?? 'Your Recent Searches'}
|
|
159
|
-
/>
|
|
160
|
-
<div ref={ref}>
|
|
161
|
-
<OuterElementContext.Provider value={other}>
|
|
162
|
-
<VariableSizeList
|
|
163
|
-
itemData={itemData}
|
|
164
|
-
height={getHeight() + 2 * LISTBOX_PADDING}
|
|
165
|
-
width="100%"
|
|
166
|
-
ref={gridRef}
|
|
167
|
-
outerElementType={OuterElementType}
|
|
168
|
-
innerElementType="ul"
|
|
169
|
-
itemSize={(index) => getChildSize(itemData[index])}
|
|
170
|
-
overscanCount={5}
|
|
171
|
-
itemCount={itemCount}
|
|
172
|
-
>
|
|
173
|
-
{renderRow}
|
|
174
|
-
</VariableSizeList>
|
|
175
|
-
</OuterElementContext.Provider>
|
|
176
|
-
</div>
|
|
177
|
-
<SeeMoreButton
|
|
178
|
-
onSeeMore={seeMore?.onSeeMore}
|
|
179
|
-
show={seeMore?.show}
|
|
180
|
-
text={seeMore?.text}
|
|
181
|
-
options={options}
|
|
182
|
-
setScrollTopPosition={setScrollTopPosition}
|
|
183
|
-
listboxRef={listboxRef}
|
|
184
|
-
/>
|
|
185
|
-
</>
|
|
186
|
-
);
|
|
187
|
-
}
|
|
188
|
-
);
|
|
189
|
-
|
|
190
109
|
return (
|
|
191
110
|
<>
|
|
192
111
|
<LineLoader show={loading ?? false} />
|
|
193
112
|
<Autocomplete
|
|
194
|
-
|
|
113
|
+
openOnFocus
|
|
195
114
|
id={id}
|
|
196
|
-
|
|
115
|
+
disablePortal
|
|
197
116
|
className={className}
|
|
117
|
+
options={options}
|
|
198
118
|
loading={loading}
|
|
199
119
|
noOptionsText={noOptionsText}
|
|
200
|
-
PopperComponent={fullWidth ? StyledPopper : Popper}
|
|
201
|
-
ListboxComponent={ListboxComponent}
|
|
202
|
-
options={options}
|
|
203
120
|
groupBy={renderGroupBy}
|
|
204
121
|
getOptionLabel={renderOptionLabel}
|
|
205
|
-
renderOption={
|
|
206
|
-
renderGroup={(params) => params as any}
|
|
207
|
-
onChange={onChange}
|
|
122
|
+
renderOption={renderOption as any}
|
|
208
123
|
renderInput={renderInput}
|
|
124
|
+
PopperComponent={fullWidth ? StyledPopper : Popper} // TODO:https://code.tls.nuskin.io/ns-am/content-foundation/nuskin-app/-/jobs/1355817
|
|
125
|
+
ListboxComponent={renderListBoxComponent}
|
|
126
|
+
renderGroup={renderGroupName}
|
|
127
|
+
onChange={onChange}
|
|
209
128
|
onInputChange={onInputChange}
|
|
210
129
|
value={value}
|
|
211
130
|
onFocus={onFocus ?? noop}
|