Skip to content

Commit

Permalink
Update and cleanup the Checkbox component
Browse files Browse the repository at this point in the history
  • Loading branch information
belcherj committed Sep 21, 2019
1 parent 0549faa commit 30cfb83
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 4 deletions.
57 changes: 57 additions & 0 deletions lib/components/checkbox/__snapshots__/test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`renders Checkbox correctly when checked 1`] = `
<span
aria-checked={false}
className="checkbox"
role="checkbox"
tabIndex="0"
>
<span
aria-hidden="true"
className="checkbox__icon"
>
<svg
className="icon-circle"
height="24px"
viewBox="0 0 24 24"
width="24px"
xmlns="http://www.w3.org/2000/svg"
>
<g
transform="translate(2 2)"
>
<path
d="m10 0c-5.523 0-10 4.477-10 10s4.477 10 10 10 10-4.477 10-10-4.477-10-10-10zm0 18.5c-4.694 0-8.5-3.806-8.5-8.5s3.806-8.5 8.5-8.5 8.5 3.806 8.5 8.5-3.806 8.5-8.5 8.5z"
/>
</g>
</svg>
</span>
</span>
`;

exports[`renders Checkbox correctly when not checked 1`] = `
<span
aria-checked={true}
className="checkbox"
role="checkbox"
tabIndex="0"
>
<span
aria-hidden="true"
className="checkbox__icon"
>
<svg
className="icon-checkmark"
height="24px"
viewBox="0 0 24 24"
width="24px"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M11,17.768l-4.884-4.884l1.768-1.768L11,14.232l8.658-8.658C17.823,3.391,15.075,2,12,2C6.477,2,2,6.477,2,12 s4.477,10,10,10s10-4.477,10-10c0-1.528-0.353-2.971-0.966-4.266L11,17.768z"
/>
</svg>
</span>
</span>
`;
4 changes: 2 additions & 2 deletions lib/components/checkbox/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import PropTypes from 'prop-types';
import CheckmarkIcon from '../../icons/checkmark';
import CircleIcon from '../../icons/circle';

const Checkbox = ({ checked = false, onChange }) => {
function Checkbox({ checked = false, onChange }) {
// A custom checkbox with an ARIA role is used here to work around a bug in
// DraftJS, where using a hidden <input type="checkbox"> will trigger a error.
return (
Expand All @@ -20,7 +20,7 @@ const Checkbox = ({ checked = false, onChange }) => {
</span>
</span>
);
};
}

Checkbox.propTypes = {
checked: PropTypes.bool,
Expand Down
4 changes: 2 additions & 2 deletions lib/components/checkbox/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
.checkbox__icon {
display: inline;
position: relative;
top: -.09em;
top: -0.09em;

svg {
width: 1.3em;
height: 1.3em;
width: 1.3em;
}
}
20 changes: 20 additions & 0 deletions lib/components/checkbox/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from 'react';
import Checkbox from './';
import renderer from 'react-test-renderer';

it('renders Checkbox correctly when checked', () => {
const tree = renderer.create(<Checkbox checked={false}></Checkbox>).toJSON();
expect(tree).toMatchSnapshot();
});

it('renders Checkbox correctly when not checked', () => {
const tree = renderer.create(<Checkbox checked={true}></Checkbox>).toJSON();
expect(tree).toMatchSnapshot();
});

it('should call onChange prop when span is clicked', () => {
const noop = jest.fn();
const output = renderer.create(<Checkbox onChange={noop}></Checkbox>);
output.root.findByType('span').props.onClick();
expect(noop).toHaveBeenCalled();
});

0 comments on commit 30cfb83

Please sign in to comment.