-
Notifications
You must be signed in to change notification settings - Fork 840
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add
optionMatcher
prop to EuiSelectable
and EuiComboBox
c…
…omponents (#7709)
- Loading branch information
Showing
15 changed files
with
522 additions
and
51 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 @@ | ||
- Added a new, optional `optionMatcher` prop to `EuiSelectable` and `EuiComboBox` allowing passing a custom option matcher function to these components and controlling option filtering for given search string |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import React, { useCallback, useState } from 'react'; | ||
|
||
import { | ||
EuiComboBox, | ||
EuiComboBoxOptionOption, | ||
EuiComboBoxOptionMatcher, | ||
} from '../../../../src'; | ||
|
||
const options: EuiComboBoxOptionOption[] = [ | ||
{ | ||
label: 'Titan', | ||
'data-test-subj': 'titanOption', | ||
}, | ||
{ | ||
label: 'Enceladus', | ||
}, | ||
{ | ||
label: 'Mimas', | ||
}, | ||
{ | ||
label: 'Dione', | ||
}, | ||
{ | ||
label: 'Iapetus', | ||
}, | ||
{ | ||
label: 'Phoebe', | ||
}, | ||
{ | ||
label: 'Rhea', | ||
}, | ||
{ | ||
label: | ||
"Pandora is one of Saturn's moons, named for a Titaness of Greek mythology", | ||
}, | ||
{ | ||
label: 'Tethys', | ||
}, | ||
{ | ||
label: 'Hyperion', | ||
}, | ||
]; | ||
|
||
export default () => { | ||
const [selectedOptions, setSelected] = useState<EuiComboBoxOptionOption[]>( | ||
[] | ||
); | ||
const onChange = (selectedOptions: EuiComboBoxOptionOption[]) => { | ||
setSelected(selectedOptions); | ||
}; | ||
|
||
const startsWithMatcher = useCallback<EuiComboBoxOptionMatcher<unknown>>( | ||
({ option, searchValue }) => { | ||
return option.label.startsWith(searchValue); | ||
}, | ||
[] | ||
); | ||
|
||
return ( | ||
<EuiComboBox | ||
placeholder="Select options" | ||
options={options} | ||
selectedOptions={selectedOptions} | ||
onChange={onChange} | ||
isClearable={true} | ||
optionMatcher={startsWithMatcher} | ||
/> | ||
); | ||
}; |
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
71 changes: 71 additions & 0 deletions
71
src-docs/src/views/selectable/selectable_option_matcher.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,71 @@ | ||
import React, { useCallback, useState } from 'react'; | ||
|
||
import { EuiSelectable, EuiSelectableOption } from '../../../../src'; | ||
import { EuiSelectableOptionMatcher } from '../../../../src/components/selectable/selectable'; | ||
|
||
export default () => { | ||
const [options, setOptions] = useState<EuiSelectableOption[]>([ | ||
{ | ||
label: 'Titan', | ||
'data-test-subj': 'titanOption', | ||
}, | ||
{ | ||
label: 'Enceladus is disabled', | ||
disabled: true, | ||
}, | ||
{ | ||
label: 'Mimas', | ||
checked: 'on', | ||
}, | ||
{ | ||
label: 'Dione', | ||
}, | ||
{ | ||
label: 'Iapetus', | ||
checked: 'on', | ||
}, | ||
{ | ||
label: 'Phoebe', | ||
}, | ||
{ | ||
label: 'Rhea', | ||
}, | ||
{ | ||
label: | ||
"Pandora is one of Saturn's moons, named for a Titaness of Greek mythology", | ||
}, | ||
{ | ||
label: 'Tethys', | ||
}, | ||
{ | ||
label: 'Hyperion', | ||
}, | ||
]); | ||
|
||
const startsWithMatcher = useCallback<EuiSelectableOptionMatcher<unknown>>( | ||
({ option, searchValue }) => { | ||
return option.label.startsWith(searchValue); | ||
}, | ||
[] | ||
); | ||
|
||
return ( | ||
<EuiSelectable | ||
aria-label="Searchable example" | ||
searchable | ||
searchProps={{ | ||
'data-test-subj': 'selectableSearchHere', | ||
}} | ||
options={options} | ||
onChange={(newOptions) => setOptions(newOptions)} | ||
optionMatcher={startsWithMatcher} | ||
> | ||
{(list, search) => ( | ||
<> | ||
{search} | ||
{list} | ||
</> | ||
)} | ||
</EuiSelectable> | ||
); | ||
}; |
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
Oops, something went wrong.