From d5d9b79d0f8d4f551bc93b026fc53f0aaa76d53e Mon Sep 17 00:00:00 2001 From: huangminjian Date: Thu, 21 Apr 2022 17:49:29 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=9C=A8constructor=E5=92=8CgetDerivedStat?= =?UTF-8?q?eFromProps=E4=B8=AD=E4=BF=AE=E6=94=B9defaultChecked=E7=9A=84?= =?UTF-8?q?=E4=BC=98=E5=85=88=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.jsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.jsx b/src/index.jsx index dd42cdb..f293e8d 100644 --- a/src/index.jsx +++ b/src/index.jsx @@ -21,7 +21,7 @@ class Checkbox extends Component { constructor(props) { super(props); - const checked = 'checked' in props ? props.checked : props.defaultChecked; + const checked = 'defaultChecked' in props ? props.defaultChecked : 'checked' in props ? props.checked : false; this.state = { checked, @@ -32,7 +32,7 @@ class Checkbox extends Component { if ('checked' in props) { return { ...state, - checked: props.checked, + checked: 'defaultChecked' in props ? props.defaultChecked : props.checked, }; } return null; From 4e0be680dbd65acbca53f67d8260cedd7b776e91 Mon Sep 17 00:00:00 2001 From: huangminjian Date: Fri, 22 Apr 2022 13:47:20 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=BD=93checked=E5=80=BC=E4=B8=BAundefined?= =?UTF-8?q?=E6=88=96null=E6=97=B6=E8=BF=9B=E8=A1=8C=E7=89=B9=E6=AE=8A?= =?UTF-8?q?=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/examples/simple.jsx | 27 +++++++++++++++++++++++++++ src/index.jsx | 21 +++++++++++++-------- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/docs/examples/simple.jsx b/docs/examples/simple.jsx index e201972..01d5960 100644 --- a/docs/examples/simple.jsx +++ b/docs/examples/simple.jsx @@ -130,6 +130,33 @@ export default class SimpleDemo extends React.Component {

+
+

+ +    +

+

+ +    +

+
+ diff --git a/src/index.jsx b/src/index.jsx index f293e8d..0c31aac 100644 --- a/src/index.jsx +++ b/src/index.jsx @@ -1,4 +1,3 @@ -// eslint-disable-next-line import/no-extraneous-dependencies import React, { Component } from 'react'; import classNames from 'classnames'; @@ -21,7 +20,10 @@ class Checkbox extends Component { constructor(props) { super(props); - const checked = 'defaultChecked' in props ? props.defaultChecked : 'checked' in props ? props.checked : false; + const checked = + 'checked' in props && !(props.checked === undefined || props.checked === null) + ? props.checked + : props.defaultChecked; this.state = { checked, @@ -29,10 +31,10 @@ class Checkbox extends Component { } static getDerivedStateFromProps(props, state) { - if ('checked' in props) { + if ('checked' in props && !(props.checked === undefined || props.checked === null)) { return { ...state, - checked: 'defaultChecked' in props ? props.defaultChecked : props.checked, + checked: props.checked, }; } return null; @@ -46,12 +48,15 @@ class Checkbox extends Component { this.input.blur(); } - handleChange = e => { - const { disabled, onChange } = this.props; + handleChange = (e) => { + const { disabled, onChange, checked } = this.props; if (disabled) { return; } - if (!('checked' in this.props)) { + if ( + !('checked' in this.props) || + ('checked' in this.props && (checked === undefined || checked === null)) + ) { this.setState({ checked: e.target.checked, }); @@ -73,7 +78,7 @@ class Checkbox extends Component { } }; - saveInput = node => { + saveInput = (node) => { this.input = node; };