Skip to content

Commit 2ae0680

Browse files
authored
fix(combobox): prevent optionTagProps state bloat (#1584)
1 parent 323b441 commit 2ae0680

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

packages/dropdowns.next/.size-snapshot.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
{
22
"index.cjs.js": {
3-
"bundled": 55497,
4-
"minified": 40415,
5-
"gzipped": 9223
3+
"bundled": 55888,
4+
"minified": 40554,
5+
"gzipped": 9277
66
},
77
"index.esm.js": {
8-
"bundled": 50791,
9-
"minified": 35953,
10-
"gzipped": 8646,
8+
"bundled": 51176,
9+
"minified": 36086,
10+
"gzipped": 8693,
1111
"treeshaked": {
1212
"rollup": {
13-
"code": 28261,
13+
"code": 28382,
1414
"import_statements": 1064
1515
},
1616
"webpack": {
17-
"code": 31151
17+
"code": 31286
1818
}
1919
}
2020
}

packages/dropdowns.next/src/elements/combobox/Combobox.tsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,12 @@ export const Combobox = forwardRef<HTMLDivElement, IComboboxProps>(
8989
const tagProps: Record<string, IOptionProps['tagProps']> = {};
9090
const retVal = toOptions(children, tagProps);
9191

92-
setOptionTagProps(value => ({ ...value, ...tagProps }));
92+
if (isMultiselectable) {
93+
setOptionTagProps(value => ({ ...value, ...tagProps }));
94+
}
9395

9496
return retVal;
95-
}, [children]);
97+
}, [children, isMultiselectable]);
9698
const hasFocus = useRef(false);
9799
const triggerRef = useRef<HTMLDivElement>(null);
98100
const inputRef = useRef<HTMLInputElement>(null);
@@ -213,6 +215,21 @@ export const Combobox = forwardRef<HTMLDivElement, IComboboxProps>(
213215
return () => labelProps && setLabelProps(undefined);
214216
}, [getLabelProps, labelProps, setLabelProps]);
215217

218+
useEffect(() => {
219+
// prevent `optionTagProps` state bloat
220+
if (Array.isArray(selection)) {
221+
setOptionTagProps(values =>
222+
selection.reduce((value, option) => {
223+
const key = toString(option);
224+
225+
return { ...value, [key]: values[key] };
226+
}, {})
227+
);
228+
}
229+
230+
return () => setOptionTagProps({});
231+
}, [selection]);
232+
216233
const Tags = ({ selectedOptions }: { selectedOptions: IOption[] }) => {
217234
const [isFocused, setIsFocused] = useState(hasFocus.current);
218235
const value = selectedOptions.length - maxTags;

0 commit comments

Comments
 (0)