20

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>
1
  • if you want to do it the react way, you should store the input values somewhere(e.g state,redux-form) so you can get them later. Commented May 19, 2017 at 5:35

3 Answers 3

35

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'  />
Sign up to request clarification or add additional context in comments.

7 Comments

Nice elegant solution +1
I had to set button type='submit' for this to work, nice find!
Best solution IMO
The owner should accept this answer as the right one. Thanks Pavlo!
I was wondering: what is the 'positive' attribute in the Button component ?
|
7

What does your makeActivityInfoUpdateHandler function look like?

I assume you did it by the following way, and just continue adding more code to make it work for you:

1/ Add ref to your Form, then you can access the Form in the parent (Modal):

<Modal>

    <Modal.Content>
        <ActivityInfoForm ref="activityForm" />
    </Modal.Content>

</Modal>

2/ Then in the makeActivityInfoUpdateHandler function:

makeActivityInfoUpdateHandler = (activityId) => {
    // ...
    this.refs.activityForm.getWrappedInstance().submit();
    // ...

}

The above code is the way you should do, please post here some more details in case this doesn't work yet!

=========== EDITED VERSION BELOW: (after discussion with the author, and we together found a good way around!):

The idea now is put the ref on a button (this button has type="submit", and it belongs to the form), then when the button outside is clicked, we just need to call the "click()" function of the ref button [which is a smart thinking from the author himself]

(Actually, component from semantic-ui is a modified and improved version, no longer the standard form, so my previous way above may not work when it tries to submit the form, however, the below way will work)

The code will now look like:

1/ Add ref to the button on the form:

<Form onSubmit={ this.handleSubmit} >
    <button style={{}} type='submit' ref={ (button) => { this.activityFormButton = button } } >Submit</button>
</Form>

2/ Then in the makeActivityInfoUpdateHandler function, trigger click() of the above button:

makeActivityInfoUpdateHandler = (activityId) => {
    // ...
    this.activityFormButton.click();
    // ...

}

5 Comments

That function is really just a skeleton function awaiting to be filled.
that does not seem to be working, does not give me any errors either, they <Form> is actually a component in Semantic UI React can be found here react.semantic-ui.com/collections/form#form-example-form
Seems so..actually I guess Semantic UI React handle their form differently, I will try another way and update the previous answer shortly, please hold on
However, which action will be executed when the form is submitted? Do you have handleSubmit() function for your Form component? If yes, can you console.log and see some values in your submitted form?
2

The selected answer was useful. But the method in it doesn't seem to work any longer. Here's how I went about it.

You can give a ref to the child component when it is being created.

<ChildComponent ref={this.childComponent}/>

And use it in the button's onClick method. (This is the code for the button)

<Button variant= "light" onClick={this.onClick}>  
    Submit
</Button>

(This is the onClick method)

onClick = (event) => {
    this.childComponent.current.handleSubmit(event);
}

I'm calling a method in the child component called handleSubmit. It can look like this.

handleSubmit = (event)=> {
    event.preventDefault();

    //Your code here. For example,
    alert("Form submitted");  
}

1 Comment

That's smart but I add a detail : Don't forget in constructor this.childComponent = React.createRef(); if you use a class component.

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.