Skip to content

Commit

Permalink
[Autocomplete] use useMemo to render grouped options
Browse files Browse the repository at this point in the history
  • Loading branch information
sujinleeme committed Oct 29, 2020
1 parent 44c4606 commit 81aaa6f
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions packages/material-ui/src/Autocomplete/Autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,24 @@ const Autocomplete = React.forwardRef(function Autocomplete(props, ref) {
);
};

const renderGroupedOptions = React.useMemo(
() => (groupedOptions) => {
return groupedOptions.map((option, index) => {
if (groupBy) {
return renderGroup({
key: option.key,
group: option.group,
children: option.options.map((option2, index2) =>
renderListOption(option2, option.index + index2),
),
});
}
return renderListOption(option, index);
});
},
[groupedOptions],
);

const hasClearIcon = !disableClearable && !disabled && dirty;
const hasPopupIcon = (!freeSolo || forcePopupIcon === true) && forcePopupIcon !== false;

Expand Down Expand Up @@ -496,18 +514,7 @@ const Autocomplete = React.forwardRef(function Autocomplete(props, ref) {
{...getListboxProps()}
{...ListboxProps}
>
{groupedOptions.map((option, index) => {
if (groupBy) {
return renderGroup({
key: option.key,
group: option.group,
children: option.options.map((option2, index2) =>
renderListOption(option2, option.index + index2),
),
});
}
return renderListOption(option, index);
})}
{renderGroupedOptions(groupedOptions)}
</ListboxComponent>
) : null}
</PaperComponent>
Expand Down

0 comments on commit 81aaa6f

Please sign in to comment.