Skip to content

Commit

Permalink
[Autocomplete] Correct example of virtualization (#20496)
Browse files Browse the repository at this point in the history
  • Loading branch information
galkadaw authored Apr 11, 2020
1 parent f270743 commit 54ace65
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
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

0 comments on commit 54ace65

Please sign in to comment.