forked from mui/material-ui
/
Virtualize.js
135 lines (119 loc) · 3.54 KB
/
Virtualize.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import * as React from 'react';
import PropTypes from 'prop-types';
import { FixedSizeList } from 'react-window';
import PopperUnstyled from '@mui/base/PopperUnstyled';
import Autocomplete from '@mui/joy/Autocomplete';
import AutocompleteListbox from '@mui/joy/AutocompleteListbox';
import AutocompleteOption from '@mui/joy/AutocompleteOption';
import FormControl from '@mui/joy/FormControl';
import FormLabel from '@mui/joy/FormLabel';
import ListSubheader from '@mui/joy/ListSubheader';
const LISTBOX_PADDING = 6; // px
function renderRow(props) {
const { data, index, style } = props;
const dataSet = data[index];
const inlineStyle = {
...style,
top: style.top + LISTBOX_PADDING,
};
if (dataSet.hasOwnProperty('group')) {
return (
<ListSubheader key={dataSet.key} component="li" style={inlineStyle}>
{dataSet.group}
</ListSubheader>
);
}
return (
<AutocompleteOption {...dataSet[0]} style={inlineStyle}>
{dataSet[1]}
</AutocompleteOption>
);
}
const OuterElementContext = React.createContext({});
const OuterElementType = React.forwardRef((props, ref) => {
const outerProps = React.useContext(OuterElementContext);
return (
<AutocompleteListbox
{...props}
{...outerProps}
component="div"
ref={ref}
sx={{
'& ul': {
padding: 0,
margin: 0,
flexShrink: 0,
},
}}
/>
);
});
// Adapter for react-window
const ListboxComponent = React.forwardRef(function ListboxComponent(props, ref) {
const { children, anchorEl, open, modifiers, ...other } = props;
const itemData = [];
children[0].forEach((item) => {
if (item) {
itemData.push(item);
itemData.push(...(item.children || []));
}
});
const itemCount = itemData.length;
const itemSize = 40;
return (
<PopperUnstyled ref={ref} anchorEl={anchorEl} open={open} modifiers={modifiers}>
<OuterElementContext.Provider value={other}>
<FixedSizeList
itemData={itemData}
height={itemSize * 8}
width="100%"
outerElementType={OuterElementType}
innerElementType="ul"
itemSize={itemSize}
overscanCount={5}
itemCount={itemCount}
>
{renderRow}
</FixedSizeList>
</OuterElementContext.Provider>
</PopperUnstyled>
);
});
ListboxComponent.propTypes = {
anchorEl: PropTypes.any.isRequired,
children: PropTypes.node,
modifiers: PropTypes.array.isRequired,
open: PropTypes.bool.isRequired,
};
function random(length) {
const characters =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let result = '';
for (let i = 0; i < length; i += 1) {
result += characters.charAt(Math.floor(Math.random() * characters.length));
}
return result;
}
const OPTIONS = Array.from(new Array(10000))
.map(() => random(10 + Math.ceil(Math.random() * 20)))
.sort((a, b) => a.toUpperCase().localeCompare(b.toUpperCase()));
export default function Virtualize() {
return (
<FormControl id="virtualize-demo">
<FormLabel>10,000 options</FormLabel>
<Autocomplete
sx={{ width: 300 }}
disableListWrap
placeholder="Type to search"
slots={{
listbox: ListboxComponent,
}}
options={OPTIONS}
groupBy={(option) => option[0].toUpperCase()}
renderOption={(props, option) => [props, option]}
// TODO: Post React 18 update - validate this conversion, look like a hidden bug
renderGroup={(params) => params}
/>
</FormControl>
);
}