ChildForm.jsx 6.8 KB
Newer Older
徐立's avatar
徐立 committed
1
import React from 'react'
wanyielin's avatar
wanyielin committed
2
import { Button, Row, Col, message } from 'antd';
徐立's avatar
徐立 committed
3 4 5
import ZdyTable from '../Table'
import UUID from 'react-native-uuid';

wanyielin's avatar
wanyielin committed
6
import { Card, WingBlank } from 'antd-mobile';
徐立's avatar
徐立 committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121


export default class ChildForm extends React.Component {

    constructor(props) {
        super(props)
        const value = props.value || {};

        this.state = value

    }
    triggerChange = (changedValue) => {
        // Should provide an event to pass value to Form.
        const onChange = this.props.onChange;
        if (onChange) {
            onChange(Object.assign({}, this.state, changedValue));
        }
    }
    componentWillReceiveProps(nextProps) {
        // Should be a controlled component.
        if ('value' in nextProps) {
            const value = nextProps.value;
            this.setState(value);
        }
    }
    componentDidMount = () => {
        if (Object.keys(this.state).length == 0 && this.props.isEdit) {
            if (this.props.num != null && this.props.num > 0) {
                const objs = this.state
                if (this.props.min != null && this.props.min > this.props.num) {
                    for (var i = 0; i < this.props.min; i++) {
                        objs["id_" + UUID.v4().replace(/-/g, "2")] = {}
                    }
                } else {
                    for (var i = 0; i < this.props.num; i++) {
                        objs["id_" + UUID.v4().replace(/-/g, "2")] = {}
                    }
                }

                if (!('value' in this.props)) {
                    this.setState({ ...objs });
                }
                this.triggerChange({ ...objs });


            }
        }
    }
    delete = (uuid) => {
        const objs = this.state

        if (this.props.min != null && Object.keys(objs).length - 1 < this.props.min) {
            message.error("不能小于最小限制")

            return
        }

        delete objs[uuid]
        if (!('value' in this.props)) {
            this.setState({ ...objs });
        }
        this.triggerChange({ ...objs });



    }
    add = () => {
        const objs = this.state
        console.log(objs)
        if (this.props.max != null && Object.keys(objs).length + 1 > this.props.max) {
            message.error("不能大于最大限制")

            return
        }
        objs["id_" + UUID.v4().replace(/-/g, "2")] = {}

        if (!('value' in this.props)) {
            this.setState({ ...objs });
        }
        this.triggerChange({ ...objs });



    }




    render() {
        const objs = this.state;

        const { form, mapData, sqlData, defaultValues, datas, base52, isEdit, addName, deleteName, obj, isMobile, json } = this.props
        const span = json.span || 24
        const gutter = json.gutter != null ? json.gutter : 0
        const rights = this.props.rights
        if (datas == null) {
            return (<div>还没有配置子表单key</div>)
        }

        if (isMobile) {

            return (
                <WingBlank size="lg">
                    {Object.keys(objs).length > 1 || rights.includes("add") ? <Card style={{ marginLeft: '-27px' }}>
                        {rights.includes("add") ?
                            !isEdit ? '' : (<Card.Header title={<span style={{ fontSize: 14, color: '#333333', fontWeight: 'bold' }}></span>}
                                extra={rights.includes("add") ? <Button style={{ margin: "auto" }} onClick={this.add}>{addName || "新增"}</Button> : ""}
                            />) : ""
                        }
                        <Card.Body style={{ minHeight: 10 }}>
                            {Object.keys(objs).map((r) => {
                                if (r == "") {
                                    return ""
                                }
                                return <Row key={r} gutter={gutter} ><Col style={{ textAlign: 'right' }} span={isEdit ? 23 : span} >
chscls@163.com's avatar
chscls@163.com committed
122
                                    <ZdyTable key={r} formConfig={datas} get='mobile' fatherCode={base52} isEdit={isEdit} index={r} obj={objs[r]} fatherObj={this.props.fatherObj} init={objs} isChild={true} form={form} mapData={mapData} sqlData={sqlData} {...datas} defaultValues={defaultValues} />
徐立's avatar
徐立 committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
                                    {rights.includes("delete") && isEdit ? <Button type='danger' onClick={this.delete.bind(this, r)}>{deleteName || "删除"} </Button> : ''}
                                </Col>
                                </Row>
                            })}
                        </Card.Body>
                    </Card> : ""}
                </WingBlank>
            )
        }

        return (
            <div style={{ width: "100%" }}>
                <Row gutter={gutter}>
                    {Object.keys(objs).map((r) => {
                        if (r == "") {
                            return ""
                        }

                        return <Col
                            span={span}
                        >

wanyielin's avatar
wanyielin committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
                            <ZdyTable
                                formConfig={datas}
                                key={r}
                                fatherCode={base52}
                                isEdit={isEdit}
                                index={r}
                                obj={objs[r]}
                                fatherObj={this.props.fatherObj}
                                init={objs} isChild={true}
                                get="web"
                                form={form}
                                mapData={mapData}
                                sqlData={sqlData}
                                {...datas}
                                defaultValues={defaultValues} />
徐立's avatar
徐立 committed
160 161

                            {rights.includes("delete") && isEdit ? <Button size="small"
wanyielin's avatar
wanyielin committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175
                                style={{
                                    position: "absolute",
                                    left: ' 95%',
                                    top: '10px',
                                    transform: 'translateY(-50%)',
                                    borderRadius: '50%',
                                    height: '13px',
                                    width: '13px',
                                    padding: 0,
                                    lineHeight: '10px'
                                }}
                                type='danger'
                                onClick={this.delete.bind(this, r)}>{deleteName || "-"}
                            </Button> : ""}
徐立's avatar
徐立 committed
176 177 178 179 180 181 182 183 184 185 186

                        </Col>

                    })}
                </Row>
                {rights.includes("add") && isEdit ? <Row><Col span={24} style={{ textAlign: "center" }}><Button style={{ margin: "auto" }} type='primary' onClick={this.add} size="small">{addName || "新增"}</Button></Col> </Row> : ""}
            </div>

        );
    }
}