Skip to content

Commit

Permalink
feat(checkbox): add onClick prop (#9827)
Browse files Browse the repository at this point in the history
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
tay1orjones and kodiakhq[bot] authored Oct 12, 2021
1 parent 1730d73 commit dd1d037
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ Map {
"defaultProps": Object {
"indeterminate": false,
"onChange": [Function],
"onClick": [Function],
},
"displayName": "Checkbox",
"propTypes": Object {
Expand Down Expand Up @@ -354,6 +355,9 @@ Map {
"onChange": Object {
"type": "func",
},
"onClick": Object {
"type": "func",
},
"title": Object {
"type": "string",
},
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/components/Checkbox/Checkbox-story.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const props = () => ({
hideLabel: boolean('No label (hideLabel)', false),
wrapperClassName: text('Wrapper CSS class name (wrapperClassName)', ''),
onChange: action('onChange'),
onClick: action('onClick'),
});

export const playground = () => (
Expand Down
20 changes: 19 additions & 1 deletion packages/react/src/components/Checkbox/Checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const Checkbox = React.forwardRef(function Checkbox(
id,
labelText,
onChange,
onClick,
indeterminate,
hideLabel,
wrapperClassName,
Expand All @@ -44,7 +45,15 @@ const Checkbox = React.forwardRef(function Checkbox(
);

return (
<div className={wrapperClasses}>
/*
The a11y rules below are disabled because <div> element has checkbox
input and label elements as children that allows keyboard interaction.
The onClick handler facilitates consumers' ability to stop click events
from bubbling beyond the checkbox without having to implement their own
wrapper element around <Checkbox>.
*/
/* eslint-disable-next-line jsx-a11y/no-static-element-interactions, jsx-a11y/click-events-have-key-events */
<div className={wrapperClasses} onClick={(evt) => onClick(evt)}>
<input
{...other}
type="checkbox"
Expand Down Expand Up @@ -123,6 +132,14 @@ Checkbox.propTypes = {
*/
onChange: PropTypes.func,

/**
* Provide an optional click handler that is applied to the wrapper div
* containing both the input and the span label. As such, this will be
* called twice for every click - once for the input, a second time for the label.
* Receives the dom event as its only argument.
*/
onClick: PropTypes.func,

/**
* Specify a title for the <label> node for the Checkbox
*/
Expand All @@ -139,6 +156,7 @@ Checkbox.propTypes = {

Checkbox.defaultProps = {
onChange: () => {},
onClick: () => {},
indeterminate: false,
};

Expand Down

0 comments on commit dd1d037

Please sign in to comment.