0

Hi all I have following code:

    const BasicInformation = () => {
    const { Panel } = Collapse;

    const dataFromBackend = [
    { id: 2, depositeAmt: '500' },
    { id: 3, depositeAmt: '1500' },
    ];

    const totalCount = dataFromBackend
    .map((item) => item.depositeAmt)
    .reduce((prev, curr) => +prev + +curr, 0);

    const onSubmit = (data) => {
    console.log(data);
    };
    return (
    <Form onFinish={(e) => onSubmit(e)}>
      <Form.List name="users" initialValue={dataFromBackend}>
        {(fields, { add }) => {
          return (
            <>
              total Deposit Amount: $ {totalCount}
              <Button
                onClick={() => {
                  add();
                }}
                block
              >
                Add
              </Button>
              {fields.map((field, i) => (
                <Collapse
                  key={i}
                  accordion
                >
                  <Panel header="Amount : ">
                    <Form.Item
                      name={[field.name, 'depositeAmt']}
                      label="Amount"
                      fieldKey={[field.fieldKey, 'depositeAmt']}
                    >
                      <Input />
                    </Form.Item>
                  </Panel>
                </Collapse>
              ))}
            </>
          );
        }}
      </Form.List>
      <Button type="primary" htmlType="submit">
        send
      </Button>
      </Form>
    );
   };

I am receiving some date from backend dataFromBackend and reducing to get count of all deposit Amount.

Also in collapses I have deposit label. The idea is when user changing deposit field value the total Deposit Amount: and <Panel header="Amount : "> should also dynamically changed.

I try this but it not help me to resolve it:

     <Form.Item
       name={[field.name, 'depositeAmt']}
       label="Amount"
       fieldKey={[field.fieldKey, 'depositeAmt']}
       >
        <Input onChange={(e)=>totalCount + e} />
      </Form.Item>

Please help me to fix this problem, thank you very much.

1
  • What happened in add() function Commented Jan 24, 2022 at 11:34

1 Answer 1

1

You store the dataFromBackend in variable thats the issue.! You need to store dataFromBackend in state and on input (handleInputChange) change you need to update this array !

On Add btn click you need to push new object in dataFromBackend array

Show this code it's help you !

const BasicInformation = () => {
  const { Panel } = Collapse;

  const [dataFromBackend, setDataFromBackend] = useState([
    { id: 2, depositeAmt: '500' },
    { id: 3, depositeAmt: '1500' },
  ]);

  const [totalCount, setTotalCount] = useState(
    dataFromBackend
      .map((item) => item.depositeAmt)
      .reduce((prev, curr) => +prev + +curr, 0)
  );

  const onSubmit = (data) => {
    console.log(data);
  };

  const handleInputChange = (index, value) => {
    let arr = [...dataFromBackend];
    arr[index].depositeAmt = value;
    setDataFromBackend(arr);
  };

  useEffect(() => {
    let count = dataFromBackend
      .map((item) => item.depositeAmt)
      .reduce((prev, curr) => +prev + +curr, 0);

    setTotalCount(count);
  }, [dataFromBackend]);

  const addNewField = () => {
    let id = new Date().getTime();
   setDataFromBackend(oldArray => [...oldArray, { id: id, depositeAmt: 0 }]);
  };

  return (
    <Form onFinish={(e) => onSubmit(e)}>
      <Form.List name="users" initialValue={dataFromBackend}>
        {(fields, { add }) => {
          return (
            <>
              total Deposit Amount: $ {totalCount}
              <Button
                type="dashed"
                onClick={() => {
                  add();
                  addNewField();
                }}
                block
              >
                Add
              </Button>
              {fields.map((field, i) => (
                <Collapse
                  key={i}
                  accordion
                  style={{
                    background: 'rgba(25, 103, 210, 0.08)',
                    border: 'none',
                  }}
                >
                  <Panel style={{ border: 'none' }} header="Amount :">
                    <Form.Item
                      name={[field.name, 'depositeAmt']}
                      label="Amount"
                      fieldKey={[field.fieldKey, 'depositeAmt']}
                    >
                      <Input
                        type="number"
                        onChange={(e) => handleInputChange(i, e.target.value)}
                      />
                    </Form.Item>
                  </Panel>
                </Collapse>
              ))}
            </>
          );
        }}
      </Form.List>
      <Button type="primary" htmlType="submit">
        send
      </Button>
    </Form>
  );
};

ReactDOM.render(<BasicInformation />, document.getElementById('container'));
Sign up to request clarification or add additional context in comments.

3 Comments

thanks @Mayur Vaghasiya, I am trying to fix on new collapse when adding the function is not working, can you please look? stackblitz.com/edit/react-2djy5g-yywt6r
yes I try to fix that !
I update my answer check !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.