A guide on achieving a three-element lineup in React

I work with React and Typescript.

My goal is to have these three components displayed in a single row.

Currently, the first button appears on the left while the input form and upload button are stacked vertically on the right. How can I arrange all three elements horizontally?

return (
<Box padding={2}>
  <Typography variant="h4">
    <FormattedMessage defaultMessage="Add file" />
  </Typography>
  <Grid container alignItems="center" spacing={2} direction="row">
    <Grid item>
      <Button
        startIcon={<Search />}
        variant="contained"
        component="label"
        disabled={rulesFromFile.isLoading}
        onClick={() => setUploadedFile(undefined)}
      >
        <FormattedMessage defaultMessage="Choose file" />
        <input
          type="file"
          hidden
          onChange={(event: ChangeEvent<HTMLInputElement>) =>
            event.target.files && setSelectedFile(event.target.files[0])
          }
        />
      </Button>
    </Grid>
    {selectedFile && (!uploadedFile || rulesFromFile.isLoading) && (
      <div>
        <Grid item>
          <form>
            <TextField
              type="text"
              size="small"
              style={{marginLeft: 4}}
              label="someNumber"
              variant="outlined"
              value={someNumber}
              onChange={e => setNumber(e.target.value)}
            />
          </form>
        </Grid>
        <Grid item>
          <LoadingButton
            startIcon={<Upload />}
            loadingPosition="start"
            variant="contained"
            color="secondary"
            style={{marginLeft: 4}}
            onClick={() => {
              selectedFile &&
                rulesFromFile.mutateAsync({
                  file: selectedFile,
                  someNumber: someNumber,
                });
              setUploadedFile(selectedFile);
            }}
            loading={rulesFromFile.isLoading}
          >
            <FormattedMessage defaultMessage="{fileName} upload" values={{fileName: selectedFile.name}} />
          </LoadingButton>
        </Grid>
      </div>
    )}
  </Grid>
</Box>

);

Answer №1

To maintain items in a row, it is recommended to utilize a react fragment instead of a div tag

Here's an example:

{selectedFile && (!uploadedFile || rulesFromFile.isLoading) && (
   <>
      <Grid item>...</Grid>
      <Grid item>...</Grid>
   </>
)}

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

retrieving data from a separate div and injecting it into the current page using an ajax request

I have a script that calls the HTML of a div from another page to display on the current page. Here is the code snippet: <script> $(document).ready(function($)) { $('#add').click( function() { $. ...

Is the promises functionality respected by the Nextjs API?

Greetings, I hope all is well with you. I am currently learning NEXTJS and working with its API, but I have encountered a problem. When I click too quickly, the promises seem to get stuck or encounter issues. You can see the tests in action in this brief 3 ...

What HTML template is your go-to when starting a new project?

Is this the ultimate solution for creating a basic "Hello World" HTML template? I have been experimenting with this starter code to develop simple websites. Are there any other interesting tags I could incorporate to enhance it further? Usually, I refer ...

Retrieve object from nested array based on its id using Angular 6

I have an array of courses, where each course contains an array of segments. In my course-detail component, I have a specific course and I want to retrieve a segment by its ID. For example, let's say I have the following course: { "id": 1, ...

Passing events from a parent component to dynamically created child components in Angular

UPDATE: I've decided to tackle this issue in a different way by retrieving dynamic child component values in the parent component's save() function, following the accepted answer. I am attempting to create a system where a parent component emits ...

horizontal menu consolidation

Web Design <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Online Hangout</title> <meta http-equiv="Content-Type" content="text ...

Check that EACH radio button has been either selected or left unselected when the button is clicked

I'm still learning Javascript and currently working on a function triggered by a button click. My goal is to display an alert if NONE of the radio buttons have been selected, regardless of whether they are "Yes" or "No." If ALL radio buttons have been ...

Attempting to iterate over a JSON object and display its contents using HTML

I have been attempting to iterate through this JSON data and display it in an accordion format similar to the one shown here: but unfortunately, my implementation is not functioning correctly. Here is what I currently have: HTML: <div class="a ...

What is the best way to incorporate the {id} property into my Next.js dynamic route using the Context API?

I am encountering an issue with passing the ${id} property to my dynamic route [id]>page.jsx, located within the CartItemPage.jsx file. The setup involves using the Context API, similar to React, where I maintain an array of CartItems to render the Cart ...

Adjust the div container to wrap underneath the image if the webpage width decreases

Hello there, I am currently in the process of creating my own portfolio. I have a brief introduction that I would like to display next to an image of myself, with the image positioned on the right side. I have a div called "container" which contains anoth ...

Typescript encounters an error when attempting to access a property of an undefined

Encountered a TypeError: Cannot read property 'ghNewPrMethod' of undefined Every time I attempt to send a webhook POST to my Typescript application, the error mentioned above pops up. Here is the snippet of code from the controller: import { G ...

Strange occurrences with HTML image tags

I am facing an issue with my React project where I am using icons inside img tags. The icons appear too big, so I tried adjusting their width, but this is affecting the width of other elements as well. Here are some screenshots to illustrate: The icon wit ...

Update object in a mongodb collection

Currently, I am working on a full stack project using MongoDB, Mongoose, Node.js, and React. In this project, I have a requirement where I need to update the status to true when users click on a specific button. The ID of the button is passed to a route in ...

I am experiencing difficulties updating my website

Is there a way to automatically refresh my webpage in PHP whenever I press the enter key? ...

What is the process for setting up distinct radio types that will be separated?

I'm attempting to generate two separate questions with radio buttons in HTML. However, whenever I test it, I can only select one option even though I used to separate the questions to prevent interaction. Can anyone assist me? Do you agree? Yes ...

The issue of a malfunctioning react TypeScript map when used in conjunction with useContext

I am attempting to retrieve data from the TVMaze API using React, TypeScript, and useContext. Although I can display the data, the useContext does not update with the return value, so when I use the map function, nothing is displayed. Any advice on how to ...

Tips on concealing sub-menu until cursor hovers over it

I'm currently in the process of creating a navigation bar for a website, but I'm encountering some challenges with the submenu. While I've managed to position the submenu relative to its parent ul, I'm struggling with making it invisibl ...

What is the process for activating emmet in a Next.js JavaScript file?

What is the process for activating emmet in a Next.js JavaScript file? ...

Are DIV elements really impossible to click using selenium Web Driver?

Can DIV elements be clicked using selenium Web Driver? For example, I'm having trouble clicking the delete button in Gmail. https://i.stack.imgur.com/zsyio.png I've been trying to locate the element using the XPATH = //div[@aria-label='De ...

How can I utilize JQuery to dynamically refresh a dropdown menu?

My dropdown list is initially empty: <div> <label>Boarding Point </label> <select title="Select pickup city" id="boardingDropdown"> </select> </div> I am trying to popula ...