Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【React】Select在FormItem下,然后存在并列react element元素时,多选的值不能被清空 #238

Closed
Javey opened this issue Apr 1, 2019 · 0 comments

Comments

@Javey
Copy link
Member

Javey commented Apr 1, 2019

当存在如下结构时:

onDateTimeChange(c, value) {
    let model = this.state.model;
    model.date = value;
    this.setState({
        model: model
    })    
}
        
render() {
    return (
        <Row className="pcdn-panel" style={{marginTop: 0}}>
            <FormItem label="加速域名" model="model.domain" 
                rules={{required: true}} 
                messages={{required: '请至少选择一个域名'}}
            >
                <Select value={this.state.model.domain}
                        style={{width: "400px"}}
                        placeholder="支持多选" 
                        on$change-value={$.proxy(this.onDomainChange, this)} 
                        multiple clearable filterable>
                    <Option value="Monday">星期一</Option>
                </Select>
            </FormItem>
            <hr />
        </Row>
    )
}

多选的Select值无法被清空,原因是FormItem下存在并列的hr元素,当清空值时,首先调用onDomainChange函数改变state,然后改变的值传给FormItemFormItem检测到数据变更,又会再次调用setState改变model属性绑定的属性,这回导致更新队列不会立即执行。

Select内部使用了Animate组件,该组件会在更新之前(_beforeUpdate)计算children,更新之后(_update)根据children执行动画,清空后children会置为删除的子元素,但第二次更新时,children则会为空,因为已经没有需要删除的子元素了。如果_beforeUpdate_update先后执行,则没有问题,但如果执行了两次_beforeUpdate则会导致children丢失了

FormItem绑定的model属性会去setState导致在_beforeUpdate_update之间又插入了一次_beforeUpdate,所以元素不能被正确清空

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant