Skip to content

Commit

Permalink
refactor(dropdownmenu): refactor DropdownMenu to use SDS Autocomplete (
Browse files Browse the repository at this point in the history
…#605)

* refactor(dropdownmenu): refactor DropdownMenu to use SDS Autocomplete

* fix(dropdownmenu): fix imports

* fix(dropdown): fix dropdown useEffect dependencies

* fix(autocomplete): fix paper and popper component to stop scroll jump

* docs(autocomplete): adding comments for context
  • Loading branch information
masoudmanson authored Sep 21, 2023
1 parent 87e1803 commit e9d4df0
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 396 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ exports[`<Autocomplete /> Default story renders snapshot 1`] = `
style="margin: 16px 0px 0px 24px; width: 300px;"
>
<div
class="MuiAutocomplete-root MuiAutocomplete-hasPopupIcon css-41tour-MuiAutocomplete-root"
class="MuiAutocomplete-root MuiAutocomplete-hasPopupIcon css-t46og2-MuiAutocomplete-root"
label="Search by label"
>
<div
Expand Down
11 changes: 8 additions & 3 deletions packages/components/src/core/Autocomplete/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,6 @@ export default {
multiple: {
control: { type: "boolean" },
},
search: {
control: { type: "boolean" },
},
},
component: Autocomplete,
// (masoudmanson) For the purpose of storybook, the button is removed
Expand Down Expand Up @@ -182,6 +179,11 @@ export const Default = {
multiple: true,
search: true,
},
parameters: {
controls: {
exclude: ["search"],
},
},
};

// Test
Expand Down Expand Up @@ -231,6 +233,9 @@ export const Test = {
search: true,
},
parameters: {
controls: {
exclude: ["search"],
},
snapshot: {
skip: true,
},
Expand Down
26 changes: 25 additions & 1 deletion packages/components/src/core/Autocomplete/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import {
AutocompleteRenderOptionState,
InputAdornment,
AutocompleteProps as MuiAutocompleteProps,
Popper,
PopperProps,
} from "@mui/material";
import React, { ReactNode, SyntheticEvent, useState } from "react";
import React, { ReactNode, SyntheticEvent, useCallback, useState } from "react";
import { noop } from "src/common/utils";
import ButtonIcon from "../ButtonIcon";
import { IconProps } from "../Icon";
Expand Down Expand Up @@ -106,6 +108,27 @@ const Autocomplete = <

const [inputValue, setInputValue] = useState("");

/**
* (masoudmanson): Using a custom Popper or Paper with the Autocomplete
* without a useCalback results in scroll jumps while selecting an option!
*/
const defaultPopperComponent = useCallback((popperProps: PopperProps) => {
return (
<Popper
modifiers={[
{
enabled: true,
name: "offset",
options: {
offset: [0, 8],
},
},
]}
{...popperProps}
/>
);
}, []);

return (
<StyledAutocomplete
clearOnBlur={false}
Expand All @@ -116,6 +139,7 @@ const Autocomplete = <
loadingText={loadingText}
noOptionsText={noOptionsText}
PaperComponent={PaperComponent}
PopperComponent={defaultPopperComponent}
renderOption={renderOption}
getOptionLabel={getOptionLabel}
isOptionEqualToValue={isOptionEqualToValue}
Expand Down
98 changes: 46 additions & 52 deletions packages/components/src/core/Autocomplete/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {

export interface StyleProps extends CommonThemeProps {
count?: number;
hasSections?: boolean;
icon?: ReactElement;
search?: boolean;
}
Expand All @@ -25,7 +24,6 @@ const doNotForwardProps = [
"keepSearchOnSelect",
"search",
"InputBaseProps",
"hasSections",
];

export const StyledAutocomplete = styled(Autocomplete, {
Expand All @@ -38,7 +36,7 @@ export const StyledAutocomplete = styled(Autocomplete, {
}
${(props: StyleProps) => {
const { search, hasSections } = props;
const { search } = props;
const spacings = getSpaces(props);
const colors = getColors(props);
const borders = getBorders(props);
Expand All @@ -55,60 +53,56 @@ export const StyledAutocomplete = styled(Autocomplete, {
}
& + .MuiAutocomplete-popper > .MuiAutocomplete-paper {
${
search || hasSections
? `padding-left: ${spacings?.s}px !important;`
: ""
}
.MuiAutocomplete-listbox {
max-height: 40vh;
padding-top: 0;
padding-bottom: 0;
padding-right: ${spacings?.s}px;
.MuiAutocomplete-option {
min-height: unset;
}
.MuiAutocomplete-option.Mui-focused {
background-color: ${colors?.gray[100]};
}
.MuiAutocomplete-option[aria-selected="true"] {
background-color: white;
${search ? `padding-left: ${spacings?.s}px !important;` : ""}
.MuiAutocomplete-listbox {
max-height: 40vh;
padding-top: 0;
padding-bottom: 0;
padding-right: ${spacings?.s}px;
.MuiAutocomplete-option {
min-height: unset;
}
.MuiAutocomplete-option.Mui-focused {
background-color: ${colors?.gray[100]};
}
.MuiAutocomplete-option[aria-selected="true"] {
background-color: white;
}
.MuiAutocomplete-option[aria-disabled="true"] {
opacity: 1;
}
.MuiAutocomplete-option[aria-selected="true"].Mui-focused {
background-color: ${colors?.gray[100]};
}
& > li:last-child .MuiAutocomplete-groupUl {
border-bottom: none;
margin-bottom: 0;
}
}
.MuiAutocomplete-option[aria-disabled="true"] {
opacity: 1;
.MuiAutocomplete-groupLabel {
top: 0;
color: ${colors?.gray[500]};
padding: ${spacings?.xxs}px 0 ${spacings?.xxs}px 0;
}
.MuiAutocomplete-option[aria-selected="true"].Mui-focused {
background-color: ${colors?.gray[100]};
}
& > li:last-child .MuiAutocomplete-groupUl {
border-bottom: none;
margin-bottom: 0;
}
}
.MuiAutocomplete-groupLabel {
top: 0;
color: ${colors?.gray[500]};
padding: ${spacings?.xxs}px 0 ${spacings?.xxs}px 0;
}
.MuiAutocomplete-groupUl {
margin-bottom: ${spacings?.m}px;
position: relative;
padding: 0 0 ${spacings?.xs}px 0 0;
border-bottom: ${borders?.gray[200]};
& li:last-of-type {
.MuiAutocomplete-groupUl {
margin-bottom: ${spacings?.m}px;
position: relative;
margin-bottom: ${spacings?.xxs}px;
padding: 0 0 ${spacings?.xs}px 0 0;
border-bottom: ${borders?.gray[200]};
& li:last-of-type {
position: relative;
margin-bottom: ${spacings?.xxs}px;
}
}
}
`;
Expand Down
4 changes: 2 additions & 2 deletions packages/components/src/core/Dropdown/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ const Dropdown = <Multiple extends boolean | undefined = false>({

useEffect(() => {
onChange(value);
}, [value]);
}, [onChange, value]);

useEffect(() => {
if (isControlled) {
setValue(propValue);
}
}, [propValue]);
}, [isControlled, propValue]);

const [open, setOpen] = useState(false);

Expand Down
3 changes: 0 additions & 3 deletions packages/components/src/core/DropdownMenu/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,6 @@ const LivePreviewDemo = (): JSX.Element => {
}}
search={false}
multiple={false}
hasSections={false}
value={value1}
onClickAway={handleClickAway1}
/>
Expand Down Expand Up @@ -369,7 +368,6 @@ const LivePreviewDemo = (): JSX.Element => {
open={!!open4}
search={false}
multiple
hasSections
groupBy={(option) => option.section as string}
onChange={handleChange4}
disableCloseOnSelect
Expand Down Expand Up @@ -638,7 +636,6 @@ const ScreenshotTestDemo = (props: Args): JSX.Element => {
</p>
<DropdownMenu
{...props}
hasSections
groupBy={
groupBy &&
((option: (typeof SCREENSHOT_TEST_OPTIONS)[number]) =>
Expand Down
Loading

0 comments on commit e9d4df0

Please sign in to comment.