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

Utilizing theme.spacing() for organizing mathematical content in Material-UI v5

When using MUI v4, my JSS classes included code snippets like this: paddingTop: theme.mixins.toolbar.minHeight + theme.spacing(1) However, in the new version, MUI v5, theme.spacing() now returns a string rather than a number. This means that the above sta ...

The categorizing and organizing of arrays

My goal is to create a messaging application where users can view a list of their recent messages, along with a preview of the latest message received/sent, before selecting a conversation. However, I'm facing an issue with my script that displays all ...

Retrieve the content of a different page and store it as a variable using ajax

Is it possible to assign the content of another HTML page to a JavaScript variable? I attempted: var X = $(http://www.website.com/home).html() Unfortunately, this did not work, even though it seemed like a good idea. Can anyone provide guidance on how t ...

Understanding the concept of for loops in JavaScript and incorporating them in CSS styling

Hello there! I initially used this code to draw 12 lines, but now I am looking to incorporate a for loop and implement it in CSS. Can someone please guide me on how to proceed? <!DOCTYPE html> <html> <head> <style> #straigh ...

Tips for verifying the functionality of this component---Is there anything

Assistance is needed to test this component using the expect library with karma and mocha. import React from 'react'; import {Clock} from 'Clock'; import {CountdownForm} from "CountdownForm"; import {Controls} from "Controls"; export ...

When implementing protractor spyOn() with jQuery's ajax() function, an error is triggered stating 'ajax() method is non-existent'

I am currently testing the functionality of using AJAX to submit a form. Below is the Protractor code for the test: describe('login.php', function() { it("should use ajax on submit", function() { browser.get('/login.php'); spyOn($ ...

Create a multidimensional array from an array of objects

I am trying to organize my data by creating an array and listing each material based on its status. Here is the structure of my current array: let data = [ { Name: 'House 1', Details:[ {Materials: ...

Is there a way to retrieve two distinct data types from a single ng-model within a select option?

My mean stack code is functioning well, but I am looking to enhance it by adding a new feature. Specifically, I want to retrieve more elements from my NoSql database while selecting options. This is the structure of my database: Tir2 :id, price, xin, yin ...

Is there a way to attach a hidden input to the file input once the jquery simpleUpload function is successful?

Attempting to add a hidden form field after the file input used for uploading a file through the simpleUpload call. Here is the HTML (loaded dynamically): <div class="col-md-6"> <div class="form-group"> ...

showing the response message from a post request using Vue.js and Axios

Within APIService.js, there's a function: createPatient(data){ const url = 'http://192.168.1.3/api/clinic/patient/add/'; return axios.post(url, data).then(resp => {return resp}); } In the script tag of my vue component: result ...

Locate the generated ID of the inserted child when saving the parent in MongoDB

If I have a document representing a post with comments like the one below: { "_id": "579a2a71f7b5455c28a7abcb", "title": "post 1", "link": "www.link1.com", "__v": 0, "comments": [ { "author": "Andy", "body": "Wis ...

Have I potentially compromised my project by executing the command `npm audit fix --force`?

While working on a React project using Vite, everything was going smoothly. I decided to incorporate some charts and came across the recharts package, which I found quite appealing. So, I added it to my project using the command npm i recharts. After runn ...

Replacing the yellow autofill background

After much effort, I have finally discovered the ultimate method to remove autofill styling across all browsers: $('input').each(function() { var $this = $(this); $this.after($this.clone()).remove(); }); However, executing t ...

Exploring Event Propagation in AngularJS

Currently, I am working on developing a web application using angularjs for the first time. A key feature that I aim to introduce is the ability for users to create a div in the main window upon clicking on an image in the menu. To achieve this functional ...

Understanding the error handling in Express.js

Learning about error handling in express is new to me and I have a straightforward piece of code like this - const express = require('express'); const MongoClient = require('mongodb').MongoClient; const app = express(); let url = &a ...

Github Actions troubleshoot: Next.js Cypress tests encountering 'cannot read properties of undefined (reading 'getInitialProps')' error

Attempting to execute my headless Cypress tests on my Next.js React app via Github Actions has hit a snag. While the Cypress tests run smoothly when I build and run the app locally, running the same command in Github Actions triggers a Next.js error relate ...

Steps to apply V-model to elements

I am currently facing an issue with giving dynamically created input fields dynamic v-models from JSON data using vue.js. Here's what I'm doing: new Vue({ el: '#app', data() { return { totalAmt: 500, paymentMode: ...

Updating the web browser does not display the changes made on the page

Currently diving into the world of Angular and I've successfully cloned the repository. After installing the dependencies using npm, I have the web server up and running smoothly. Accessing the page on localhost:4000 works like a charm. Interestingly ...

React SVG not displaying on page

I am facing an issue with displaying an SVG in my React application. Below is the code snippet: <svg className="svg-arrow"> <use xlinkHref="#svg-arrow" /> </svg> //styling .user-quickview .svg-arrow { fill: #fff; position: ...

Formik causing malfunction in MUI DatePicker functionality

In my React project, I am using Formik to manage form processing and MUI UI components for the user interface. While I can select the day and month, I'm experiencing an issue with the year part not updating. Even when I manually type in a year in the ...