Gabriel Robaina
Gabriel Robaina

Reputation: 741

How to get children values on antd's form submit?

I made a custom component called TagsInput, and added it to a form. My problem is, when the form is submitted, the value for field tags is always undefined, because the Form.Item cannot retrieve the value from the TagsInput child. This is usually not a problem when children of Form.Item are antd components.

export default function NewProjectForm(props) {
  return (
    <Form layout="vertical" onFinish={props.onSubmit} id="newProjectForm">   
      <Form.Item label="Tags" name="tags">
        <TagsInput />
      </Form.Item>
    </Form>
  );
}

TagsInput is a class component that has its value within its state:

class TagsInput extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            value: []
        }
    }

    render(){
        return(<div></div>)
    }
}

I dont want to elevate the value field from TagsInput state to NewProjectForm because it is not a class component... I would be doing it just for the sake of solving this issue. I am asking this question because I think there is a cleaner way of solving this.

How can I make NewProjectForm retrieve the value of TagsInput from within its state? Is there any function I can pass to TagsInput as a property, so that the form asks the component for a value when it is submitted?

Upvotes: 2

Views: 4766

Answers (1)

SomoKRoceS
SomoKRoceS

Reputation: 3053

In order to achive what you want, you need to handle the state of the form (and its components) at the parent component (NewProjectForm).

If you are willing to keep that component a function component, you can use React Hooks and define a state with useState like this:

const [tagValue, setTagValue] = useState([]);

While tagValue is the value you will get from the child TagsInput, and the setTagValue is the function that can set that value. You need to pass setTagValue as a function prop to TagsInput and use it on change. It will look something like this:

export default function NewProjectForm(props) {

  const [tagValue, setTagValue] = useState([]);

  return (
    <Form layout="vertical" onFinish={props.onSubmit} id="newProjectForm">   
      <Form.Item label="Tags" name="tags">
        <TagsInput setTagValue={(val)=>setTagValue(val)} />
      </Form.Item>
    </Form>
  );
}

class TagsInput extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            value: []
        }
    }

    ......... 
    ......... 
    onChangeInput(val){
        this.props.setTagValue(val);
    }
    ......... 
    ......... 

    render(){
        return(<div></div>)
    }
}

EDIT

After the clarification in the comments, you actually need to follow the steps of using Customized Form Controls, As described here.

You need to pass an object with value and onChange that changes the value. Then, the Form component should manage to get that value automatically.

So I guess it should look something like that:

const TagsInput= ({ value = {}, onChange }) => {

  const triggerChange = newValue => {
    if (onChange) {
      onChange(newValue);
    }
  };

  return(<div></div>)

};

Upvotes: 1

Related Questions