Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional label to CheckboxBooleanInput #230

Merged
merged 1 commit into from
Jun 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions src/components/CheckboxBooleanInput.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
import React from 'react';
import React, { Component } from 'react';
import { FormGroup, Input, Label } from 'reactstrap';

const CheckboxBooleanInput = props => (
<FormGroup check>
<Label check>
<Input
type="checkbox"
checked={props.value}
onChange={e => props.onChange && props.onChange(e.target.checked)}
/>
</Label>
</FormGroup>
);
class CheckboxBooleanInput extends Component {
static propTypes = {
checkboxLabel: React.PropTypes.node,
value: React.PropTypes.bool
};

CheckboxBooleanInput.propTypes = {
value: React.PropTypes.bool
render() {
const { checkboxLabel, onChange, value } = this.props;

return (
<FormGroup check>
<Label check>
<Input
type="checkbox"
checked={value}
onChange={e => onChange && onChange(e.target.checked)}
/>
{checkboxLabel && <span className="ml-1" ref="label">{checkboxLabel}</span>}
</Label>
</FormGroup>
);
}
}

export default CheckboxBooleanInput;
2 changes: 1 addition & 1 deletion src/components/CheckboxInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import CheckboxListInput from './CheckboxListInput';
const CheckboxInput = props =>
props.children ?
<CheckboxListInput {...props} /> :
<CheckboxBooleanInput {...props} />
<CheckboxBooleanInput {...props} />;

export default CheckboxInput;
6 changes: 6 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ import BoundForm from './components/BoundForm';
import BoundFormRow from './components/BoundFormRow';
import Callout from './components/Callout.js';
import Close from './components/Close.js';
import CheckboxInput from './components/CheckboxInput.js';
import CheckboxBooleanInput from './components/CheckboxBooleanInput.js';
import CheckboxListInput from './components/CheckboxListInput.js';
import CreditCardNumber from './components/CreditCardNumber.js';
import CreditCardExpiration from './components/CreditCardExpiration.js';
import CreditCardInput from './components/CreditCardInput.js';
Expand Down Expand Up @@ -179,6 +182,9 @@ export {
BoundFormRow,
Callout,
Close,
CheckboxInput,
CheckboxBooleanInput,
CheckboxListInput,
CreditCardNumber,
CreditCardExpiration,
CreditCardInput,
Expand Down
20 changes: 15 additions & 5 deletions test/components/CheckboxBooleanInput.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@
import React from 'react';
import sinon from 'sinon';
import assert from 'assert';
import { shallow } from 'enzyme';
import { mount } from 'enzyme';

import { Input } from 'reactstrap';

import CheckboxBooleanInput from '../../src/components/CheckboxBooleanInput';
import { CheckboxBooleanInput, Input } from '../../src';

describe('<CheckboxBooleanInput />', () => {
let onChange = sinon.stub();

const wrapper = shallow(
const wrapper = mount(
<CheckboxBooleanInput value={true} onChange={onChange} />
);

Expand All @@ -25,6 +23,10 @@ describe('<CheckboxBooleanInput />', () => {
assert.equal(component.children().length, 0);
});

it('should not render checkboxLabel', () => {
assert.equal(wrapper.ref('label').exists(), false);
});

it('should use value for checked state', () => {
assert.strictEqual(component.prop('checked'), true);
});
Expand All @@ -33,4 +35,12 @@ describe('<CheckboxBooleanInput />', () => {
component.simulate('change', { target: { checked: false }});
assert(onChange.calledWith(false));
});

it('should render checkboxLabel if specified', () => {
const wrapped = mount(
<CheckboxBooleanInput checkboxLabel="Yowza" />
);
assert.equal(wrapped.ref('label').text(), 'Yowza');
assert.equal(wrapped.ref('label').exists(), true);
});
});
5 changes: 1 addition & 4 deletions test/components/CheckboxInput.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import React from 'react';
import assert from 'assert';
import { shallow } from 'enzyme';

import CheckboxInput from '../../src/components/CheckboxInput';
import CheckboxBooleanInput from '../../src/components/CheckboxBooleanInput';
import CheckboxListInput from '../../src/components/CheckboxListInput';
import FormChoice from '../../src/components/FormChoice';
import { CheckboxInput, CheckboxBooleanInput, CheckboxListInput, FormChoice } from '../../src';

describe('<CheckboxInput />', () => {
it('should be a CheckboxListInput when there are children (choices)', () => {
Expand Down