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

fix(content-switcher): call onChange with correct data with keyboard #6257

Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,74 @@ describe('ContentSwitcher', () => {
expect(secondChild.props().selected).toEqual(true);
});
});

describe('onChange', () => {
it('should call `onChange` with the newly selected switch data when using a keyboard', () => {
const onChange = jest.fn();
const wrapper = mount(
<ContentSwitcher onChange={onChange}>
<Switch name="first" text="first" />
<Switch name="second" text="second" />
<Switch name="third" text="third" />
</ContentSwitcher>
);

wrapper.find({ name: 'first' }).simulate('keydown', {
key: 'ArrowRight',
});
expect(onChange).toHaveBeenCalledTimes(1);
expect(onChange).toHaveBeenLastCalledWith({
index: 1,
name: 'second',
text: 'second',
key: 'ArrowRight',
});

wrapper.find({ name: 'second' }).simulate('keydown', {
key: 'ArrowRight',
});
expect(onChange).toHaveBeenLastCalledWith({
index: 2,
name: 'third',
text: 'third',
key: 'ArrowRight',
});

wrapper.find({ name: 'third' }).simulate('keydown', {
key: 'ArrowRight',
});
expect(onChange).toHaveBeenLastCalledWith({
index: 0,
name: 'first',
text: 'first',
key: 'ArrowRight',
});
});

it('should call `onChange` with the newly selected switch data when using a mouse', () => {
const onChange = jest.fn();
const wrapper = mount(
<ContentSwitcher onChange={onChange}>
<Switch name="first" text="first" />
<Switch name="second" text="second" />
<Switch name="third" text="third" />
</ContentSwitcher>
);

wrapper.find({ name: 'second' }).simulate('click');
expect(onChange).toHaveBeenCalledTimes(1);
expect(onChange).toHaveBeenLastCalledWith({
index: 1,
name: 'second',
text: 'second',
});

wrapper.find({ name: 'third' }).simulate('click');
expect(onChange).toHaveBeenLastCalledWith({
index: 2,
name: 'third',
text: 'third',
});
});
});
});
11 changes: 9 additions & 2 deletions packages/react/src/components/ContentSwitcher/ContentSwitcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export default class ContentSwitcher extends React.Component {

if (matches(data, [keys.ArrowRight, keys.ArrowLeft])) {
const nextIndex = getNextIndex(key, index, this.props.children.length);
const children = React.Children.toArray(this.props.children);
if (selectionMode === 'manual') {
const switchRef = this._switchRefs[nextIndex];
switchRef && switchRef.focus();
Expand All @@ -90,9 +91,15 @@ export default class ContentSwitcher extends React.Component {
selectedIndex: nextIndex,
},
() => {
const switchRef = this._switchRefs[nextIndex];
const child = children[this.state.selectedIndex];
const switchRef = this._switchRefs[this.state.selectedIndex];
switchRef && switchRef.focus();
this.props.onChange(data);
this.props.onChange({
...data,
joshblack marked this conversation as resolved.
Show resolved Hide resolved
index: this.state.selectedIndex,
name: child.props.name,
text: child.props.text,
});
}
);
}
Expand Down