Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: renderValue was missing a useCallback wrapper #1800

Merged
merged 1 commit into from
May 22, 2023
Merged
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
53 changes: 28 additions & 25 deletions packages/odyssey-react-mui/src/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,36 +125,39 @@ const Select = forwardRef<HTMLSelectElement, SelectProps>(
return { text: option, value: option, type: "option" };
});

const renderValue = (selected: string | string[]) => {
// If the selected value isn't an array, then we don't need to display
// chips and should fall back to the default render behavior
if (typeof selected === "string") {
return undefined;
}
const renderValue = useCallback(
(selected: string | string[]) => {
// If the selected value isn't an array, then we don't need to display
// chips and should fall back to the default render behavior
if (typeof selected === "string") {
return undefined;
}

// Convert the selected options array into <Chip>s
const renderedChips = selected
.map((item: string) => {
const selectedOption = normalizedOptions.find(
(option) => option.value === item
);
// Convert the selected options array into <Chip>s
const renderedChips = selected
.map((item: string) => {
const selectedOption = normalizedOptions.find(
(option) => option.value === item
);

if (!selectedOption) {
return null;
}
if (!selectedOption) {
return null;
}

return <Chip key={item} label={selectedOption.text} />;
})
.filter(Boolean);
return <Chip key={item} label={selectedOption.text} />;
})
.filter(Boolean);

if (renderedChips.length === 0) {
return null;
}
if (renderedChips.length === 0) {
return null;
}

// We need the <Box> to surround the <Chip>s for
// proper styling
return <Box>{renderedChips}</Box>;
};
// We need the <Box> to surround the <Chip>s for
// proper styling
return <Box>{renderedChips}</Box>;
},
[normalizedOptions]
);

// Convert the options into the ReactNode children
// that will populate the <Select>
Expand Down