Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
… into prod
  • Loading branch information
masoudmanson committed Sep 18, 2024
2 parents 9af8fb6 + f96cb8d commit beb97d2
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 15 deletions.
1 change: 1 addition & 0 deletions packages/components/src/core/ButtonIcon/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ const doNotForwardProps = [
"on",
"sdsSize",
"sdsType",
"sdsStyle",
"sdsIcon",
"sdsIconProps",
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { DropdownMenu } from "./stories/default";
import { ScreenshotTestDemo } from "./stories/screenshot";
import { TestDemo } from "./stories/test";
import { TitleElementDemo } from "./stories/titleElement";

export default {
argTypes: {
Expand Down Expand Up @@ -150,3 +151,25 @@ export const Test = {
},
render: (args: Args) => <TestDemo data-testid="dropdown-menu" {...args} />,
};

export const TitleElement = {
args: {
keepSearchOnSelect: false,
multiple: true,
search: true,
title: DROPDOWN_MENU_LABEL,
},
parameters: {
controls: {
exclude: DROPDOWN_MENU_EXCLUDED_CONTROLS.filter(
(control) => control !== "search" && control !== "title"
),
},
snapshot: {
skip: true,
},
},
render: (args: Args) => (
<TitleElementDemo data-testid="dropdown-menu" {...args} />
),
};
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const DropdownMenu = <
<div style={{ margin: "16px 0 0 24px" }} ref={anchorRef}>
{anchorEl ? (
<RawDropdownMenu<T, Multiple, DisableClearable, FreeSolo>
{...props}
label={label}
anchorEl={anchorEl}
disableCloseOnSelect={false}
Expand All @@ -47,7 +48,6 @@ export const DropdownMenu = <
getOptionDisabled={(option: T) => {
return option.name === "Type: feature request";
}}
{...props}
/>
) : null}
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { AutocompleteValue } from "@mui/base";
import { Args } from "@storybook/react";
import { useEffect, useRef, useState } from "react";
import { AUTOCOMPLETE_SINGLE_COLUMN_OPTIONS } from "src/common/storybook/AUTOCOMPLETE_SINGLE_COLUMN_OPTIONS";
import { DefaultAutocompleteOption } from "src/core/Autocomplete";
import Button from "src/core/Button";
import RawDropdownMenu from "src/core/DropdownMenu";

export const TitleElementDemo = <
T extends DefaultAutocompleteOption,
Multiple extends boolean | undefined,
DisableClearable extends boolean | undefined,
FreeSolo extends boolean | undefined,
>(
props: Args
): JSX.Element => {
const {
multiple,
options = AUTOCOMPLETE_SINGLE_COLUMN_OPTIONS,
search = false,
} = props;

const anchorRef = useRef(null);
const [anchorEl, setAnchorEl] = useState<HTMLElement | null>(null);

const [value, setValue] = useState<
AutocompleteValue<T, Multiple, DisableClearable, FreeSolo>
>(getInitialValue(false));

const [pendingValue, setPendingValue] = useState<
AutocompleteValue<T, Multiple, DisableClearable, FreeSolo>
>(getInitialValue(true));

useEffect(() => {
setAnchorEl(anchorRef.current);
}, []);

const CustomHeaderComponent = () => (
<Button sdsStyle="minimal" sdsType="primary">
Click Me!
</Button>
);

return (
<div style={{ margin: "16px 0 0 24px" }} ref={anchorRef}>
{anchorEl ? (
<RawDropdownMenu<T, Multiple, DisableClearable, FreeSolo>
{...props}
label="Search"
anchorEl={anchorEl}
open
search={search}
multiple={multiple}
headerComponentSlot={CustomHeaderComponent()}
value={multiple ? pendingValue : value}
onChange={handleChange}
disableCloseOnSelect={multiple}
options={options}
onClickAway={handleClickAway}
groupBy={(option: T) => option.section as string}
width={300}
/>
) : null}
</div>
);

function handleClickAway() {
return multiple && setValue(pendingValue);
}

function handleChange(
_: React.ChangeEvent<unknown>,
newValue: AutocompleteValue<T, Multiple, DisableClearable, FreeSolo>
) {
if (!multiple) {
setValue(
newValue as AutocompleteValue<T, Multiple, DisableClearable, FreeSolo>
);
}

return setPendingValue(
newValue as AutocompleteValue<T, Multiple, DisableClearable, FreeSolo>
);
}

// eslint-disable-next-line sonarjs/no-identical-functions
function getInitialValue(
isMultiple: boolean
): AutocompleteValue<T, Multiple, DisableClearable, FreeSolo> {
return isMultiple
? ([] as unknown as AutocompleteValue<
T,
Multiple,
DisableClearable,
FreeSolo
>)
: (null as AutocompleteValue<T, Multiple, DisableClearable, FreeSolo>);
}
};
11 changes: 7 additions & 4 deletions packages/components/src/core/DropdownMenu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { SDSTheme } from "src/core/styles";
import {
StyleProps,
StyledDropdownMenuAutocompleteWrapper,
StyledDropdownMenuHeader,
StyledHeaderTitle,
StyledPaper,
StyledPopper,
Expand Down Expand Up @@ -44,6 +45,7 @@ interface ExtraDropdownMenuProps extends StyleProps {
InputBaseProps?: Partial<InputSearchProps>;
PopperBaseProps?: Partial<PopperProps>;
title?: string;
headerComponentSlot?: JSX.Element;
label?: string;
anchorEl: HTMLElement | null;
PopperComponent?: React.JSXElementConstructor<PopperProps>;
Expand Down Expand Up @@ -113,6 +115,7 @@ const DropdownMenu = <
PopperBaseProps = {},
search = false,
title,
headerComponentSlot,
label = "Search",
children,
options,
Expand Down Expand Up @@ -171,10 +174,10 @@ const DropdownMenu = <
{...ClickAwayListenerProps}
>
<StyledDropdownMenuAutocompleteWrapper>
{title && (
<StyledHeaderTitle search={search}>{title}</StyledHeaderTitle>
)}

<StyledDropdownMenuHeader search={search}>
{title && <StyledHeaderTitle>{title}</StyledHeaderTitle>}
{headerComponentSlot && <>{headerComponentSlot}</>}
</StyledDropdownMenuHeader>
{anchorEl && (
<Autocomplete
label={label}
Expand Down
28 changes: 20 additions & 8 deletions packages/components/src/core/DropdownMenu/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,18 @@ const doNotForwardProps = [
"isMultiColumn",
];

interface StyledHeaderTitleProps extends CommonThemeProps {
search: boolean;
}

export const StyledHeaderTitle = styled("div", {
shouldForwardProp: (prop: string) => !doNotForwardProps.includes(prop),
})`
${fontHeaderXs}
${(props: StyledHeaderTitleProps) => {
const { search } = props;
${(props: CommonThemeProps) => {
const spaces = getSpaces(props);
const semanticColors = getSemanticColors(props);
return `
color: ${semanticColors?.base?.textPrimary};
padding-right: ${spaces?.m}px;
margin-bottom: ${search ? spaces?.s : spaces?.m}px;
`;
}}
`;
Expand Down Expand Up @@ -130,3 +123,22 @@ export const StyledPaper = styled(Paper, {
`;
}}
`;

interface StyledDropdownMenuHeaderProps extends CommonThemeProps {
search: boolean;
}

export const StyledDropdownMenuHeader = styled("div")`
${(props: StyledDropdownMenuHeaderProps) => {
const { search } = props;
const spaces = getSpaces(props);
return `
display: flex;
align-items: center;
justify-content: space-between;
margin-right: ${spaces?.m}px;
margin-bottom: ${search ? spaces?.s : spaces?.m}px;
`;
}}
`;
4 changes: 2 additions & 2 deletions packages/components/src/core/Tooltip/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ export const tooltipCss = (props: TooltipExtraProps): string => {

return css`
&.MuiTooltip-tooltip {
box-shadow: ${shadows?.m};
${sdsStyle === "dark" || inverted ? dark(props) : light(props)}
${width === "wide" && sdsStyle === "light" && wide()}
${followCursor === true && tableStyles(props)}
box-shadow: ${shadows?.m};
}
`;
};
Expand Down

0 comments on commit beb97d2

Please sign in to comment.