Select multiple options by checking checkboxes in each row using React

Is it possible to display multiple select checkboxes with more than one per row? For example, I have four options [a, b, c, d]. How can I arrange it to have 2 options per row, totaling 2 rows instead of having 1 option per row for 4 rows?

☑a ☑b

☑c ☑d

I attempted to create an array = [[a,b],[c,d]], and mapped twice but the value would read [a,b], the outermost value. So when I choose a or b, the value remains the same [a,b] since the code is similar to pseudo code like this:

   <Select
      labelId="demo-multiple-checkbox-label"
      id="demo-multiple-checkbox"
      multiple
      value={array}
      onChange={handleChange}
      input={<OutlinedInput label="Tag" />}
      renderValue={(selected) => selected.join(', ')}
      MenuProps={MenuProps}
    >
   array.map((options)=>
       <div value={options}>  <- The value in select will compare value here to check for a match 
         options.map((option)=>
           <MenuItem key={name} value={option}>  <-- This is the value I need
             <Checkbox checked={personName.indexOf(option) > -1} />
             <ListItemText primary={option} />)
           </MenuItem>
       </div>
   ) 

Can someone offer some assistance with this?

Answer №1

Are you in need of this information?

Take a look at my solution on code-sandbox

https://codesandbox.io/s/quizzical-curie-nhqlh9?fontsize=14&hidenavigation=1&theme=dark

https://i.stack.imgur.com/WXqd1.png

import "./styles.css";
import {
  Select,
  OutlinedInput,
  MenuItem,
  Checkbox,
  ListItemText
} from "@material-ui/core";
export default function App() {
  const array = ["a", "b", "c", "d"];
  return (
    <>
      <Select
        labelId="demo-multiple-checkbox-label"
        id="demo-multiple-checkbox"
        value={array}
        multiple
        style={{ width: "300px", display: "flex", flexDirection: "row" }}
        input={<OutlinedInput label="Tag" />}
      >
        <div
          style={{
            width: "300px",
            display: "flex",
            flexDirection: "row",
            flexWrap: "wrap"
          }}
        >
          {array.map((o) => (
            <MenuItem value={o} style={{ width: "150px" }}>
              <Checkbox />
              <ListItemText primary={o} />
            </MenuItem>
          ))}
        </div>
      </Select>
    </>
  );
}

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

String representation of the DOM

Is there a way to convert a DOM element to a string for parsing purposes? I've tried various libraries but can't seem to figure it out. class CustomComponent extends React.Component { render() { return ( <div className="custom ...

You are unable to utilize global SCSS in Next.js as the Global CSS is restricted to being imported only from files within your Custom <App> component

After setting up node-sass in my next.js application, I attempted to include my global scss file using import '../styles/style.scss'; in the _app.js component, but encountered an error: The system is alerting that Global CSS cannot be imported fr ...

Error during compilation due to a missing parenthesis in the loop structure

I've been experimenting with creating a carousel using just html and css, without any Javascript. I've been exploring resources on the web and following tutorials to achieve this goal. However, I've encountered an issue. I created a mixin l ...

What is the best way to prevent the onClick event from triggering during the page rendering process?

I am currently working with React, Gatsby, and Material UI Buttons. I'm facing an issue where the most recently pressed button is getting disabled along with all other buttons when running my code. Despite already implementing bindings, as suggested b ...

Is it possible to utilize a conditional statement in my EJS document to determine a directory path within my Node.js and Express application?

Just starting out with EJS and I'm trying to create a conditional statement. Basically, what I want is if I am on a specific directory path, then replace a link in my navigation bar. <% let url = window.document.URL; if(!url.includes(&a ...

Node React authentication

Struggling with implementing authentication in React Router. I am using componentDidMount to check if the user is logged in by calling an endpoint. If not, it should redirect to login, otherwise proceed to the desired component. However, this setup doesn&a ...

Angular-material's Md-dialog popup box is displayed as a separate view within the Yeoman framework

I have recently created a project using Yeoman (angular-fullstack, angular-material) and encountered an issue with triggering the md-dialog box. When clicking on a div element, the dialog box is supposed to appear. However, instead of showing the popup the ...

sharing data between two node.js servers using http

I am currently working on integrating two node.js/express servers that communicate with each other using HTTP. One of the servers, known as server A, is responsible for handling file upload requests from the browser. My goal is to seamlessly transfer any u ...

Is there a way to invoke a method in Jest Enzyme without using .simulate()?

During my unit testing for a React flight seat selection application using Jest/Enzyme, I encountered a scenario where I need to test a method within my class-based component that runs after a button is clicked. However, the button in question resides deep ...

Strategies for managing cookies and ensuring settings persist

[DEVELOPMENT] - Everything is running smoothly with no issues as cookies are set on the same domain 'localhost' [PROD / LIVE] - Click the link below The problem: The cookies are not being properly set or persisted, and I'm unsure why. If a ...

By-passing Mistakes in Iteration in JavaScript

I have been working on using Google's Feed API to fetch images from a feed within an AngularJS controller. It successfully retrieves three images, but encounters an issue when it reaches a Twitter URL, causing the script to break. I would like it to s ...

Adding Exciting Background Images and Text with CSS

I am looking to enhance my website by incorporating text in the foreground and background images on my webpages for SEO purposes. Can anyone recommend the most effective HTML/CSS approach to achieve this? Currently, I am facing compatibility issues with I ...

Implementing React with Tailwind and AOS to Enhance User Experience

I recently utilized the React Tailwind template to develop a website. It functions flawlessly in my browser with no issues when running npm start. However, I encountered errors while attempting to deploy it on Netlify or Heroku despite following the docume ...

Error message: In ReactJS, Firebase is requesting the "projectId" app configuration value which has not been provided, resulting in the error code installations/missing-app-config-values

When I deploy my Firebase React app using the command line, everything works fine. However, I encounter an error when deploying it with Travis. https://i.stack.imgur.com/372a3.png Here are my configuration files. .travis.yml language: node_js node_js: ...

Convert HTML content to a PDF file with Java

Welcome to the community! My project involves extracting data from a website containing information on various chemical substances and converting it into a PDF while preserving the original HTML formatting, including CSS styles. For example, here is a li ...

Vue 3 has a known issue where scoped styles do not get applied correctly within the content of a <slot> element

Utilizing the Oruga and Storybook libraries for creating Vue 3 components. The code in the Vue file looks like this: <template> <o-radio v-bind="$props" v-model="model"> <slot /> </o-radio> </template ...

Transforming the text color after clicking on it on Squarespace - here's how to do it!

How can I make text change color upon clicking in Squarespace? Where should I place the block id in the code to target a specific block? Check out the code snippet I've been experimenting with: <script> document.getElementById('chang ...

What is the best method for customizing icons in Material UI DataGrid?

We are struggling to customize the icons used in the DataGrid component from Material UI. Despite the limited documentation available, we are unable to find a clear way to achieve this. While the documentation mentions slots for icons, it lacks explanatio ...

Implement two different styles within an element's style properties

Welcome everyone, I have a question regarding adding marquee effects to a table row along with highlighting it. Is it possible to include moving text from left to right in the same active row? For example, let's say that the current time is 12:45 AM. ...

Utilizing JavaScript and jQuery to make a query to mySQL

Looking to solve a SQL database query challenge using JavaScript or jQuery? Here's the scenario: I have a SQL db and typically use the following query to retrieve data based on distance from specified coordinates: SELECT id, ( 3959 * acos( cos( rad ...