Skip to content

Commit

Permalink
Support for arbitrary props and change of onChange to return event in…
Browse files Browse the repository at this point in the history
…stead of value.
  • Loading branch information
eirikv committed Dec 7, 2016
1 parent c95ce76 commit 0a0f325
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 59 deletions.
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
git-tag-version=false
registry=***REMOVED***
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 3.0.0

* Added support for arbitrary props on the input
* Breaking: Removed 'isTabbable' prop - use 'tabIndex' instead!
* Breaking: onChange now returns the event instead of just returning the value of the checkbox.

## 2.2.1

* Added support for ffe-form version 3.x.x
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import Checkbox from 'ffe-checkbox-react';
name={ string }
label={ string }
noMargins={ boolean } // disables top- and bottom margins, useful for use in tables etc
isTabbable={ false } // Remove checkbox from tab order by setting it to false. Defaults to true.
/>
```

Expand Down
10 changes: 1 addition & 9 deletions docs/example-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,10 @@ import React from 'react';

export const MainExample = (
<Checkbox
onChange={ () => {} }
checked={false}
name="Yellow"
label="Yellow"
/>
);

export default props => <Checkbox
onChange={props.onChange}
checked={props.checked}
name={props.name}
label={props.label}
noMargins={props.noMargins}
isTabbable={props.isTabbable}
/>;
export default props => <Checkbox {...props} />;
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ffe-checkbox-react",
"version": "2.2.1",
"version": "3.0.0",
"main": "lib/checkbox.js",
"scripts": {
"build": "babel -d lib/. --ignore=*.test.js src/. && npm run example",
Expand All @@ -27,6 +27,7 @@
"babel-cli": "6.7.5",
"babel-plugin-react-remove-prop-types": "^2.0.2",
"babel-plugin-remove-proptypes": "^1.0.3",
"babel-plugin-transform-object-rest-spread": "^6.19.0",
"babel-preset-es2015": "^6.3.13",
"babel-preset-react": "^6.5.0",
"babel-register": "^6.4.3",
Expand Down Expand Up @@ -61,7 +62,8 @@
"react"
],
"plugins": [
"react-remove-prop-types"
"react-remove-prop-types",
"transform-object-rest-spread"
]
}
}
56 changes: 30 additions & 26 deletions src/checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,37 @@ import React, { PropTypes } from 'react';
import hash from 'nfe-hash';
import classNames from 'classnames';

export default function CheckBox({ name, label, onChange, checked, children, noMargins, isTabbable }) {
const id = `checkbox-${hash(name)}`;
const classes = classNames({
'ffe-checkbox': true,
'ffe-checkbox--inline': true,
'ffe-checkbox--no-margin': noMargins
});
export default function CheckBox(props) {
const {
children,
label,
noMargins,
onChange,
...rest,
} = props;

return <span>
<input
onChange={() => onChange(!checked) }
className="ffe-hidden-checkbox"
type="checkbox"
checked={checked}
name={name}
id={id}
{... isTabbable === false ? { tabIndex: '-1' } : {}}
/>
<label className={ classes } htmlFor={id}>
{label || children}
</label>
</span>;
const id = `checkbox-${hash(rest.name)}`;

return (
<span>
<input
onChange={onChange || (f => f)}
className="ffe-hidden-checkbox"
id={id}
type="checkbox"
{...rest}
/>
<label
className={classNames({
'ffe-checkbox': true,
'ffe-checkbox--inline': true,
'ffe-checkbox--no-margin': noMargins
})}
htmlFor={id}
>
{label || children}
</label>
</span>);
}

CheckBox.propTypes = {
Expand All @@ -33,9 +42,4 @@ CheckBox.propTypes = {
checked: PropTypes.bool,
noMargins: PropTypes.bool,
children: PropTypes.array,
isTabbable: PropTypes.bool
};

CheckBox.defaultProps = {
isTabbable: true
};
38 changes: 17 additions & 21 deletions src/checkbox.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,13 @@ describe('<Checkbox />', () => {
});

it('should call onChange with the correct parameter', () => {
let spy = sinon.spy();

let wrapper = shallow(CreateCheckbox({ onChange: spy }));

wrapper.find('input').simulate('change');
assert.equal(spy.args[0][0], true);

spy = sinon.spy();
wrapper = shallow(CreateCheckbox({ onChange: spy, checked: true }));
const mockEvent = { target: { value: true } };
const spy = sinon.spy();

wrapper.find('input').simulate('change');
assert.equal(spy.args[0][0], false);
const wrapper = shallow(CreateCheckbox({ onChange: spy }));

wrapper.find('input').simulate('change', mockEvent);
assert.equal(spy.firstCall.args[0], mockEvent);
});

it('should render a default value if passed', () => {
Expand Down Expand Up @@ -61,16 +55,18 @@ describe('<Checkbox />', () => {
1);
});

it('should not set tabindex by default', () => {
const wrapper = shallow(CreateCheckbox({}));
assert.equal(
wrapper.find('input').prop('tabIndex'), undefined);
});

it('should support removing input from taborder', () => {
const wrapper = shallow(CreateCheckbox({ isTabbable: false }));
assert.equal(
wrapper.find('input').prop('tabIndex'), -1);
it('should set arbitrary props (rest) on input', () => {
const wrapper = shallow(CreateCheckbox({
name: 'checkbox',
iDontReallyDoAnything: 'false',
tabIndex: -1,
'aria-invalid': true,
}));

assert.equal(wrapper.find('input').prop('name'), 'checkbox');
assert.equal(wrapper.find('input').prop('aria-invalid'), true);
assert.equal(wrapper.find('input').prop('iDontReallyDoAnything'), 'false');
assert.equal(wrapper.find('input').prop('tabIndex'), -1);
});

});

0 comments on commit 0a0f325

Please sign in to comment.