Skip to content
This repository has been archived by the owner on Oct 6, 2020. It is now read-only.

Commit

Permalink
fix(RadioGroup, Checkbox): imlement getDerivedStateFromProps (#9)
Browse files Browse the repository at this point in the history
* implement getDerivedStateFromProps for checkbox, radiogroup
* update docs to showcase examples
* fix checkbox color props
* only check for undefined values
  • Loading branch information
Hurshal Patel authored Dec 3, 2018
1 parent ff0f468 commit b7c9947
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 26 deletions.
18 changes: 10 additions & 8 deletions src/Form/Checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ export default class Checkbox extends React.Component {
disabled: false,
};

static getDerivedStateFromProps(props, state) {
if (props.value !== undefined && props.value !== state.value) {
return {
value: props.value,
};
}

return null;
}

state = {
value: this.props.value,
};
Expand All @@ -84,14 +94,6 @@ export default class Checkbox extends React.Component {
return this.state.value === this.props.valueTrue;
}

componentDidUpdate() {
if (this.props.value !== undefined && this.state.value !== this.props.value) {
this.setState({
value: this.props.value,
});
}
}

handleChange = () => {
const { valueTrue, valueFalse, onChange } = this.props;

Expand Down
54 changes: 47 additions & 7 deletions src/Form/Checkbox.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Form Checkbox component. Different sizes are available.
<Checkbox id="checkbox" name="checkbox" />
</Playground>

### Checkbox Group
### Checkbox Group vertical
<Playground>
{() => {
const checkboxValues = [
Expand All @@ -37,19 +37,41 @@ Form Checkbox component. Different sizes are available.
label: 'Checkbox 2',
},
];

return (
<div>
<h3>Vertical</h3>
<CheckboxGroup name="checkboxes" id="checkboxes" choices={checkboxValues} />
</div>
)
}}
</Playground>

<h3>Horizontal</h3>
### Checkbox Group horizontal
<Playground>
{() => {
const checkboxValues = [
{
id: 1,
value: 'test',
label: 'Checkbox 1',
},
{
id: 2,
value: 'test2',
label: 'Checkbox 2',
},
];

return (
<div>
<CheckboxGroup horizontal name="checkboxes" id="checkboxes" choices={checkboxValues} />
</div>
)
}}
</Playground>

### Checkbox colors

### Checkbox Group exclusivity
<Playground>
{() => {
const checkboxValues = [
Expand All @@ -63,11 +85,29 @@ Form Checkbox component. Different sizes are available.
value: 'test2',
label: 'Checkbox 2',
},
{
id: 3,
value: 'neither',
label: 'Neither',
exclusive: true,
},
];

return (
<React.Fragment>
<Checkbox id="checkbox" name="checkbox" color="green" />
</React.Fragment>
<div>
<CheckboxGroup horizontal name="checkboxes" id="checkboxes" choices={checkboxValues} />
</div>
)
}}
</Playground>


### Checkbox colors
<Playground>
{() => (
<React.Fragment>
<Checkbox id="checkbox" name="checkbox" colorOn="green" colorOff="red" />
<Checkbox id="checkbox" name="checkbox" colorOn="green" colorOff="red" value={true} />
</React.Fragment>
)}
</Playground>
2 changes: 1 addition & 1 deletion src/Form/Radio.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Form Radio component. Different sizes are available.
},
];
return (
<RadioGroup color="green" name="radio" id="radio" choices={radioValues} />
<RadioGroup colorOn="green" colorOff="red" name="radio" id="radio" choices={radioValues} value='radio2' />
)
}}
</Playground>
25 changes: 15 additions & 10 deletions src/Form/RadioGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export default class RadioGroup extends Component {
name: PropTypes.string,
onChange: PropTypes.func,
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
color: PropTypes.string,
colorOn: PropTypes.string,
colorOff: PropTypes.string,
size: PropTypes.string,
choices: PropTypes.arrayOf(
PropTypes.shape({
Expand All @@ -31,20 +32,23 @@ export default class RadioGroup extends Component {
static defaultProps = {
choices: [],
onChange() {},
color: 'primary',
size: 'md',
};

static getDerivedStateFromProps(props, state) {
if (props.value !== undefined && props.value !== state.value) {
return {
value: props.value,
};
}

return null;
}

state = {
value: this.props.value || null,
};

componentDidUpdate() {
if (this.props.value !== undefined && this.props.value !== this.state.value) {
this.handleChange(null, this.props.value);
}
}

handleChange = (field, value) => {
// Bail out if value is the same
if (this.state.value === value) return;
Expand All @@ -55,7 +59,7 @@ export default class RadioGroup extends Component {
};

render() {
const { choices, error, horizontal, label, name, color, size } = this.props;
const { choices, error, horizontal, label, name, colorOn, colorOff, size } = this.props;

return (
<StyledRadioGroup>
Expand All @@ -74,7 +78,8 @@ export default class RadioGroup extends Component {
name={key}
horizontal={horizontal}
size={size}
color={color}
colorOn={colorOn}
colorOff={colorOff}
label={choiceLabel}
value={this.state.value}
valueTrue={value}
Expand Down

0 comments on commit b7c9947

Please sign in to comment.