Skip to content

Commit c9dca03

Browse files
fix height out-of-sync
1 parent ac72caf commit c9dca03

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

docs/src/pages/components/autocomplete/Virtualize.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ const OuterElementType = React.forwardRef((props, ref) => {
2727
return <div ref={ref} {...props} {...outerProps} />;
2828
});
2929

30+
function useResetCache(data) {
31+
const ref = React.useRef(null);
32+
React.useEffect(() => {
33+
if (ref.current != null) {
34+
ref.current.resetAfterIndex(0, true);
35+
}
36+
}, [data]);
37+
return ref;
38+
}
39+
3040
// Adapter for react-window
3141
const ListboxComponent = React.forwardRef(function ListboxComponent(props, ref) {
3242
const { children, ...other } = props;
@@ -51,13 +61,16 @@ const ListboxComponent = React.forwardRef(function ListboxComponent(props, ref)
5161
return itemData.map(getChildSize).reduce((a, b) => a + b, 0);
5262
};
5363

64+
const gridRef = useResetCache(itemCount);
65+
5466
return (
5567
<div ref={ref}>
5668
<OuterElementContext.Provider value={other}>
5769
<VariableSizeList
5870
itemData={itemData}
5971
height={getHeight() + 2 * LISTBOX_PADDING}
6072
width="100%"
73+
ref={gridRef}
6174
outerElementType={OuterElementType}
6275
innerElementType="ul"
6376
itemSize={(index) => getChildSize(itemData[index])}
@@ -88,6 +101,7 @@ function random(length) {
88101

89102
const useStyles = makeStyles({
90103
listbox: {
104+
boxSizing: 'border-box',
91105
'& ul': {
92106
padding: 0,
93107
margin: 0,

docs/src/pages/components/autocomplete/Virtualize.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ const OuterElementType = React.forwardRef<HTMLDivElement>((props, ref) => {
2626
return <div ref={ref} {...props} {...outerProps} />;
2727
});
2828

29+
function useResetCache(data: any) {
30+
const ref = React.useRef<VariableSizeList>(null);
31+
React.useEffect(() => {
32+
if (ref.current != null) {
33+
ref.current.resetAfterIndex(0, true);
34+
}
35+
}, [data]);
36+
return ref;
37+
}
38+
2939
// Adapter for react-window
3040
const ListboxComponent = React.forwardRef<HTMLDivElement>(function ListboxComponent(props, ref) {
3141
const { children, ...other } = props;
@@ -50,13 +60,16 @@ const ListboxComponent = React.forwardRef<HTMLDivElement>(function ListboxCompon
5060
return itemData.map(getChildSize).reduce((a, b) => a + b, 0);
5161
};
5262

63+
const gridRef = useResetCache(itemCount);
64+
5365
return (
5466
<div ref={ref}>
5567
<OuterElementContext.Provider value={other}>
5668
<VariableSizeList
5769
itemData={itemData}
5870
height={getHeight() + 2 * LISTBOX_PADDING}
5971
width="100%"
72+
ref={gridRef}
6073
outerElementType={OuterElementType}
6174
innerElementType="ul"
6275
itemSize={(index) => getChildSize(itemData[index])}
@@ -83,6 +96,7 @@ function random(length: number) {
8396

8497
const useStyles = makeStyles({
8598
listbox: {
99+
boxSizing: 'border-box',
86100
'& ul': {
87101
padding: 0,
88102
margin: 0,

0 commit comments

Comments
 (0)