antd form + checkbox 全选/反选问题

import React, { Component } from 'react';
import { Form,Checkbox, Row, Col } from 'antd';
const FormItem = Form.Item;
const options1=[
    { label: 'A1', value: 'a1' },
    { label: 'A2', value: 'a2' },
    { label: 'A3', value: 'a3' },
    { label: 'A4', value: 'a4' },
    { label: 'A5', value: 'a5' },
    { label: 'A6', value: 'a6' },
    { label: 'A7', value: 'a7' },
    { label: 'A8', value: 'a8' },
    { label: 'A9', value: 'a9' },
    { label: 'A10', value: 'a10' },
    { label: 'A11', value: 'a11' },
]
const options2=[
    { label: 'B1', value: 'b1' },
    { label: 'B2', value: 'b2' },
    { label: 'B3', value: 'b3' },
    { label: 'B4', value: 'b4' },
    { label: 'B5', value: 'b5' },
    { label: 'B6', value: 'b6' },
]
const optionValue1 = options1.map(item=>{
    return item.value
})
const optionValue2 = options2.map(item=>{
    return item.value
})
class ExportForm extends Component{

    state={
        indeterminate1: false,
        indeterminate2: false,
        checkAll1: false,
        checkAll2: false,
    }
    componentDidMount(){
        const {
			form: { setFieldsValue, getFieldsValue }
		} = this.props;
        setFieldsValue({ "name1": [], "name2": [] });
        getFieldsValue();
    }
    render(){
        const { getFieldDecorator } = this.props.form

        return(
            
选择所有状态 {getFieldDecorator('name1', { rules: [{ required: false, message: '请选择', }], })( { options1 ? options1.map(item=>{ return( {item.label} ) }) : [] } )} 选择所有字段 {getFieldDecorator('name2', { rules: [{ required: false, message: '请选择', }], })( { options2 ? options2.map(item=>{ return( {item.label} ) }) : [] } )}
) } onChange1 = checkedList => { const { form: { setFieldsValue, getFieldsValue } } = this.props; if(checkedList.length){ setFieldsValue({ "name1": checkedList }); getFieldsValue(); this.setState({ indeterminate1: !!checkedList.length && checkedList.length < options1.length, checkAll1: checkedList.length === options1.length, }); } }; onCheckAllChange1 = e => { const { form: { setFieldsValue, getFieldsValue } } = this.props; setFieldsValue({ "name1": e.target.checked ? optionValue1 : [] }); getFieldsValue(); this.setState({ indeterminate1: false, checkAll1: e.target.checked, }); }; onChange2 = checkedList => { const { form: { setFieldsValue, getFieldsValue } } = this.props; if(checkedList.length){ setFieldsValue({ "name2": checkedList }); getFieldsValue(); this.setState({ indeterminate2: !!checkedList.length && checkedList.length < options2.length, checkAll2: checkedList.length === options2.length, }); } }; onCheckAllChange2 = e => { const { form: { setFieldsValue, getFieldsValue } } = this.props; setFieldsValue({ "name2": e.target.checked ? optionValue2 : [] }); getFieldsValue(); this.setState({ indeterminate2: false, checkAll2: e.target.checked, }); }; } export default Form.create()(ExportForm);

 

1. 不知为何,按照antd的案例写,多试几次正反选就出问题了,最终设置值没有用initialValue,用的setFieldsValue

你可能感兴趣的:(antd,antd,design,checkbox,form)