-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3d1e48a
commit 7d75327
Showing
5 changed files
with
102 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import React from 'react'; | ||
import { Button } from 'theme-ui'; | ||
import { Multiselect, MultiselectItem } from './Multiselect'; | ||
|
||
export default { | ||
title: 'Components/Multiselect', | ||
component: Multiselect, | ||
}; | ||
|
||
export const overview = () => { | ||
const [state, setState] = React.useState<MultiselectItem[]>([ | ||
{ | ||
label: 'option-1', | ||
selected: true, | ||
}, | ||
{ | ||
label: 'option-2', | ||
selected: false, | ||
}, | ||
{ | ||
label: 'option-3', | ||
selected: false, | ||
}, | ||
]); | ||
return ( | ||
<Multiselect | ||
items={state} | ||
onChange={item => { | ||
setState( | ||
state.map(i => (i === item ? { ...i, selected: !i.selected } : i)), | ||
); | ||
}} | ||
> | ||
<Button>open</Button> | ||
</Multiselect> | ||
); | ||
}; |
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,53 @@ | ||
/** @jsx jsx */ | ||
import { FC } from 'react'; | ||
import { jsx, Box, Label, Checkbox } from 'theme-ui'; | ||
import { Popover, PopoverProps } from '../Popover'; | ||
|
||
export interface MultiselectItem { | ||
label: string; | ||
selected: boolean; | ||
} | ||
export interface MultiselectOwnProps { | ||
/** | ||
* array of items to select from | ||
*/ | ||
items: MultiselectItem[]; | ||
/** | ||
* function called when the selected state of an item changes | ||
*/ | ||
onChange: (item: MultiselectItem) => void; | ||
} | ||
|
||
export type MultiselectProps = MultiselectOwnProps & | ||
Omit<PopoverProps, 'tooltip'>; | ||
|
||
/** | ||
* A Popover multiselect displaying checkboxes for select/unselect. | ||
*/ | ||
export const Multiselect: FC<MultiselectProps> = ({ | ||
items, | ||
onChange, | ||
...props | ||
}) => { | ||
return ( | ||
<Popover | ||
trigger={['click']} | ||
{...props} | ||
tooltip={() => ( | ||
<Box variant="multiselect.container"> | ||
{items.map(item => ( | ||
<Box key={`multi_select_${item.label}`} variant="multiselect.item"> | ||
<Label> | ||
<Checkbox | ||
onChange={() => onChange(item)} | ||
checked={item.selected} | ||
/> | ||
{item.label} | ||
</Label> | ||
</Box> | ||
))} | ||
</Box> | ||
)} | ||
/> | ||
); | ||
}; |
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 @@ | ||
export * from './Multiselect'; |
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