-
Notifications
You must be signed in to change notification settings - Fork 353
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(SimpleSelect,CheckboxSelect): Handle initial selection(s) (#10734)
- Loading branch information
1 parent
f31177f
commit 3b857f8
Showing
5 changed files
with
60 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
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
33 changes: 25 additions & 8 deletions
33
packages/react-templates/src/components/Select/examples/CheckboxSelectDemo.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 |
---|---|---|
@@ -1,13 +1,30 @@ | ||
import React from 'react'; | ||
import { CheckboxSelect, CheckboxSelectOption } from '@patternfly/react-templates'; | ||
|
||
const Options: { content: string; value: string; description?: string; isDisabled?: boolean }[] = [ | ||
{ content: 'Option 1', value: 'option-1' }, | ||
{ content: 'Option 2', value: 'option-2', description: 'Option with description' }, | ||
{ content: 'Option 3', value: 'option-3', isDisabled: true }, | ||
{ content: 'Option 4', value: 'option-4' } | ||
]; | ||
|
||
export const SelectBasic: React.FunctionComponent = () => { | ||
const initialOptions: CheckboxSelectOption[] = [ | ||
{ content: 'Option 1', value: 'option-1' }, | ||
{ content: 'Option 2', value: 'option-2', description: 'Option with description' }, | ||
{ content: 'Option 3', value: 'option-3', isDisabled: true }, | ||
{ content: 'Option 4', value: 'option-4' } | ||
]; | ||
|
||
return <CheckboxSelect initialOptions={initialOptions} />; | ||
const [selected, setSelected] = React.useState<string[]>(['option-2']); | ||
|
||
const initialOptions = React.useMemo<CheckboxSelectOption[]>( | ||
() => Options.map((o) => ({ ...o, selected: selected.includes(o.value) })), | ||
[selected] | ||
); | ||
|
||
return ( | ||
<CheckboxSelect | ||
initialOptions={initialOptions} | ||
onSelect={(_ev, value) => { | ||
const val = String(value); | ||
setSelected((prevSelected) => | ||
prevSelected.includes(val) ? prevSelected.filter((item) => item !== val) : [...prevSelected, String(val)] | ||
); | ||
}} | ||
/> | ||
); | ||
}; |
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