Tips for including a permanent button at the bottom of a material ui dialog box

I want to implement a dialog popup in my react js application. When the user clicks on a button, the dialog should open up. Inside the dialog, there is a form with input fields. After the user fills out all the inputs, they can submit the information by clicking the "submit" button at the bottom of the dialog. However, I am facing an issue where the submit button is not sticking to the footer. Even if there are more than 15 inputs, the user has to scroll all the way down to see the "submit" button. I am aware that material ui provides DialogActions for this purpose, but since the dialog is in the parent component, I cannot access DialogActions from the child component. Here is my code:


App.js (parent)

import React, { useState } from "react";

import Info from "./Info";
import Dialog from "@material-ui/core/Dialog";
import { DialogTitle } from "@material-ui/core";

import DialogContent from "@material-ui/core/DialogContent";
import DialogContentText from "@material-ui/core/DialogContentText";

import { DialogActions } from "@material-ui/core";

export default function App() {
  const [open, setOpen] = useState(false);

  const handleClickOpen = () => {
    setOpen(true);
  };
  const handleClose = () => {
    setOpen(false);
  };

  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <button onClick={handleClickOpen}>Click me to open dialog</button>
      <Dialog
        open={open}
        aria-labelledby="responsive-dialog-title"
        maxWidth="md"
        setMaxWidth="md"
        fullWidth={true}
      >
        <dialogContent>
          <dialogTitle>
            {" "}
            <div>
              <h4>Fill out the form</h4>
            </div>
          </dialogTitle>
          <DialogContentText>
            <Info />
          </DialogContentText>
        </dialogContent>
        {/* <DialogActions>
          <button id="responsive-dialog-title" onClick={handleClose}>
            {" "}
            Submit{" "}
          </button>
        </DialogActions> */}
      </Dialog>{" "}
    </div>
  );
}

and Info.js (child) :

import React, { useState } from "react";

export default function Info() {
  const [username, setUsername] = useState("");
  const [password, setPassword] = useState("");
  const handleClickOpen = () => {
    setOpen(true);
  };

  const handleSubmit = () => {
    console.log(username);
    console.log(password);
    console.log(address);
  };
  return (
    <form onSubmit={handleSubmit}>
      <div
        style={{
          display: "flex",
          flexDirection: "column",
          width: "350px",
          padding: "20px"
        }}
      >
        <label> Username</label>
        <input
          value={username}
          onChange={(e) => setUsername(e.target.value)}
          type="text"
        />
      </div>
      <div
        style={{
          display: "flex",
          flexDirection: "column",
          width: "350px",
          padding: "20px"
        }}
      >
        <label> Password</label>
        <input
          value={password}
          onChange={(e) => setPassword(e.target.value)}
          type="password"
        />
      </div>

      <button> Submit</button>
    </form>
  );
}

codesandbox

Can anyone suggest a way to ensure that the "submit" button in Info.js is fixed at the bottom or displayed using DialogActions? Any help would be appreciated.

Answer №1

To achieve this effect, use the CSS property position: fixed. Here is an example of how your code would look:

<form onSubmit={handleSubmit}>
  <div>
    <div
      style={{
        display: "flex",
        flexDirection: "column",
        width: "350px",
        padding: "20px"
      }}
    >
      <label> Username</label>
      <input
        value={username}
        onChange={(e) => setUsername(e.target.value)}
        type="text"
      />
    </div>
    <div
      style={{
        display: "flex",
        flexDirection: "column",
        width: "350px",
        padding: "20px"
      }}
    >
      <label> Password</label>
      <input
        value={password}
        onChange={(e) => setPassword(e.target.value)}
        type="password"
      />
    </div>
  </div>
  <div style={{ position: "fixed" }}>
    <button> Submit</button>
    <button> Cancel</button>
  </div>
</form>

To remove the default scroll from the dialog, you can do the following:

<Dialog
    open={open}
    aria-labelledby="responsive-dialog-title"
    maxWidth="md"
    setMaxWidth="md"
    fullWidth={true}
  >
    <div style={{ overflow: "hidden", height: "100%", width: "100%" }}>
      <dialogTitle>
        {" "}
        <div>
          <h4>Fill out the form</h4>
        </div>
      </dialogTitle>
      <Info />
    </div>
  </Dialog>

This approach works, but it's recommended to call the submit function from the parent component. The inputs can be populated using context or a simple state in the parent.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Storing an array in React state using the `setState`

I am attempting to add an array of markers after clicking on the map. I have tried the code below (labeled "this doesn't work") but it is causing an error to occur. PS: How can I retrieve the location of my cursor in order to create a new marker base ...

Toggle the class of a div when clicked and then change the class of another div

In my website, I have a site overlay Div (#site-overlay) that is currently set as display:none. I am looking to change the class to block when I hover and click on the menu buttons. I can use both vanilla JavaScript and jQuery to achieve this. The menu it ...

Is there a way to efficiently modify the positions of numerous markers on Google Maps while utilizing PhoneGap?

Hey there, I'm new to this and I have a service for tracking multiple cars. I'm using a timer to receive their locations, but I'm having trouble figuring out how to update the old marker with the new value. I've tried deleting all the m ...

Setting the Height of a Fixed Element to Extend to the Bottom of the Browser Window

I currently have a webpage layout featuring a header at the top, a fixed sidebar on the left side, and main content displayed on the right. A demo version of this layout can be viewed at http://jsbin.com/iqibew/3. The challenge I'm facing is ensuring ...

Enhancing the design of a button with whitespace using CSS

Recently, I had a well-designed landing page created using LeadPages. However, due to financial constraints, I am unable to continue paying for their services at the moment. The current landing page has proven to be effective in converting leads. https:// ...

Can you provide a guide on how to retrieve an HTML file using JSON?

There is a problem with fetching files in different formats. Specifically, I want to retrieve an HTML file that needs to be embedded in an iFrame. Currently, my AJAX request only retrieves SWF files. Is there a way to call and fetch the HTML file instead ...

What is the reason for $http.get not requiring a return value?

I am currently developing an angular application and I have a controller that interacts with a service. The controller sends a URL to the service, which then makes a GET request to that URL and receives JSON data in return. Within this JSON data is a URI f ...

Guide on applying CSS to option tag within a select element in VUE.js

I am attempting to design a dropdown menu that resembles the one shown in the image. However, I'm currently unable to include any CSS styling. Can anyone provide guidance on how to create a customizable select dropdown in Vue? https://i.stack.imgur.c ...

Changing the alignment of divs to center instead of the right side

I am currently working on a project that involves creating a tiled interface with responsive tiles. One issue I have encountered is that all the tiles are shifting to the left side of the div and not getting centered, no matter what I try. To see the pro ...

Refreshing the Date Display with AJAX Response

Within a view, there is a DisplayFor connected to a DateTime field in the Model. After an AJAX call returns a Date to update the field, I am able to convert the AJAX date into a MM/DD/YYYY format. However, using .val to set the DisplayFor does not reflect ...

What causes nodejs to exit prematurely without running all the code?

When running the code provided, it randomly prints "DONE" or not. Can you explain why this happens? How can I ensure that it always reaches the console.log("DONE"); line every time? const {Worker, isMainThread, parentPort} = require('node:worker_threa ...

Tips on transforming JSON data into a hierarchical/tree structure with javascript/angularJS

[ {"id":1,"countryname":"India","zoneid":"1","countryid":"1","zonename":"South","stateid":"1","zid":"1","statename":"Karnataka"}, {"id":1,"countryname":"India","zoneid":"1","countryid":"1","zonename":"South","stateid":"2","zid":"1","s ...

How to send the values of the current table row to a subcomponent in React using Material UI when a button is clicked

Icons like Edit, Get App, Account Tree, and Group have onclick events that redirect to specific subcomponents. Each row's data should be passed to the respective subcomponent. {recordsAfterPaging().map((row, index) =>{ ...

Finding the identification of the first element within a nested div using JQuery

Here is the HTML snippet that I am working with: <div id="parent"> <div id="computer"> <div id="items"> <div id="1">hp</div> <div id="2">compaq</div> <div id="3"& ...

Submitting form data using Vue and PHPMailer

I've been encountering an issue with submitting a contact form from a page. After submitting the form, nothing seems to happen. My tech stack includes Vue, Axios, and PHPMailer for handling this functionality. Upon inspecting the network tab, it appea ...

A pair of div elements within one section

Can you explain why the following HTML code uses two divs, one with a class and the other with an ID, instead of just using one of them to assign properties? .header { background-image: url("https://s3.amazonaws.com/codecademy-content/courses/web-101/ ...

The function FileReader() is not functioning properly within a Vue computed property

I'm attempting to display a set of image thumbnails by dragging images onto the screen. Here is an example of my data structure: data() { return { files: [Image1, Image2, Image3] } } ...where each Image is in a blob format. Below is my co ...

Steps to disable a field when its value is set from state

In my field array, I have dynamic fields being generated. My goal is to disable the input if its value has been initialized from the state. ...

Sending information across React context

I've encountered a challenge when trying to transfer data from one context to another in React. The job data I receive from a SignalR connection needs to be passed to a specific job context, but I'm unsure of the best approach for achieving this. ...

Creating Apache Arrow vectors in TypeScript for writing data to a Table

Currently, I am in the process of creating a method that is designed to take a column of data, referred to as data: any[], and then pack it into an Arrow-typed Array Buffer for insertion into an Arrow table. To illustrate with an example, if we consider T ...