Skip to content

Commit

Permalink
fix checkbox
Browse files Browse the repository at this point in the history
  • Loading branch information
yiminghe committed Oct 21, 2016
1 parent 48e6ae9 commit 5960cd1
Showing 1 changed file with 5 additions and 17 deletions.
22 changes: 5 additions & 17 deletions components/checkbox/Checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,26 @@ export default class Checkbox extends React.Component<CheckboxProps, any> {
constructor(props: CheckboxProps, context: any) {
super(props, context);

let checked;
if (props.checked !== null && props.checked !== undefined) {
checked = !!props.checked;
} else {
checked = !!props.defaultChecked;
}
this.state = {
checked: checked,
checked: props.checked || props.defaultChecked || false,
};
}

componentWillReceiveProps(nextProps: CheckboxProps): void {
if (nextProps.checked !== null && nextProps.checked !== undefined) {
const oldChecked = this.state.checked;
if (nextProps.checked === oldChecked) {
return;
}
if ('checked' in nextProps) {
this.setState({
checked: !!nextProps.checked,
});
}
}

toggle() {
let checked = !this.state.checked;

if (this.props.checked === null || this.props.checked === undefined) {
const checked = !this.state.checked;
if (!('checked' in this.props)) {
this.setState({
checked: checked,
checked,
});
}

if (this.props.onChange) {
this.props.onChange(checked);
}
Expand Down

0 comments on commit 5960cd1

Please sign in to comment.