简体   繁体   English

如何在 React-Bootstrap 中处理多个模态?

[英]How to handle multiple modals in React-Bootstrap?

I'm building a CRUD app using React-Bootstrap and TypeScript .我正在使用React-BootstrapTypeScript构建一个 CRUD 应用程序。 My Users page is supposed to have two modals for the Create User and Edit User buttons.我的Users页面应该有两个用于Create UserEdit User按钮的模式。 I have coded the edit button to open the Edit Modal , and it's working fine.我已经对编辑按钮进行了编码以打开Edit Modal ,并且工作正常。 I tried to use the same approach to open the Create Modal , but it opens the same edit modal instead of the create modal.我尝试使用相同的方法打开Create Modal ,但它打开的是相同的编辑模式而不是创建模式。

Can someone show me how I should change my code to make the Create User button work?有人可以告诉我应该如何更改我的代码以使“ Create User ”按钮起作用吗?

Here's my code.这是我的代码。

Users.tsx用户.tsx

const Users = (props: Props) => {
  const [userList, setUserList] = useState<IList[]>([]);

  const [show, setShow] = useState(false);
  const [selected, setSelected] = useState(Object);
  const [updatedItem, setUpdatedItem] = useState();

  const getUsers = async () => {...};

  useEffect(() => {...}, [updatedItem]);

  const handleShow = () => setShow(true);
  const closeHandler = () => setShow(false);
  const userHandler = async (user: any) => {...};
  const updateHandler = async (user: any) => {...};
  const handleEdit = (selected: any) => {...};

  const createModal = (
    <>
      <Modal show={show} onHide={closeHandler} className="modal">
        <Modal.Header closeButton>
          <Modal.Title>Create User</Modal.Title>
        </Modal.Header>
        <Modal.Body>
          <UserForm
            userHandler={userHandler}
            closeHandler={closeHandler}
            updateHandler={updateHandler}
            formUpdate={false}
            userList={""}
          />
        </Modal.Body>
      </Modal>
    </>
  );

  const editModal = (
    <>
      <Modal show={show} onHide={closeHandler} className="modal">
        <Modal.Header closeButton>
          <Modal.Title>Edit User</Modal.Title>
        </Modal.Header>
        <Modal.Body>
          <UserForm
            userHandler={userHandler}
            closeHandler={closeHandler}
            updateHandler={updateHandler}
            formUpdate={true}
            userList={selected}
          />
        </Modal.Body>
      </Modal>
    </>
  );

  return (
    <div>
      {/* <UserForm
        userHandler={userHandler}
        closeHandler={closeHandler}
        updateHandler={updateHandler}
        userList={""}
      /> */}

      <Button variant="danger" type="button" onClick={handleShow}>
        Create User
      </Button>

      {createModal}

      {userList.length > 0 ? (
        <UserTable
          deleteHandler={deleteHandler}
          handleEdit={handleEdit}
          userHandler={userHandler}
          closeHandler={closeHandler}
          updateHandler={updateHandler}
          userList={userList}
        />
      ) : (
        ""
      )}

      {editModal}
    </div>
  );
};

export default Users;

UserForm.tsx用户窗体.tsx

type Props = {
  userHandler: (user: object) => void;
  userList: any;
  updateHandler: (user: object) => void;
  formUpdate?: boolean;
  closeHandler: () => void;
};

class UserForm extends Component<Props, State> {
 
  componentDidUpdate() {...}
  addUser = (e: any) => {...};
  update = () => {...};

  render() {
    return (
      <div>
        <Form onSubmit={this.addUser}>
          ...

          <Modal.Footer>
            {this.props.formUpdate ? (
              <>
                <Button
                  variant="outline-secondary"
                  className="modal-btn"
                  onClick={this.props.closeHandler}
                >
                  Cancel
                </Button>
                <Button
                  variant="danger"
                  className="modal-btn"
                  type="button"
                  onClick={this.update}
                >
                  Edit User
                </Button>
              </>
            ) : (
              <>
                <Button
                  variant="outline-secondary"
                  className="modal-btn"
                  onClick={this.props.closeHandler}
                >
                  Cancel
                </Button>
                <Button
                  variant="danger"
                  className="modal-btn"
                  type="submit"
                  onClick={this.addUser}
                >
                  Create User
                </Button>
              </>
            )}
          </Modal.Footer>
        </Form>
      </div>
    );
  }
}

export default UserForm;

You are using the same boolean flag show in both the models.您在两个模型中使用相同的 boolean 标志show You need to create two different state to manage two models.您需要创建两个不同的 state 来管理两个模型。 Right now when you setShow(true) , it will make both the models visible and edit modal being on top on create user model.现在,当您setShow(true)时,它将使模型可见,并且编辑模式位于创建用户 model 的顶部。

  const [showCreateUserModal, setShowCreateUserModal] = useState(false);
  const [showEditUserModal, setShowEditUserModal] = useState(false);

For Create User对于创建用户

<Modal show={showCreateUserModal} onHide={()=>setShowCreateUserModal(false)} className="modal">

For Create User对于创建用户

<Modal show={showEditUserModal} onHide={()=>setShowEditUserModal(false)} className="modal">

And then update code to use two different states rather than using just one.然后更新代码以使用两种不同的状态,而不是只使用一种。

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

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