Skip to content

Commit

Permalink
fix(tile): adds onChange call to handlers in selectable tile (#5156)
Browse files Browse the repository at this point in the history
* fix(selectabletile): adjust handlers so that onchange function is called

* fix(tile): add handlonchange function for VO compatibility
  • Loading branch information
abbeyhrt authored Jan 27, 2020
1 parent f3422c4 commit 1ebf04a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
30 changes: 25 additions & 5 deletions packages/react/src/components/Tile/Tile-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,15 @@ describe('Tile', () => {
});

describe('Renders selectable tile as expected', () => {
const wrapper = mount(
<SelectableTile className="extra-class">
<div className="child">Test</div>
</SelectableTile>
);
let wrapper;
let label;

beforeEach(() => {
wrapper = mount(
<SelectableTile className="extra-class">
<div className="child">Test</div>
</SelectableTile>
);
wrapper.state().selected = false;
label = wrapper.find('label');
});
Expand Down Expand Up @@ -200,6 +201,25 @@ describe('Tile', () => {
expect(wrapper.props().light).toEqual(true);
expect(wrapper.childAt(1).hasClass('bx--tile--light')).toEqual(true);
});

it('should call onChange when the checkbox value changes', () => {
const onChange = jest.fn();
const wrapper = mount(
<SelectableTile onChange={onChange}>
<span id="test-id">test</span>
</SelectableTile>
);

const content = wrapper.find('#test-id');

// Tile becomes selected
content.simulate('click');
expect(onChange).toHaveBeenCalledTimes(1);

// Tile becomes un-selected
content.simulate('click');
expect(onChange).toHaveBeenCalledTimes(2);
});
});

describe('Renders expandable tile as expected', () => {
Expand Down
23 changes: 10 additions & 13 deletions packages/react/src/components/Tile/Tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,19 +261,15 @@ export class SelectableTile extends Component {
handleClick = evt => {
evt.preventDefault();
evt.persist();
const isInput = evt.target === this.input;
if (!isInput) {
this.setState(
{
selected: !this.state.selected,
},
() => {
this.props.handleClick(evt);
}
);
} else {
this.props.handleClick(evt);
}
this.setState(
{
selected: !this.state.selected,
},
() => {
this.props.handleClick(evt);
this.props.onChange(evt);
}
);
};

handleKeyDown = evt => {
Expand All @@ -286,6 +282,7 @@ export class SelectableTile extends Component {
},
() => {
this.props.handleKeyDown(evt);
this.props.onChange(evt);
}
);
} else {
Expand Down

0 comments on commit 1ebf04a

Please sign in to comment.