Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion docs/src/pages/components/autocomplete/Virtualize.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ const OuterElementType = React.forwardRef((props, ref) => {
return <div ref={ref} {...props} {...outerProps} />;
});

function useResetCache(data) {
const ref = React.useRef(null);
React.useEffect(() => {
if (ref.current != null) {
ref.current.resetAfterIndex(0, true);
}
}, [data]);
return ref;
}

// Adapter for react-window
const ListboxComponent = React.forwardRef(function ListboxComponent(props, ref) {
const { children, ...other } = props;
Expand All @@ -51,14 +61,16 @@ const ListboxComponent = React.forwardRef(function ListboxComponent(props, ref)
return itemData.map(getChildSize).reduce((a, b) => a + b, 0);
};

const gridRef = useResetCache(itemCount);

return (
<div ref={ref}>
<OuterElementContext.Provider value={other}>
<VariableSizeList
itemData={itemData}
height={getHeight() + 2 * LISTBOX_PADDING}
width="100%"
key={itemCount}
ref={gridRef}
outerElementType={OuterElementType}
innerElementType="ul"
itemSize={(index) => getChildSize(itemData[index])}
Expand Down Expand Up @@ -89,6 +101,7 @@ function random(length) {

const useStyles = makeStyles({
listbox: {
boxSizing: 'border-box',
'& ul': {
padding: 0,
margin: 0,
Expand Down
15 changes: 14 additions & 1 deletion docs/src/pages/components/autocomplete/Virtualize.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ const OuterElementType = React.forwardRef<HTMLDivElement>((props, ref) => {
return <div ref={ref} {...props} {...outerProps} />;
});

function useResetCache(data: any) {
const ref = React.useRef<VariableSizeList>(null);
React.useEffect(() => {
if (ref.current != null) {
ref.current.resetAfterIndex(0, true);
}
}, [data]);
return ref;
}

// Adapter for react-window
const ListboxComponent = React.forwardRef<HTMLDivElement>(function ListboxComponent(props, ref) {
const { children, ...other } = props;
Expand All @@ -50,14 +60,16 @@ const ListboxComponent = React.forwardRef<HTMLDivElement>(function ListboxCompon
return itemData.map(getChildSize).reduce((a, b) => a + b, 0);
};

const gridRef = useResetCache(itemCount);

return (
<div ref={ref}>
<OuterElementContext.Provider value={other}>
<VariableSizeList
itemData={itemData}
height={getHeight() + 2 * LISTBOX_PADDING}
width="100%"
key={itemCount}
ref={gridRef}
outerElementType={OuterElementType}
innerElementType="ul"
itemSize={(index) => getChildSize(itemData[index])}
Expand All @@ -84,6 +96,7 @@ function random(length: number) {

const useStyles = makeStyles({
listbox: {
boxSizing: 'border-box',
'& ul': {
padding: 0,
margin: 0,
Expand Down