简体   繁体   English

提交表单时更改引导模式的内容

[英]Changing contents of Bootstrap modal upon form submission

I have created a navbar component which, when you click on a certain button, a modal box pops up.我创建了一个导航栏组件,当您单击某个按钮时,会弹出一个模式框。 Within this modal box is a form.在这个模态框中是一个表格。 When the form is completed (when all the required fields are filled in) and the user clicks the 'Add' button, I want the form to disappear from the modal box and for a message to appear instead saying 'Completion successful'.当表单完成(填写所有必填字段)并且用户单击“添加”按钮时,我希望表单从模式框中消失并显示一条消息,而不是显示“完成成功”。 I also wanted to show a React loading spinner for around 5 seconds before the completion shows.我还想在完成显示前大约 5 秒显示一个 React 加载微调器。 Here is my Modal component -这是我的模态组件-

function AddSystemModal(props: any) {
  return (
    <Modal
      {...props}
      size="lg"
      aria-labelledby="contained-modal-title-vcenter"
      centered
    >
      <Modal.Header closeButton>
        <Modal.Title id="contained-modal-title-vcenter">
          Add New System
        </Modal.Title>
      </Modal.Header>
      <Modal.Body>
        <SystemForm />
      </Modal.Body>
      <Modal.Footer></Modal.Footer>
    </Modal>
  );
}

export { AddSystemModal };

Here is my form component ( a bit messy ).这是我的表单组件(有点乱)。 At the moment I managed to get a spinner to appear when the 'Add' button is clicked but in terms of clearing the contents of the modal etc. I am stuck.目前,当单击“添加”按钮时,我设法使微调器出现,但就清除模式的内容等而言,我被卡住了。

function SystemForm() {
  const [validated, setValidated] = useState(false);

  const [showSpinner, setShowSpinner] = React.useState(false);
  const changeModal = () => setShowSpinner(true);

  const LoadingSpinner = () => (
    <div id="spinner" className="loading-spinner">
      <Spinner animation="border" role="status">
        <span className="sr-only">Loading...</span>
      </Spinner>
    </div>
  );

  const handleSubmit = (event) => {
    const form = event.currentTarget;
    if (form.checkValidity() === false) {
      event.preventDefault();
      event.stopPropagation();
    }
    setValidated(true);
  };

  return (
    <Form noValidate validated={validated} onSubmit={handleSubmit}>
      <Form.Row>
        <Form.Group as={Col} md="4" controlId="validationCustom01">
          <Form.Label>ID</Form.Label>
          <Form.Control
            required
            type="text"
            placeholder="Leave Blank For New System"
            defaultValue=""
          />
        </Form.Group>
        <Form.Group as={Col} md="4" controlId="validationCustom02">
          <Form.Label>Name</Form.Label>
          <Form.Control
            required
            type="text"
            placeholder="Name"
            defaultValue=""
          />
          <Form.Control.Feedback>Looks good!</Form.Control.Feedback>
        </Form.Group>
        <Form.Group controlId="exampleForm.SelectCustomSizeLg">
          <Form.Label>Allow Multiple Xref</Form.Label>
          <Form.Control as="select" custom required>
            <option>1</option>
            <option>2</option>
            <option>3</option>
            <option>4</option>
            <option>5</option>
          </Form.Control>
          <Form.Control.Feedback type="invalid">
            Please select an option.
          </Form.Control.Feedback>
        </Form.Group>
      </Form.Row>
      <Form.Row>
        <Form.Group controlId="exampleForm.SelectCustomSizeLg">
          <Form.Label>Status Code</Form.Label>
          <Form.Control as="select" custom required>
            <option>Available</option>
            <option>Blah</option>
            <option>Blah</option>
          </Form.Control>
          <Form.Control.Feedback type="invalid">
            Please provide a valid status code.
          </Form.Control.Feedback>
        </Form.Group>
        <Form.Group as={Col} md="3" controlId="validationCustom04">
          <Form.Label>Last Modified By</Form.Label>
          <Form.Control type="text" placeholder="" />
        </Form.Group>
      </Form.Row>
      <Button onClick={changeModal} type="submit">
        Add
      </Button>
      <div>{showSpinner ? <LoadingSpinner /> : null}</div>
    </Form>
  );
}

I have tried googling to see how I can clear the contents of something in React but haven't been able to find much.我尝试用谷歌搜索看看如何清除 React 中某些内容的内容,但找不到太多内容。 I am wondering if this is due to how I have arranged the component and whether I need to have two separate components for inside the modal and conditionally render them depending on if the user has clicked 'Add'.我想知道这是否是由于我如何安排组件以及是否需要在模式内部有两个单独的组件并根据用户是否单击“添加”有条件地呈现它们。

Apologies if this is a bad question - I am new to learning React!抱歉,如果这是一个不好的问题 - 我是学习 React 的新手!

This is how I would do it...这就是我会做的...

  • Have the formSubmission flag in the parent which is triggered when form is submitted inside the modal - this will ensure that on repeated modal display, the form isn't showed在模态中提交表单时触发父级中的 formSubmission 标志 - 这将确保在重复模态显示时,不会显示表单
  • Have a buttonClicked flag inside the modal to know when to show/hide the spinner在模态框内有一个 buttonClicked 标志,以了解何时显示/隐藏微调器
  • I trust you to manage the exact field, design etc. the demo shows the steps where I think you're stuck我相信你可以管理确切的领域、设计等。演示显示了我认为你被卡住的步骤

relevant parent component:相关组件:

import React, { useState } from "react";
import ModalExample from "./modal";
import Child from "./child";
import ModalExample from "./modal";

const MyNav = () => {
  const [formSubmitted, setFormSubmitted] = useState(false);

  const handleClick = () => {
    console.log('inside hooks handleClick');
    setFormSubmitted(true);
  }

  return (
    <>
      <nav>
        Navigation
        <ModalExample
          buttonLabel="click Me to fire the modal inside navigation"
          myName={'name'}
          formSubmissionStatus={formSubmitted}
          handleClick={handleClick}
        />
      </nav>
    </>
  );
};

export default MyNav;

relevant Modal component:相关的模态组件:

import React, { useState } from "react";
import { Button, Modal, ModalHeader, ModalBody, ModalFooter } from "reactstrap";
import { Form, FormGroup, Label, Input, FormText } from "reactstrap";

const ModalExample = props => {
  const { buttonLabel, className, myName, formSubmissionStatus } = props;

  const [modal, setModal] = useState(false);
  const [spinner, setSpinner] = useState(false);
  const [buttonClicked, setButtonClicked] = useState(false);

  const toggle = () => setModal(!modal);

  const ModalClicked = () => {
    console.log("inside ModalClicked");
    setButtonClicked(true);
    setTimeout(() => {
      setSpinner(false);
      props.handleClick();
    }, 1500);
  };

  return (
    <div>
      <Button color="danger" onClick={toggle}>
        {buttonLabel}
      </Button>
      <Modal isOpen={modal} toggle={toggle} className={className}>
        <ModalHeader toggle={toggle}>Modal title</ModalHeader>
        <ModalBody>
          {formSubmissionStatus ? (
            <>
              <p>Form is submitted</p>
            </>
          ) : buttonClicked ? (
            <img src="https://i.ya-webdesign.com/images/loading-png-gif.gif" />
          ) : (
            <Form>
              <FormGroup>
                <Label for="exampleEmail">Email</Label>
                <Input
                  type="email"
                  name="email"
                  id="exampleEmail"
                  placeholder="with a placeholder"
                />
              </FormGroup>
              <FormGroup>
                <Label for="examplePassword">Password</Label>
                <Input
                  type="password"
                  name="password"
                  id="examplePassword"
                  placeholder="password placeholder"
                />
              </FormGroup>
            </Form>
          )}
        </ModalBody>
        <ModalFooter>
          <button onClick={ModalClicked} type="button">
            {myName}
          </button>
        </ModalFooter>
      </Modal>
    </div>
  );
};

export default ModalExample;

complete working stackblitz here在这里完成工作堆栈闪电战

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM