-
Notifications
You must be signed in to change notification settings - Fork 841
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[EuiComboBox] Added
sortMatchesBy
prop (#3089)
* added startingWith prop to combobox * Update CHANGELOG.md * Update src/components/combo_box/combo_box.tsx Co-Authored-By: Caroline Horn <549577+cchaos@users.noreply.github.com> * updated prop * added test * Update src-docs/src/views/combo_box/combo_box_example.js Co-Authored-By: Caroline Horn <549577+cchaos@users.noreply.github.com> * Update src-docs/src/views/combo_box/combo_box_example.js Co-Authored-By: Caroline Horn <549577+cchaos@users.noreply.github.com> * Update src/components/combo_box/combo_box.test.tsx Co-Authored-By: Greg Thompson <thompsongl@users.noreply.github.com> * Update src/components/combo_box/combo_box.test.tsx Co-Authored-By: Greg Thompson <thompsongl@users.noreply.github.com> * resolved eslint errors Co-authored-by: Caroline Horn <549577+cchaos@users.noreply.github.com> Co-authored-by: Greg Thompson <thompsongl@users.noreply.github.com>
- Loading branch information
1 parent
08e84c6
commit 93ca8ef
Showing
5 changed files
with
175 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import React, { Component } from 'react'; | ||
|
||
import { EuiComboBox } from '../../../../src/components'; | ||
|
||
export default class extends Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.options = [ | ||
{ | ||
label: 'Titan', | ||
'data-test-subj': 'titanOption', | ||
}, | ||
{ | ||
label: 'Enceladus is disabled', | ||
disabled: true, | ||
}, | ||
{ | ||
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', | ||
}, | ||
]; | ||
|
||
this.state = { | ||
selectedOptions: [this.options[2], this.options[4]], | ||
}; | ||
} | ||
|
||
onChange = selectedOptions => { | ||
this.setState({ | ||
selectedOptions, | ||
}); | ||
}; | ||
|
||
onCreateOption = (searchValue, flattenedOptions) => { | ||
const normalizedSearchValue = searchValue.trim().toLowerCase(); | ||
|
||
if (!normalizedSearchValue) { | ||
return; | ||
} | ||
|
||
const newOption = { | ||
label: searchValue, | ||
}; | ||
|
||
// Create the option if it doesn't exist. | ||
if ( | ||
flattenedOptions.findIndex( | ||
option => option.label.trim().toLowerCase() === normalizedSearchValue | ||
) === -1 | ||
) { | ||
this.options.push(newOption); | ||
} | ||
|
||
// Select the option. | ||
this.setState(prevState => ({ | ||
selectedOptions: prevState.selectedOptions.concat(newOption), | ||
})); | ||
}; | ||
|
||
render() { | ||
const { selectedOptions } = this.state; | ||
return ( | ||
<EuiComboBox | ||
sortMatchesBy="startsWith" | ||
placeholder="Select or create options" | ||
options={this.options} | ||
selectedOptions={selectedOptions} | ||
onChange={this.onChange} | ||
onCreateOption={this.onCreateOption} | ||
isClearable={true} | ||
data-test-subj="demoComboBox" | ||
/> | ||
); | ||
} | ||
} |
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