Permalink
Browse files

fix checkbox

  • Loading branch information...
yiminghe committed Oct 21, 2016
1 parent 48e6ae9 commit 5960cd11a94570cfc0b874fa2f05ab0f2bce466a
Showing with 5 additions and 17 deletions.
  1. +5 −17 components/checkbox/Checkbox.tsx
@@ -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);
}

0 comments on commit 5960cd1

Please sign in to comment.