Skip to content
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

Merged
merged 4 commits into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src-docs/src/views/selectable/selectable_exclusion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ export default () => {
aria-label="Example of Selectable supporting exclusions"
allowExclusions
options={options}
onChange={(newOptions) => setOptions(newOptions)}
onChange={(newOptions, event, changedOption) => {
setOptions(newOptions);
console.log({ newOptions, event, changedOption });
Copy link
Contributor Author

@cee-chen cee-chen Dec 15, 2022

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 the option obj that was clicked/toggled, with the checked status updated (either 'on', 'off', or undefined). Its various values should be present in this object.

Copy link
Contributor

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! 🎉

}}
>
{(list) => list}
</EuiSelectable>
Expand Down
10 changes: 9 additions & 1 deletion src/components/selectable/selectable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ describe('EuiSelectable', () => {
});

describe('onChange', () => {
it('calls onChange with selected options array and click/keyboard event', () => {
it('calls onChange with selected options array, click/keyboard event, and the clicked option', () => {
const onChange = jest.fn();
const component = mount(
<EuiSelectable options={options} onChange={onChange}>
Expand All @@ -388,11 +388,19 @@ describe('EuiSelectable', () => {
expect(onChange).toHaveBeenCalledTimes(1);
expect(onChange.mock.calls[0][0][0].checked).toEqual('on');
expect(onChange.mock.calls[0][1].type).toEqual('click');
expect(onChange.mock.calls[0][2]).toEqual({
...options[0],
checked: 'on',
});

component.simulate('keydown', { key: 'Enter', shiftKey: true });
expect(onChange).toHaveBeenCalledTimes(2);
expect(onChange.mock.calls[1][0][0].checked).toEqual('on');
expect(onChange.mock.calls[1][1].type).toEqual('keydown');
expect(onChange.mock.calls[1][2]).toEqual({
...options[0],
checked: 'on',
});
});
});

Expand Down
11 changes: 7 additions & 4 deletions src/components/selectable/selectable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,13 @@ export type EuiSelectableProps<T = {}> = CommonProps &
options: Array<EuiSelectableOption<T>>;
/**
* Passes back the altered `options` array with selected options having `checked: 'on'`.
* Also passes back the React click/keyboard event as a second argument.
* Also passes back the React click/keyboard event as a second argument,
* and the option that triggered the onChange event as a third argument.
*/
onChange?: (
options: Array<EuiSelectableOption<T>>,
event: EuiSelectableOnChangeEvent
event: EuiSelectableOnChangeEvent,
changedOption: EuiSelectableOption<T>
) => void;
/**
* Passes back the current active option whenever the user changes the currently
Expand Down Expand Up @@ -455,7 +457,8 @@ export class EuiSelectable<T = {}> extends Component<

onOptionClick = (
options: Array<EuiSelectableOption<T>>,
event: EuiSelectableOnChangeEvent
event: EuiSelectableOnChangeEvent,
clickedOption: EuiSelectableOption<T>
) => {
const { isPreFiltered, onChange } = this.props;
const { searchValue } = this.state;
Expand All @@ -468,7 +471,7 @@ export class EuiSelectable<T = {}> extends Component<
this.setState({ visibleOptions });

if (onChange) {
onChange(options, event);
onChange(options, event, clickedOption);
}
};

Expand Down
19 changes: 13 additions & 6 deletions src/components/selectable/selectable_list/selectable_list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -450,6 +452,7 @@ export class EuiSelectableList<T> extends Component<EuiSelectableListProps<T>> {
event: EuiSelectableOnChangeEvent
) => {
const { onOptionClick, options, singleSelection } = this.props;
let changedOption = { ...addedOption };
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor technical note/aside: cloning addedOption via spread operator is likely unnecessary here, but I wanted to be very careful not to mutate the option objects being passed to us by consumers no matter what, so this does the same thing const updatedOption = { ...option }; is doing below, just in case


const updatedOptions = options.map((option) => {
// if singleSelection is enabled, uncheck any selected option(s)
Expand All @@ -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);
};
}
1 change: 1 addition & 0 deletions upcoming_changelogs/6487.md
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