How to implement a dynamic form with controlled components in ReactJS?

后端 未结 4 1575
野性不改
野性不改 2020-11-29 22:18

As I am looking at the examples in the reference for controlled form components in react.js official website, I am wondering how is one supposed to implement a

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-29 23:09

    import React, { Component, } from 'react';
    import { BrowserRouter as Router, Route, Link } from "react-router-dom";
    var childJson = []
    export default class AddInvoice extends Component {
    constructor(props) {
        super(props);
        this.state = {
            Json: [],
            rows: [{}]
        }
    }
    
    handleChange = idx => e => {
        const { name, value } = e.target;
        const rows = [...this.state.rows];
        rows[idx] = { [name]: value };
        this.setState({ rows });
    
        console.log(`rows-->>${this.state.rows[idx].amount}`);
        childJson.push(this.dynamicJson(this.state.rows, idx))
        this.setState({ Json: childJson })
    
    };
    handleAddRow = () => {
        const item = {
            name: "",
            mobile: "",
            btn: ""
        };
        this.setState({
            rows: [...this.state.rows, item]
        });
    };
    handleRemoveRow = (idx) => {
        this.state.rows.splice(idx, 1);
        this.setState({ rows: this.state.rows });
    };
    dynamicJson(rows, index) {
        return {
            "service": rows[index].text,
            "tax": rows[index].tax,
            "amount": rows[index].amount
        }
    };
    render() {
        return (
            

    INVOICE-ADD NEW

    this.setState({ invoiceno: e.target.value })} />
    this.setState({ invoicedate: e.target.value })} />
    this.setState({ duedate: e.target.value })} />
    this.setState({ header: e.target.value })} />
    this.setState({ remark: e.target.value })} />

    Action

    Text

    Tax

    Amount

    {this.state.rows.map((item, idx) => (
    this.handleRemoveRow(idx)}>
    ))}
    ); }

    }

提交回复
热议问题