Skip to content

Commit 9f61093

Browse files
authored
Merge pull request #21 from huwshimi/fix-button-click
Fix button click events
2 parents 60948d9 + 3bb7957 commit 9f61093

File tree

3 files changed

+40
-18
lines changed

3 files changed

+40
-18
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
### Changed
1313

14+
- Correctly pass click events to buttons.
15+
1416
### Removed
1517

1618
## [0.2.0] - 2019-11-03

src/components/Button/Button.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const Button = ({
1010
element: Component = "button",
1111
hasIcon,
1212
inline,
13+
onClick,
1314
...props
1415
}) => {
1516
const classes = classNames(className, `p-button--${appearance}`, {
@@ -18,27 +19,22 @@ const Button = ({
1819
"is-inline": inline
1920
});
2021
const onClickDisabled = e => e.preventDefault();
22+
const commonProps = {
23+
...props,
24+
className: classes,
25+
onClick: disabled ? onClickDisabled : onClick
26+
};
2127

2228
if (Component === "button") {
2329
return (
24-
<button
25-
className={classes}
26-
disabled={disabled}
27-
{...props}
28-
onClick={disabled ? onClickDisabled : undefined}
29-
>
30+
<button {...commonProps} disabled={disabled}>
3031
{children}
3132
</button>
3233
);
3334
}
3435

3536
return (
36-
<Component
37-
className={classes}
38-
aria-disabled={disabled}
39-
{...props}
40-
onClick={disabled ? onClickDisabled : undefined}
41-
>
37+
<Component {...commonProps} aria-disabled={disabled}>
4238
{children}
4339
</Component>
4440
);
@@ -55,7 +51,8 @@ Button.propTypes = {
5551
PropTypes.string
5652
]),
5753
hasIcon: PropTypes.bool,
58-
inline: PropTypes.bool
54+
inline: PropTypes.bool,
55+
onClick: PropTypes.func
5956
};
6057

6158
export default Button;

src/components/Button/Button.test.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,49 @@ describe("Button ", () => {
88
const wrapper = shallow(<Button>Test content</Button>);
99
expect(wrapper).toMatchSnapshot();
1010
});
11-
1211
it("renders as a link", () => {
1312
const wrapper = shallow(<Button element="a">Test content</Button>);
1413
expect(wrapper).toMatchSnapshot();
15-
expect(wrapper.prop("onClick")).toBe(undefined);
14+
});
15+
16+
it("can handle button click events", () => {
17+
const onClick = jest.fn();
18+
const wrapper = shallow(<Button onClick={onClick} />);
19+
wrapper.simulate("click");
20+
expect(onClick).toHaveBeenCalled();
21+
});
22+
23+
it("can handle anchor click events", () => {
24+
const onClick = jest.fn();
25+
const wrapper = shallow(<Button element="a" onClick={onClick} />);
26+
wrapper.simulate("click");
27+
expect(onClick).toHaveBeenCalled();
1628
});
1729

1830
it("correctly disables a button", () => {
19-
const wrapper = shallow(<Button disabled={true} />);
31+
const onClick = jest.fn();
32+
const wrapper = shallow(<Button disabled={true} onClick={onClick} />);
2033
expect(wrapper.prop("disabled")).toBe(true);
2134
expect(wrapper.prop("className").includes("is-disabled")).toBe(false);
2235
expect(wrapper.prop("aria-disabled")).toBe(undefined);
36+
const preventDefault = jest.fn();
37+
wrapper.simulate("click", { preventDefault });
38+
expect(preventDefault).toHaveBeenCalled();
39+
expect(onClick).not.toHaveBeenCalled();
2340
});
2441

2542
it("correctly disables a link", () => {
26-
const wrapper = shallow(<Button element="a" disabled={true} />);
43+
const onClick = jest.fn();
44+
const wrapper = shallow(
45+
<Button element="a" disabled={true} onClick={onClick} />
46+
);
2747
expect(wrapper.prop("className").includes("is-disabled")).toBe(true);
2848
expect(wrapper.prop("aria-disabled")).toBe(true);
2949
expect(wrapper.prop("disabled")).toBe(undefined);
30-
expect(wrapper.prop("onClick")).not.toBe(undefined);
50+
const preventDefault = jest.fn();
51+
wrapper.simulate("click", { preventDefault });
52+
expect(preventDefault).toHaveBeenCalled();
53+
expect(onClick).not.toHaveBeenCalled();
3154
});
3255

3356
it("correctly handle icons", () => {

0 commit comments

Comments
 (0)