-
Notifications
You must be signed in to change notification settings - Fork 843
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[EuiSelectable] Add the single triggering option as a parameter to onChange
#6487
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,11 +108,13 @@ export type EuiSelectableListProps<T> = EuiSelectableOptionsListProps & { | |
*/ | ||
searchValue: string; | ||
/** | ||
* Returns the array of options with altered checked state | ||
* Returns the array of options with altered checked state, the click/keyboard event, | ||
* and the option that triggered the click/keyboard event | ||
*/ | ||
onOptionClick: ( | ||
options: Array<EuiSelectableOption<T>>, | ||
event: EuiSelectableOnChangeEvent | ||
event: EuiSelectableOnChangeEvent, | ||
clickedOption: EuiSelectableOption<T> | ||
) => void; | ||
/** | ||
* Custom render for the label portion of the option; | ||
|
@@ -450,6 +452,7 @@ export class EuiSelectableList<T> extends Component<EuiSelectableListProps<T>> { | |
event: EuiSelectableOnChangeEvent | ||
) => { | ||
const { onOptionClick, options, singleSelection } = this.props; | ||
let changedOption = { ...addedOption }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor technical note/aside: cloning |
||
|
||
const updatedOptions = options.map((option) => { | ||
// if singleSelection is enabled, uncheck any selected option(s) | ||
|
@@ -461,50 +464,54 @@ export class EuiSelectableList<T> extends Component<EuiSelectableListProps<T>> { | |
// if this is the now-selected option, check it | ||
if (option === addedOption) { | ||
updatedOption.checked = 'on'; | ||
changedOption = updatedOption; | ||
} | ||
|
||
return updatedOption; | ||
}); | ||
|
||
onOptionClick(updatedOptions, event); | ||
onOptionClick(updatedOptions, event, changedOption); | ||
}; | ||
|
||
private onRemoveOption = ( | ||
removedOption: EuiSelectableOption<T>, | ||
event: EuiSelectableOnChangeEvent | ||
) => { | ||
const { onOptionClick, singleSelection, options } = this.props; | ||
let changedOption = { ...removedOption }; | ||
|
||
const updatedOptions = options.map((option) => { | ||
const updatedOption = { ...option }; | ||
|
||
if (option === removedOption && singleSelection !== 'always') { | ||
delete updatedOption.checked; | ||
changedOption = updatedOption; | ||
} | ||
|
||
return updatedOption; | ||
}); | ||
|
||
onOptionClick(updatedOptions, event); | ||
onOptionClick(updatedOptions, event, changedOption); | ||
}; | ||
|
||
private onExcludeOption = ( | ||
excludedOption: EuiSelectableOption<T>, | ||
event: EuiSelectableOnChangeEvent | ||
) => { | ||
const { onOptionClick, options } = this.props; | ||
excludedOption.checked = 'off'; | ||
let changedOption = { ...excludedOption }; | ||
|
||
const updatedOptions = options.map((option) => { | ||
const updatedOption = { ...option }; | ||
|
||
if (option === excludedOption) { | ||
updatedOption.checked = 'off'; | ||
changedOption = updatedOption; | ||
} | ||
|
||
return updatedOption; | ||
}); | ||
|
||
onOptionClick(updatedOptions, event); | ||
onOptionClick(updatedOptions, event, changedOption); | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
- Added a third argument to `EuiSelectable`'s `onChange` callback. The single `option` object that triggered the `onChange` event is now also passed to consumers with its most recent `checked` state |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Heenawter Please let us know if this satisfies your use case. You can examine the output of
changedOption
at https://eui.elastic.co/pr_6487/#/forms/selectable#options-can-be-excluded (once staging is done spinning up in ~30 mins or so).The returned
changedOption
should be theoption
obj that was clicked/toggled, with thechecked
status updated (either 'on', 'off', or undefined). Its various values should be present in this object.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@constancecchen This is amazing, thank you so much! I tested it out, and it looks like it's doing exactly what we would need! 🎉