Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submit Form using Button in Parent Component in React

form modal

So I have to implement a form in modal, as you can see, the button in the modal are not the buttons in the form. I created the form as a child component of the modal. How can I submit the form using the button in the parent component. I am using React Semantic-UI react as my UI framework.

I think if I can hide the button in the form and trigger it using JavaScript. I think this might be achieved via getElementById, but is there a react way of doing it?

My current Modal looks like this:

<Modal open={this.props.open} onClose={this.props.onClose} size="small" dimmer={"blurring"}>
    <Modal.Header> Edit Activity {this.props.name} </Modal.Header>
    <Modal.Content>
      <ActivityInfoForm/>
    </Modal.Content>
    <Modal.Actions>
        <Button negative onClick={this.props.onClose}>
            Cancel
        </Button>
        <Button positive
                content='Submit'
                onClick={this.makeActivityInfoUpdateHandler(this.props.activityId)} />
    </Modal.Actions>
</Modal>

My form code looks like this:

<Form>
    <Form.Group widths='equal'>
        <Form.Input label='Activity Name' placeholder='eg. CIS 422' />
        <Form.Input label='Activity End Date' placeholder='Pick a Date' />
    </Form.Group>
    <Form.Group widths='equal'>
        <Form.Input label='Total Capacity' placeholder='eg. 30' />
        <Form.Input label='Team Capacity' placeholder='eg. 3' />
    </Form.Group>
</Form>
like image 971
Rui Avatar asked May 19 '17 05:05

Rui


People also ask

How do I submit a parent form in React?

In the parent you should create a ref value in the constructor. In the render method when you create the child you have to define the ref value. And the button has a onclick method. In the click method you call the function you wrote in the child component.

How do you submit a child component form from the parent component in React?

Just create a ref in the parent component, send it to the child component and assign that ref to an invisible submit button.

How do you implement submit button in React?

To submit a form using the Enter key in React: Add a button element with type set to submit . Add an onSubmit prop that points to a submit handler function. Every time the user presses Enter, the handle submit function will be invoked.

How do you submit form from a button outside that component in React?

You can pass the onSubmit click handler as a props as follows: import React, { Component } from "react"; import ReactDOM from "react-dom"; class CustomForm extends Component { render() { return ( <form onSubmit={this. props.


1 Answers

The simplest solution would be to use HTML form Attribute

Add "id" attribute to your form: id='my-form'

<Form id='my-form'>
                    <Form.Group widths='equal'>
                        <Form.Input label='Activity Name' placeholder='eg. CIS 422' />
                        <Form.Input label='Activity End Date' placeholder='Pick a Date' />
                    </Form.Group>
                    <Form.Group widths='equal'>
                        <Form.Input label='Total Capacity' placeholder='eg. 30' />
                        <Form.Input label='Team Capacity' placeholder='eg. 3' />
                    </Form.Group>
                </Form>

Add the appropriate "form" attribute to the needed button outside of the form: form='my-form'

<Button positive form='my-form' content='Submit' value='Submit'  />
like image 135
Pavlo Kyrylenko Avatar answered Sep 21 '22 23:09

Pavlo Kyrylenko