react antd Form.List Form.Iterm Checkbox.Group的案例

邮件订阅交易报表流水

需求功能

在这里插入图片描述

关键代码

import React from "react";
import { inject, observer } from "mobx-react";
import { withRouter } from "react-router-dom";
import {Input, Button, Checkbox, Form, Skeleton} from "antd";
import addIcon from 'images/icon/add.png';
import subIcon from 'images/icon/reduce.png';

@inject(state => ({
    history: state.history,
    subscribeInfo: state.store.subscribe.subscribeInfo,
    getSubscribeInfo: state.store.subscribe.getSubscribeInfo,
    setSubscribeInfo: state.store.subscribe.setSubscribeInfo,
}))

@withRouter
@observer
class Report extends React.Component {
    formRef = React.createRef();
    state = {
        cycleOptions: [
            { label: '全部', value: 'all' },
            { label: '日报', value: 'day' },
            { label: '周报', value: 'week' },
            { label: '月报', value: 'month' },
        ],
        cycleRules: [
            {
                required: true,
                message: '请勾选交易报告纬度'
            }
        ],
        mailRules: [
            {
                required: true,
                message: '请输入邮箱地址'
            },
            {
                pattern: /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/,
                message: '邮箱地址不正确'
            }
        ],
        originReportCycle: [],
        formData: {
            toMails:[null],
            toCcMails:[null],
            reportCycle: [],
        },
        formLoading: true,
    };

    componentDidMount () {
        this.props.getSubscribeInfo( () => {
            let reportInfo = this.props.subscribeInfo;
            let initCycle = this.initReportCycle(reportInfo.reportCycle);
            this.setState({
                formData: {
                	//为空时需有首元素,否则页面不会渲染,故而为空赋值为[null]
                	// toMails: reportInfo.toMails.length > 0 ? reportInfo.toMails : [null],
                	//  toCcMails: reportInfo.toCcMails.length > 0  ? reportInfo.toCcMails : [null],
                	// 此种写法有BUG,当把接口数据设置到表单,编辑Form.List的原有其中任意一项数据,此时其他原有数据项直接置空。
                	// 请修改为如下写法,因为ObservableArray 与 Array 是有区别的,Form.list 期待的是Array。
                    toMails: reportInfo.toMails.length > 0 ? [...reportInfo.toMails] : [null],
                    toCcMails: reportInfo.toCcMails.length > 0  ? [...reportInfo.toCcMails] : [null],
                    reportCycle: initCycle,
                },
                formLoading: false,
            }, () => {
                this.onFill();
            });
        });
    }

    initReportCycle(reportCycle = []) {
        let allCycle = this.state.cycleOptions.map(item => item.value);
        if (reportCycle.includes('all') || ( !reportCycle.includes('all') &&  allCycle.length -1 === reportCycle.length ) ) {
            return  allCycle;
        }

        return reportCycle;
    }


    // checkout.group 修改值 里边的变更逻辑
    // 利用每次只会变更一个值的特点
    onChangeCycle = (currentCycle) => {
        let allCycle = this.state.cycleOptions.map(item => item.value);
        let originReportCycle = this.state.formData.reportCycle;

        // console.log('allCycle', allCycle, 'originReportCycle', this.state.originReportCycle, 'currentCycle', currentCycle);
        if (originReportCycle.length > currentCycle.length) { //减少勾选
        	//包含all,说明取消勾选非全选是其他选项,此时把全选去除
            if (currentCycle.includes('all')) { 
                currentCycle.splice(currentCycle.indexOf('all'), 1);
            }
  			//不包含all且剩余其他项,说明取消勾选的是全选,此时全部反选
            if (! currentCycle.includes('all') && currentCycle.length === allCycle.length - 1 ) { //包含all
                currentCycle = [];
            }

        } else { //增加勾选
        	//包含all,说明新增勾选的是全选,此时需选中全部选项
            if (currentCycle.includes('all')) {
                currentCycle = allCycle;
            }
			//不包含all且其他选项全部选中,此时需选中全部
            if (! currentCycle.includes('all') && currentCycle.length === allCycle.length - 1 ) {
                currentCycle = allCycle;
            }
        }
        
        //记录原始值
        this.setState({
            originReportCycle: currentCycle,
        }, () => {
        	this.onFill();
        });

    }

    goBack() {
        this.props.history.push('/yourPageName');
    }

    onCancel = () => {
        this.goBack();
    }

    onFinish = (values) => {
        let data = {
            reportCycle: values.reportCycle,
            toMails:values.toMails,
            toCcMails :values.toCcMails,
        }

        this.props.setSubscribeInfo(data, () => {
            this.goBack();
        });
    }


    onFill = () => {
        this.formRef.current.setFieldsValue({...this.state.formData});
    }

    render() {
        let {
            cycleOptions,
            cycleRules,
            mailRules,
            formLoading,
        } = this.state;

        return (
            <div className="trade-flow-report">
                <div className="title">
                    <span>设置交易流水报告邮件接收人</span>
                </div>
                {
                    formLoading
                        ? <Skeleton active={ true } title={ false } paragraph={{ rows: 15 }}/>
                        : <Form ref={this.formRef}  onFinish={ this.onFinish} scrollToFirstError>
                            <div className="content">
                                <div className="item-row">
                                    <div className="item-label">交易报告维度</div>
                                    <div className="item-info">
                                        <Form.Item name="reportCycle" rules={cycleRules}>
                                            <Checkbox.Group
                                                options={cycleOptions}
                                                onChange={this.onChangeCycle}
                                            />
                                        </Form.Item>
                                    </div>
                                </div>
                                <div className="item-row">
                                    <div className="item-label">报告收件人</div>
                                    <div className="item-info">
                                        <Form.List name="toMails">
                                            {
                                                ( fields, { add , remove } ) => {
                                                    return (
                                                        <div className="mails-list">
                                                            {
                                                                fields.map( (field, index) => (
                                                                    <span className="mails-box" key={field.key}>
                                                                <Form.Item {...field} validateTrigger={['onChange', 'onBlur']} rules={mailRules} className="user-mail">
                                                                    <Input placeholder="请输入邮箱地址" className="common-input" value={field.value}/>
                                                                 </Form.Item>
                                                                <span><img src={addIcon} alt="添加" onClick={() => add()}/></span>
                                                                <span>{index === 0 ? '' : <img src={subIcon} alt="删除" onClick={() => remove(field.name)}/>}</span>
                                                            </span>
                                                                ))
                                                            }
                                                        </div>
                                                    );
                                                }
                                            }
                                        </Form.List>
                                    </div>
                                </div>
                                <div className="item-row">
                                    <div className="item-label">报告抄送人</div>
                                    <div className="item-info">
                                        <Form.List name="toCcMails">
                                            {
                                                ( fields, { add , remove } ) => {
                                                    return (
                                                        <div className="mails-list">
                                                            {
                                                                fields.map( (field, index) => (
                                                                    <span className="mails-box" key={field.key}>
                                                                <Form.Item {...field} validateTrigger={['onChange', 'onBlur']} rules={mailRules} className="user-mail">
                                                                    <Input placeholder="请输入邮箱地址" className="common-input" value={field.value}/>
                                                                 </Form.Item>
                                                                <span><img src={addIcon} alt="添加" onClick={() => add()}/></span>
                                                                <span>{index === 0 ? '' : <img src={subIcon} alt="删除" onClick={() => remove(field.name)}/>}</span>
                                                            </span>
                                                                ))
                                                            }
                                                        </div>
                                                    );
                                                }
                                            }
                                        </Form.List>
                                    </div>
                                </div>
                            </div>
                            <Form.Item className="operate-box">
                                <Button className="operate-btn" onClick={this.onCancel }>取消</Button>
                                <Button  className="operate-btn" type="primary" htmlType="submit">提交</Button>
                            </Form.Item>
                        </Form>
                }
            </div>
        )
    }
}

export default Report
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值