-
Notifications
You must be signed in to change notification settings - Fork 99
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
Showing
5 changed files
with
126 additions
and
20 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
113 changes: 113 additions & 0 deletions
113
src/components/Select/__stories__/UseSelectOptionsShowcase.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,113 @@ | ||
import React from 'react'; | ||
|
||
import {Button} from '../../Button'; | ||
import {TextInput} from '../../controls'; | ||
import {Select, useSelectOptions} from '../index'; | ||
import type {SelectOption, SelectProps} from '../index'; | ||
|
||
export const UseSelectOptionsShowcase = () => { | ||
const [value, setValue] = React.useState<string[]>([]); | ||
const [filter, setFilter] = React.useState(''); | ||
const filterable = true; | ||
const {options, filteredOptions} = useSelectOptions({ | ||
options: [ | ||
{ | ||
label: 'Group 1', | ||
options: [ | ||
{value: 'val1', content: 'Value 1'}, | ||
{value: 'val2', content: 'Value 2'}, | ||
{value: 'val3', content: 'Value 3'}, | ||
{value: 'val4', content: 'Value 4'}, | ||
], | ||
}, | ||
{ | ||
label: 'Group 2', | ||
options: [ | ||
{value: 'val5', content: 'Value 5'}, | ||
{value: 'val6', content: 'Value 6'}, | ||
{value: 'val7', content: 'Value 7'}, | ||
{value: 'val8', content: 'Value 8'}, | ||
], | ||
}, | ||
], | ||
filter, | ||
filterable, | ||
}); | ||
|
||
const renderFilter: SelectProps['renderFilter'] = ({ | ||
value: filterValue, | ||
ref, | ||
onChange, | ||
onKeyDown, | ||
}) => { | ||
const optionsWithoutGroupLabels = options.filter( | ||
(option) => !('label' in option), | ||
) as SelectOption[]; | ||
const filteredOptionsWithoutGroupLabels = filteredOptions.filter( | ||
(option) => !('label' in option), | ||
) as SelectOption[]; | ||
const allOptionsSelected = Boolean( | ||
value.length && optionsWithoutGroupLabels.length === value.length, | ||
); | ||
const allVisibleOptionsSelected = Boolean( | ||
value.length && | ||
filteredOptionsWithoutGroupLabels | ||
.map((o) => o.value) | ||
.every((o) => value.includes(o)), | ||
); | ||
|
||
const handleAllOptionsButtonClick = () => { | ||
const nextValue = allOptionsSelected | ||
? [] | ||
: optionsWithoutGroupLabels.map((option) => option.value); | ||
setValue(nextValue); | ||
}; | ||
|
||
const handleAllVisibleOptionsButtonClick = () => { | ||
const nextValue = allVisibleOptionsSelected | ||
? [] | ||
: filteredOptionsWithoutGroupLabels.map((option) => option.value); | ||
setValue(nextValue); | ||
}; | ||
|
||
return ( | ||
<div | ||
style={{ | ||
display: 'flex', | ||
flexDirection: 'column', | ||
rowGap: 4, | ||
padding: '4px 4px 0 4px', | ||
}} | ||
> | ||
<TextInput | ||
controlRef={ref} | ||
controlProps={{size: 1}} | ||
value={filterValue} | ||
onUpdate={onChange} | ||
onKeyDown={onKeyDown} | ||
/> | ||
<Button | ||
disabled={!filteredOptionsWithoutGroupLabels.length} | ||
onClick={handleAllVisibleOptionsButtonClick} | ||
> | ||
{allVisibleOptionsSelected ? 'Deselect all visible' : 'Select all visible'} | ||
</Button> | ||
<Button onClick={handleAllOptionsButtonClick}> | ||
{allOptionsSelected ? 'Deselect all' : 'Select all'} | ||
</Button> | ||
</div> | ||
); | ||
}; | ||
|
||
return ( | ||
<Select | ||
value={value} | ||
options={options} | ||
filterable={filterable} | ||
multiple={true} | ||
renderFilter={renderFilter} | ||
onFilterChange={setFilter} | ||
onUpdate={setValue} | ||
/> | ||
); | ||
}; |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './Select'; | ||
export * from './types'; | ||
export {SelectQa} from './constants'; | ||
export * from './hooks-public'; |
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