Skip to content

Commit

Permalink
Breaking: Radio and RadioGroup components
Browse files Browse the repository at this point in the history
  • Loading branch information
lightbringer1991 committed Jun 5, 2018
1 parent a8e9650 commit b0df36a
Show file tree
Hide file tree
Showing 17 changed files with 608 additions and 75 deletions.
1 change: 1 addition & 0 deletions config/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const publicPath = '/docs/assets/';

module.exports = merge(commonConfig, {
entry: [
'babel-polyfill',
// activate HMR for React (needs to be before everything except polyfills)
'react-hot-loader/patch',
// bundle the client for webpack-dev-server and connect to the provided endpoint
Expand Down
3 changes: 3 additions & 0 deletions docs/components/Layout/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import AlertExample from '../../examples/AlertExample';
import CheckboxExample from '../../examples/CheckboxExample';
import CheckboxGroupExample from '../../examples/CheckboxGroupExample';
import RadioExample from '../../examples/RadioExample';
import RadioGroupExample from '../../examples/RadioGroupExample';
import SelectExample from '../../examples/SelectExample';
import DatePickerExample from '../../examples/DatePickerExample';
import BorderedWellExample from '../../examples/BorderedWellExample';
Expand Down Expand Up @@ -77,6 +78,7 @@ const componentsBySection = {
'checkbox',
'checkbox-group',
'radio',
'radio-group',
'select',
'date-picker',
],
Expand Down Expand Up @@ -178,6 +180,7 @@ class PageLayout extends React.Component {
<CheckboxExample />
<CheckboxGroupExample />
<RadioExample />
<RadioGroupExample />
<SelectExample />
<DatePickerExample />

Expand Down
14 changes: 12 additions & 2 deletions docs/examples/CheckboxExample.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ const exampleProps = {
{
label: '',
propTypes: [
{
propType: 'id',
type: 'string',
},
{
propType: 'className',
type: 'string',
note: 'This class will be applied to the input element',
},
{
propType: 'name',
type: 'string',
Expand All @@ -47,23 +56,24 @@ const exampleProps = {
{
propType: 'value',
type: 'string',
note: 'Required.',
},
{
propType: 'checked',
type: 'bool',
defaultValue: <code>false</code>,
},
{
propType: 'disabled',
type: 'bool',
defaultValue: <code>false</code>,
},
{
propType: 'dts',
type: 'string',
},
{
propType: 'onChange',
type: 'function',
type: 'func',
},
],
},
Expand Down
24 changes: 22 additions & 2 deletions docs/examples/CheckboxGroupExample.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,22 @@ const exampleProps = {
{
label: '',
propTypes: [
{
propType: 'id',
type: 'string',
},
{
propType: 'className',
type: 'string',
},
{
propType: 'name',
type: 'string',
note: (
<span>
<strong>Required.</strong> All Checkboxes within this group will have the same name
</span>
),
},
{
propType: 'value',
Expand All @@ -46,11 +59,18 @@ const exampleProps = {
},
{
propType: 'children',
type: '<Checkbox /> elements',
type: 'arrayOf <Checkbox /> elements',
note: <strong>Required.</strong>,
},
{
propType: 'onChange',
type: 'Function',
type: 'func',
note: 'Triggers when selection changes.',
},
{
propType: 'dts',
type: 'string',
note: 'render `data-test-selector` onto the component. It can be useful for testing.',
},
],
},
Expand Down
72 changes: 49 additions & 23 deletions docs/examples/RadioExample.jsx
Original file line number Diff line number Diff line change
@@ -1,48 +1,74 @@
import React from 'react';
import Example from '../components/Example';
import { Radio, RadioGroup } from '../../src';
import Radio from 'adslot-ui/Radio';

class RadioExample extends React.PureComponent {
onChange(event) {
_.noop();
}

render() {
return (
<RadioGroup name="yesNo" className="radiogroup-stacked">
<Radio label="Yes" value="true" />
<Radio label="No" value="false" />
</RadioGroup>
<Radio
name="Radio button name"
label="Radio button label"
dts="radio-button-data-test-selector"
value="Radio button value"
onChange={this.onChange}
/>
);
}
}

const exampleProps = {
componentName: 'Radio',
designNotes: (
<p>
<span className="text-bold">Radio buttons</span> used for making a single selection from multiple options. Only
one selection can ever be made from the radio button group at a time.
</p>
),
notes: (
<p>
See <a href="https://github.com/luqin/react-icheck">React iCheck Documentation</a>
</p>
),
exampleCodeSnippet: `
<RadioGroup name="yesNo" className="radiogroup-stacked">
<Radio label="Yes" value="true" />
<Radio label="No" value="false" />
</RadioGroup>`,
notes: '',
exampleCodeSnippet: `<Radio
name="Radio button name"
label="Radio button label"
dts="radio-button-data-test-selector"
value="Radio button value"
onChange={this.onChange}
/>`,
propTypeSectionArray: [
{
propTypes: [
{
propType: 'id',
type: 'string',
},
{
propType: 'className',
type: 'string',
note: 'This class will be applied to the input element',
},
{
propType: 'name',
type: 'string',
},
{
propType: 'label',
type: 'node',
note: 'Usually fine to rely on a string but can pass HTML e.g. for a url.',
type: 'string',
},
{
propType: 'value',
type: 'string',
},
{
propType: 'dts',
type: 'string',
note: 'render `data-test-selector` onto the component. It can be useful for testing.',
},
{
propType: 'disabled',
type: 'bool',
defaultValue: <code>false</code>,
},
{
propType: 'checked',
type: 'bool',
defaultValue: <code>false</code>,
},
{
propType: 'onChange',
type: 'func',
Expand Down
89 changes: 89 additions & 0 deletions docs/examples/RadioGroupExample.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import React from 'react';
import Example from '../components/Example';
import RadioGroup from 'adslot-ui/RadioGroup';
import Radio from 'adslot-ui/Radio';

class RadioGroupExample extends React.PureComponent {
onChangeGroup(event) {
_.noop();
}

onChangeIndividual(event) {
_.noop();
}

render() {
return (
<RadioGroup name="hobbies" value="badminton" onChange={this.onChangeGroup} dts="radio-group-dts">
<Radio value="swimming" label="Swimming" dts="radio-dts" />
<Radio value="soccer" label="Soccer" onChange={this.onChangeIndividual} />
<Radio value="badminton" label="Badminton" />
</RadioGroup>
);
}
}

const exampleProps = {
componentName: 'RadioGroup',
designNotes: (
<p>
<span className="text-bold">Radio buttons</span> used for making a single selection from multiple options. Only
one selection can ever be made from the radio button group at a time.
</p>
),
notes: '',
exampleCodeSnippet: `<RadioGroup name="hobbies" value="badminton" onChange={this.onChangeGroup} dts="radio-group-dts">
<Radio value="swimming" label="Swimming" dts="radio-dts" />
<Radio value="soccer" label="Soccer" onChange={this.onChangeIndividual} />
<Radio value="badminton" label="Badminton" />
</RadioGroup>`,
propTypeSectionArray: [
{
propTypes: [
{
propType: 'id',
type: 'string',
},
{
propType: 'className',
type: 'string',
},
{
propType: 'name',
type: 'string',
note: (
<span>
<strong>Required.</strong> All Radio buttons within this group will have the same name
</span>
),
},
{
propType: 'value',
type: 'string',
note: 'value of the selected radio button, should be one of children <Radio /> values',
},
{
propType: 'children',
type: 'arrayOf <Radio /> elements',
note: <strong>Required.</strong>,
},
{
propType: 'onChange',
type: 'func',
note: 'Triggers when selection changes.',
},
{
propType: 'dts',
type: 'string',
note: 'render `data-test-selector` onto the component. It can be useful for testing.',
},
],
},
],
};

export default () => (
<Example {...exampleProps}>
<RadioGroupExample />
</Example>
);
49 changes: 16 additions & 33 deletions src/components/adslot-ui/Checkbox/index.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react';
import PropTypes from 'prop-types';
import { expandDts } from 'lib/utils';

import { expandDts } from '../../../lib/utils';
import { checkboxPropTypes } from '../../prop-types/inputPropTypes';
import './styles.scss';

class Checkbox extends React.Component {
Expand All @@ -28,29 +27,24 @@ class Checkbox extends React.Component {
}

render() {
const { name, value, label, dts } = this.props;
const optional = {
id: this.props.id ? this.props.id : null,
className: this.props.className ? this.props.className : null,
const { name, value, label, dts, disabled, id, className } = this.props;
const checkboxInputProps = {
type: 'checkbox',
name,
checked: this.state.checked,
disabled,
onChange: this.onChangeDefault,
value,
id,
className,
};
if (this.props['data-name']) {
optional['data-name'] = this.props['data-name'];
}

return (
<div className="checkbox-component">
<div className="checkbox-component" {...expandDts(dts)}>
<span className={`selection-component-icon icheckbox${this.state.checked ? ' checked' : ''}`} />
<label>
<div className="checkbox-component-input-container">
<input
type="checkbox"
name={name}
value={value}
onChange={this.onChangeDefault}
disabled={this.props.disabled}
checked={this.state.checked}
{...expandDts(dts)}
{...optional}
/>
<input {...checkboxInputProps} />
</div>
{label ? <span>{label}</span> : null}
</label>
Expand All @@ -59,18 +53,7 @@ class Checkbox extends React.Component {
}
}

Checkbox.propTypes = {
id: PropTypes.string,
className: PropTypes.string,
'data-name': PropTypes.string,
name: PropTypes.string,
label: PropTypes.node,
value: PropTypes.string,
dts: PropTypes.string,
disabled: PropTypes.bool,
checked: PropTypes.bool,
onChange: PropTypes.func,
};
Checkbox.propTypes = checkboxPropTypes;

Checkbox.defaultProps = {
dts: '',
Expand Down
5 changes: 2 additions & 3 deletions src/components/adslot-ui/Checkbox/index.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@ describe('Checkbox', () => {
expect(component.state()).to.eql({ checked: true });
});

it('should render with id, className, and data-name', () => {
const component = shallow(<Checkbox id="checkboxId" className="checkboxClass" data-name="checkboxName" />);
it('should render with id, className', () => {
const component = shallow(<Checkbox id="checkboxId" className="checkboxClass" />);
const checkboxElement = component.find('input[type="checkbox"]');
expect(checkboxElement.hasClass('checkboxClass')).to.equal(true);
expect(component.find('#checkboxId')).to.have.length(1);
expect(component.find('[data-name="checkboxName"]')).to.have.length(1);
});

it('should handle change event', () => {
Expand Down
Loading

0 comments on commit b0df36a

Please sign in to comment.