Learning to use Material-UI: Wrapping a list into columns

Currently, I am using Material-UI to display a list of checkboxes, similar to the example shown here.

The problem arises when my list contains around 30 items and I am placing them inside a dialog box. I want the checkbox list items to stack vertically up to a maximum height of 500px, after which I would like the remaining items to wrap into new columns, filling the width of the dialog box horizontally.

I am seeking a solution like the one mentioned in this post on Stack Overflow: (similar query on Stack Overflow)

Below is the code snippet from the MUI Reference Docs that I have replicated:

<List className={classes.root}>
  {CHECKS.map((value: string) => {                     // (CHECKS = array of strings)
    const labelId = `checkbox-list-label-${value}`;

    return (
      <ListItem
        key={value}
        role={undefined}
        dense
        button
        onClick={handleToggle(value)}
        className={classes.listItem}
      >
        <ListItemIcon>
          <Checkbox
            edge="start"
            checked={checked.indexOf(value) !== -1}
            tabIndex={-1}
            disableRipple
            inputProps={{ "aria-labelledby": labelId }}
          />
        </ListItemIcon>
        <ListItemText id={labelId} primary={value} />
      </ListItem>
    );
  })}
</List>

However, applying custom classes 'root' and 'listItem' does not seem to be making any changes. The CSS styles I have added are as follows:

checklist: {
  display: "flex",
  flexWrap: "wrap",
  flexDirection: "column",
  height: "500px",
},
item: {
  flexBasis: "50%",     // Something I saw online
},

Do I need to override some default MUI styles or make additional adjustments?

(I hope to avoid splitting my CHECKS list into multiple lists and utilizing complex flexbox configurations)

Answer №1

To achieve a wrapping effect for flex items in columns, you can utilize the column wrap property. Be sure to include overflow: auto to display a horizontal scrollbar if necessary:

const useStyles = makeStyles({
  container: {
    display: "flex",
    flexFlow: "column wrap",
    gap: "0 30px",
    backgroundColor: "pink",
    height: 300, // Adjust height as needed
    overflow: "auto"
  },
  item: {
    width: "auto"
  }
});
<div className={classes.container}>
  {CHECKS.map((value: string) => {
    return (
      <ListItem key={value} className={classes.item}>
        <ListItemIcon>
          <Checkbox edge="start" disableRipple />
        </ListItemIcon>
        <ListItemText primary={value} />
      </ListItem>
    );
  })}
</div>

Check out the Live Demo

View live demo here

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

Utilizing border-image property to showcase four dots in each corner of my div container

I'm having trouble applying a gradient color to my border using the border-image property. When I use border-image: linear-gradient(-180deg, #2D6BD0 0%, #83B8FF 100%);, I'm only seeing a single dot in each corner of my DIV. Does anyone know why t ...

The functionality of changing the checkbox to "checked" by clicking on the span is not

How can I create a toggle button with a checkbox using css and jquery? Clicking on the span representing the toggle button should change the checked property of the checkbox. Currently, the span does not change the property, even though it triggers the c ...

The issue with Docker compose is that React JS does not refresh as expected

Being new to dockers and everything related to it, I have been struggling with getting my FrontEnd code changes to update in my docker-compose project. Despite trying tutorials and instructions from the web, the answers from this question point me towards ...

The module compilation was unsuccessful due to an error in the Babel loader within the React scripts' node modules, resulting in a SyntaxError

ERROR in ./src/components/MainComments/MainComments.js Module build failed (from ./node_modules/react-scripts/node_modules/babel-loader/lib/index.js): SyntaxError: C:\Users\atapi\Desktop\brainstation\alex-tapia-brainFlix\brai ...

What is the best way to display information from an array that is nested within an object retrieved from an API using

I am attempting to display data from . Using axios to fetch data from the API, I encounter an issue when trying to map through the received data as it is an array inside an object. This results in an error: TypeError: this.props.colors.map is not a funct ...

Creating a vertical scrolling marquee to showcase a list in React - step by step guide

Trying to create a marquee effect that displays a list of items in a vertical auto-scroll. After reaching the end of the list, I want it to loop back to the beginning seamlessly for continuous animation. The code snippet below shows my progress so far - a ...

How can I maintain the router query when refreshing a page in Next.js?

I am facing an issue with a dynamic page and URL that is rendered on the client side. Below is the code snippet for the link that navigates to the second page: <Link href= {{ pathname: '/dashboard/[orderdetail]', query: { id: `${orderID}` } ...

I'm currently attempting to set up the React JS package, but unfortunately encountering some errors in the process

I'm having trouble installing react as I keep receiving errors. My system has npm version 8.12.1 installed. I attempted to downgrade react, but that didn't resolve the issue. I tried the following steps: Update npm using: npm install npm -g Dow ...

Error in React/Redux: props are undefined when using mapDispatchToProps

I am currently searching for information within a database using a react app. Within the actions.js file, I can see the response.data data when I utilize console.table. However, even after exporting, when attempting to display the information, the variabl ...

Create a dynamic list of checkboxes populated by API data and utilize React hooks to handle updates

I am looking to utilize hooks within my component that generates a list of checkboxes and allows for independent status updates for each checkbox. To accomplish this, I am currently utilizing useSelector() to retrieve the checkbox data obtained from the U ...

The autoHideDuration feature in Snackbar @material-ui is not functioning as expected

A situation arose where I utilized Snackbar from Material-ui for an alert display. My aim was to automatically hide the Snackbar after 5 seconds by setting the autoHideDuration, however, it seems this feature is not functioning as expected. <Snackbar ...

Transforming the MUI CircularProgress into a half circle shape

After utilizing CirculaProgress, I was able to achieve the following: Is there a simple method to transform it into a semicircle like shown here? ...

Unable to display images on mobile devices using HTML

I recently created a website using HTML and CSS. I have successfully added images to some of the web pages, and they display properly on laptops and desktops. However, I am facing an issue where the images do not appear on small screens, even though all im ...

Having trouble retrieving data from Flask in React when deployed on Heroku, but the connection works perfectly when running locally

When attempting to deploy a web application made with React and its REST api built with Python + Flask on Heroku, an issue arises. While I can successfully access and use JSON data from Flask when serving the application locally with npm start, the same ca ...

The Checkbox component from Material-UI does not seem to be compatible with the Redux

The data source can be found in this repository Although I am using Redux store to update the checkbox's check flag and observing that the state changes correctly, unfortunately, these modifications are not reflecting on the React components. It see ...

Encountering an issue where I am unable to access properties of undefined (specifically 'up') within Material UI styling

I encountered an error message Uncaught TypeError: Cannot read properties of undefined (reading 'up') while executing the code snippet below: I am having trouble with compiling my react js Code Snippet from Index.js import React from 'react ...

Creating SVG paths using coordinates for THREE.js

I copied the exact code from this ThreeJs Example designed for generating a three-dimensional City Model. I've created an SVG path outlining city boundaries using Google Maps and now I'm trying to use the above code to create a similar 3D object ...

Loading Disqus comments dynamically upon clicking a button within a Next.js application

After noticing a significant decrease in page performance scores due to Disqus comments embedded on Vercel Analytics, I am considering implementing a "Load comments" button instead of loading the actual comments onClick. I have been using the disqus-react ...

Utilizing Bootstrap 4 to create fixed and fluid width side-by-side divs with scrollbars

I want to create a basic dashboard page. I decided to arrange two divs next to each other to serve as the side menu and content. Here is what the layout looks like: https://i.sstatic.net/EATK6.png I encountered an issue where the vertical scrollbar is n ...

Troubleshooting the issue of option tag failing to insert values into MongoDB with React and NodeJS

I am currently working on integrating the option value tags in my MERN project. However, I am facing an issue where nothing is being inserted into the database. As a beginner in learning MERN, I am struggling to identify the root cause of this problem. B ...