-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add multi listbox component (#14540)
- Loading branch information
Showing
7 changed files
with
217 additions
and
16 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
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
52 changes: 52 additions & 0 deletions
52
airbyte-webapp/src/components/ui/ListBox/MultiListBox.stories.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,52 @@ | ||
import { StoryObj } from "@storybook/react"; | ||
import { useState } from "react"; | ||
|
||
import { Option } from "./ListBox"; | ||
import { MultiListBox, MultiListBoxProps } from "./MultiListBox"; | ||
|
||
export default { | ||
title: "ui/MultiListBox", | ||
component: MultiListBox, | ||
} as StoryObj<typeof MultiListBox>; | ||
|
||
const exampleOptions: Array<Option<number>> = [ | ||
{ | ||
label: "one", | ||
value: 1, | ||
}, | ||
{ | ||
label: "two", | ||
value: 2, | ||
}, | ||
{ | ||
label: "three", | ||
value: 3, | ||
}, | ||
{ | ||
label: "four", | ||
value: 4, | ||
}, | ||
{ | ||
label: "five", | ||
value: 5, | ||
}, | ||
]; | ||
|
||
export const Default: StoryObj<typeof MultiListBox<number>> = { | ||
args: { | ||
label: "Items", | ||
}, | ||
render: (args) => <MultiListBoxExample {...args} />, | ||
}; | ||
|
||
const MultiListBoxExample = (args: MultiListBoxProps<number>) => { | ||
const [selectedValues, setSelectedValues] = useState<number[]>([]); | ||
return ( | ||
<MultiListBox | ||
{...args} | ||
selectedValues={selectedValues} | ||
onSelectValues={setSelectedValues} | ||
options={exampleOptions} | ||
/> | ||
); | ||
}; |
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,98 @@ | ||
import { | ||
Listbox, | ||
ListboxOption as OriginalListboxOption, | ||
ListboxButton as OriginalListboxButton, | ||
ListboxOptions as OriginalListboxOptions, | ||
} from "@headlessui/react"; | ||
import { Float } from "@headlessui-float/react"; | ||
import classNames from "classnames"; | ||
import isEqual from "lodash/isEqual"; | ||
import React from "react"; | ||
|
||
import { Text } from "components/ui/Text"; | ||
|
||
import styles from "./ListBox.module.scss"; | ||
import { Badge } from "../Badge"; | ||
import { CheckBox } from "../CheckBox"; | ||
import { FlexContainer } from "../Flex"; | ||
import { Icon } from "../Icon"; | ||
|
||
export interface ListBoxControlButtonProps<T> { | ||
selectedValues: T[]; | ||
isDisabled?: boolean; | ||
label: string; | ||
} | ||
|
||
const DefaultControlButton = <T,>({ selectedValues, label }: ListBoxControlButtonProps<T>) => { | ||
return ( | ||
<> | ||
<FlexContainer> | ||
<Text>{label}</Text> | ||
{selectedValues.length > 0 && <Badge variant="blue">{selectedValues.length}</Badge>} | ||
</FlexContainer> | ||
<Icon type="chevronDown" color="action" /> | ||
</> | ||
); | ||
}; | ||
|
||
export interface Option<T> { | ||
label: React.ReactNode; | ||
value: T; | ||
icon?: React.ReactNode; | ||
disabled?: boolean; | ||
"data-testid"?: string; | ||
} | ||
|
||
export interface MultiListBoxProps<T> { | ||
options: Array<Option<T>>; | ||
selectedValues: T[]; | ||
onSelectValues: (selectedValues: T[]) => void; | ||
isDisabled?: boolean; | ||
controlButton?: React.ComponentType<ListBoxControlButtonProps<T>>; | ||
label: string; | ||
} | ||
|
||
export const MultiListBox = <T,>({ | ||
options, | ||
selectedValues, | ||
onSelectValues, | ||
controlButton: ControlButton = DefaultControlButton, | ||
isDisabled, | ||
label, | ||
}: MultiListBoxProps<T>) => { | ||
return ( | ||
<Listbox value={selectedValues} onChange={onSelectValues} disabled={isDisabled} by={isEqual} multiple> | ||
<Float | ||
placement="bottom-start" | ||
flip | ||
offset={5} // $spacing-sm | ||
> | ||
<OriginalListboxButton className={classNames(styles.button)}> | ||
<ControlButton selectedValues={selectedValues} isDisabled={isDisabled} label={label} /> | ||
</OriginalListboxButton> | ||
<OriginalListboxOptions as="ul" className={classNames(styles.optionsMenu)}> | ||
{options.map(MultiListBoxOption)} | ||
</OriginalListboxOptions> | ||
</Float> | ||
</Listbox> | ||
); | ||
}; | ||
|
||
interface MutliListboxOptionProps<T> { | ||
label: React.ReactNode; | ||
value: T; | ||
} | ||
|
||
const MultiListBoxOption = <T,>({ label, value }: MutliListboxOptionProps<T>) => ( | ||
<OriginalListboxOption as="li" value={value} className={styles.option}> | ||
{({ focus, selected }) => ( | ||
<FlexContainer | ||
alignItems="center" | ||
className={classNames(styles.optionValue, { [styles.selected]: selected, [styles.focus]: focus })} | ||
> | ||
<CheckBox checked={selected} readOnly /> | ||
<Text className={styles.label}>{label}</Text> | ||
</FlexContainer> | ||
)} | ||
</OriginalListboxOption> | ||
); |
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