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

gt - Update FormChoice to support Bootstrap 4 #438

Merged
merged 1 commit into from
Aug 4, 2018
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
2 changes: 0 additions & 2 deletions src/components/FormChoice.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import * as React from 'react';

interface FormChoiceProps extends Omit<React.InputHTMLAttributes<HTMLOptionElement>, 'disabled'> {
inline?: boolean;
color?: string;
state?: string;
disabled?: boolean;
checked?: boolean;
type: 'checkbox' | 'radio' | 'select';
Expand Down
99 changes: 60 additions & 39 deletions src/components/FormChoice.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,81 @@
import React from 'react';
import PropTypes from 'prop-types';
import classname from 'classnames';
import FormGroup from './FormGroup';
import Input from './Input';
import Label from './Label';

const FormChoice = props => {
const {
inline,
color,
state,
disabled,
checked,
children,
type,
value,
selected,
...attributes
} = props;

if (type === 'select') {
return <option {...attributes} value={value} children={children} />;
class FormChoice extends React.Component {
static propTypes = {
inline: PropTypes.bool,
disabled: PropTypes.bool,
checked: PropTypes.bool,
children: PropTypes.node,
type: PropTypes.oneOf(['checkbox', 'radio', 'select']),
value: PropTypes.any,
selected: PropTypes.bool
}
render() {
const {
inline,
disabled,
checked,
children,
type,
value,
selected,
...attributes
} = this.props;

const labelClasses = classname({ 'form-check-inline': inline });
if (type === 'select') {
return <option {...attributes} value={value}>{children}</option>;
}

const computedValue = value || children;
const labelClasses = classname('form-check', { 'form-check-inline': inline });

let computedChecked;
const computedValue = value || children;

if (type === 'checkbox') {
computedChecked = selected && selected.indexOf(computedValue) > -1;
} else if (type === 'radio') {
computedChecked = selected && selected === computedValue;
} else {
throw new Error(`Type '${type}' is not supported`);
}
let computedChecked;

if (type === 'checkbox') {
computedChecked = selected && selected.indexOf(computedValue) > -1;
} else if (type === 'radio') {
computedChecked = selected && selected === computedValue;
} else {
throw new Error(`Type '${type}' is not supported`);
}

const item = (
<Label className={labelClasses} check={!inline}>
<Input type={type} {...attributes} disabled={disabled} value={computedValue} checked={computedChecked} />
<span children={children} />
</Label>
);
const item = (
<div className={labelClasses} check={!inline}>
<Input
type={type}
{...attributes}
disabled={disabled}
value={computedValue}
checked={computedChecked}
style={{ cursor: disabled && 'not-allowed' }}
/>
<Label
className="form-check-label"
style={{ cursor: disabled && 'not-allowed' }}
>
{children}
</Label>
</div>
);

if (inline) {
return item;
} else {
if (inline) {
return item;
}
return (
<FormGroup
check
color={color || state}
disabled={disabled}
children={item}
/>
>
{item}
</FormGroup>
);
}
};
}

export default FormChoice;
10 changes: 4 additions & 6 deletions test/components/FormChoice.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('<FormChoice />', () => {
);

assert.equal(component.find(Input).prop('type'), 'checkbox');
assert.equal(component.find('span').text(), 'my type');
assert.equal(component.find(Label).props().children, 'my type');
assert.equal(component.type(), FormGroup);
});

Expand All @@ -48,8 +48,7 @@ describe('<FormChoice />', () => {
);

assert.equal(component.find(Input).prop('type'), 'checkbox');
assert.equal(component.find('span').text(), 'my type');
assert.equal(component.type(), Label);
assert.equal(component.find(Label).props().children, 'my type');
});

describe('with computed value', () => {
Expand Down Expand Up @@ -120,7 +119,7 @@ describe('<FormChoice />', () => {
);

assert.equal(component.find(Input).prop('type'), 'radio');
assert.equal(component.find('span').text(), 'my type');
assert.equal(component.find(Label).props().children, 'my type');
assert.equal(component.type(), FormGroup);
});

Expand All @@ -130,8 +129,7 @@ describe('<FormChoice />', () => {
);

assert.equal(component.find(Input).prop('type'), 'radio');
assert.equal(component.find('span').text(), 'my type');
assert.equal(component.type(), Label);
assert.equal(component.find(Label).props().children, 'my type');
});

describe('with computed value', () => {
Expand Down