Sunday 15 November 2020

Retrieving data from state and displaying it in dynamic form in antd

In this line {({languages}, {add, remove}) => .... I am trying to put languages from state. But I have a problem referencing the state. The message .map is not a function appears. After pressing Add another languages, more inputs should appear with the possibility of choosing another language. I use library antd.

Code here: https://stackblitz.com/edit/react-ycty22

import React from "react";
import React, {useState} from 'react';
import {
  Form,
  Button,
  Select,
  Space 
} from 'antd';
import { MinusCircleOutlined, PlusOutlined } from '@ant-design/icons';
import 'antd/dist/antd.css';


const { Option } = Select;

const App = () => {

    const [values, setValues] = useState({
        birthCountries: ['England', 'Germany', 'France'],
        birthCountry: '',
        languages: [{name: "German", level: "B1"}]
    });

    const {languages, birthCountries} = values;

    
    const Languages = () => {
        const onFinish = values => {
        console.log('Received values of form:', values);
    };


    return (
      
      <Form name="dynamic_form_nest_item" onFinish={onFinish} autoComplete="off">
        <Form.List name="users">
          {({languages}, { add, remove }) => { //{languages} in this place I want to put languages 
                                               //from state
            return (
              <div>
                {languages.map(language => (
                  <Space key={language.name} style= align="start">
                    <Form.Item
                      label="Language"
                      {...language}
                      name={[language.name, 'first']}
                    >
                      <Select defaultValue={language.name} onChange={}>
                        {birthCountries.map((birthCountry, index) => {
                          return <Option value={birthCountry} key={index}>{birthCountry}</Option>
                        })}
                      </Select>
                    </Form.Item>
                    <Form.Item
                      label="Level"
                      {...language}
                      name={[language.level, 'last']}
                    >
                      <Select defaultValue={language.level} onChange={}>
                        {birthCountries.map((birthCountry, index) => {
                          return <Option value={birthCountry} key={index}>{birthCountry}</Option>
                        })}
                      </Select>
                    </Form.Item>
  
                    <MinusCircleOutlined
                      onClick={() => {
                        remove(language.name);
                      }}
                    />
                  </Space>
                ))}
  
                <Form.Item>
                  <Button
                    type="dashed"
                    onClick={() => {
                      add();
                    }}
                    block
                  >
                    <PlusOutlined /> Add another Language
                  </Button>
                </Form.Item>
              </div>
            );
          }}
        </Form.List>
      </Form>
    );
  };

    return (
        <div>
      {Languages()}
    </div>
    );
};

export default App;


from Retrieving data from state and displaying it in dynamic form in antd

No comments:

Post a Comment