-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/chanzuckerberg/sci-components…
… into prod
- Loading branch information
Showing
7 changed files
with
153 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -163,6 +163,7 @@ const doNotForwardProps = [ | |
"on", | ||
"sdsSize", | ||
"sdsType", | ||
"sdsStyle", | ||
"sdsIcon", | ||
"sdsIconProps", | ||
]; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
packages/components/src/core/DropdownMenu/__storybook__/stories/titleElement.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters