Selection tool for Material UI Fields

Is there a way to design something similar to this layout using Material UI?

I'm looking to create a selection between fields. Any advice on how to achieve this in Material UI?

Answer №1

Develop a unique component for each button, assigning it a className that dictates the background color similar to the unmarked buttons in your design. The className should correspond to the state property.

Subsequently, within the onClick event, modify the state property to switch to a CSS class with a red background.

For instance:

class Button extends React.Component {
    constructor(props) {
        super(props);
        this.state = {buttonDesign: "not-selected"};
        this.handleClick = this.handleClick.bind(this);
    }

    handleClick() {
        if (this.state.buttonDesign == "selected"){
            this.setState({
            buttonDesign: "not-selected"
          });
        }
        else if (this.state.buttonDesign == "not-selected"){
            this.setState({
            buttonDesign: "selected"
          });
        } 
        }
    };

    render() {
        return(
            <button className={this.state.buttonDesign} onClick={this.handleClick}>
                "your button name"
            </button>
        );
    }
}

Ensure you have corresponding styling in your CSS file:

.not-selected {
  background-color : #ff9191; // or desired color
}
.selected{
  background-color: #ff0000; // or desired color
}

Implement logic to keep track of selected buttons.

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

The html button adorned with a variety of CSS styles

I've been attempting to create a button for a message system that displays an orange dot when there's a new message, but I'm having trouble getting it to work. Do you think it's possible? Check out the button below: <input type="bu ...

Struggling to determine the expense upon button activation - data remains stagnant

My coding project involves a basic ordering system where users can input an integer, click on an update button, and see the total cost displayed below. Despite my efforts, I've encountered issues with two different methods: Using plain JavaScript for ...

React: It is important for every child element within a list to have a distinct "key" prop assigned to it

Despite assigning each child a key, I keep encountering this error message. function BertScoreSentence({sentence}: BertScoreSentenceProps) { let content: JSX.Element[] = [] sentence.words.forEach((word) => { content.push( <> ...

Navigating through sections in NextJS-14: Utilizing useRef for seamless scrolling

In the past, I had developed an older portfolio website using Vite React + TS and implemented useRef for scrolling to sections from the Navbar. Now, my goal is to transition this portfolio to NextJS 14. I transferred my old components and style folders in ...

I've noticed that when I use destructuring to set up multiple variables with the useState hook, everything runs smoothly. However, I'm facing issues when it comes

Here is a snippet of code for a signup form: import React, { useState } from "react"; import { Link } from "react-router-dom"; import useForm from "../useForm"; import validate from "../validation"; import { isVali ...

Printing with tiled SVG background images

Whenever I use an SVG tile as the background for my website, it appears perfectly fine when viewed in a browser. However, when I try to print the page, it scales in a strange and nonuniform way. Is there a solution to prevent this distortion when printing ...

Is it possible to horizontally center an auto-fill grid using only CSS?

I would like to fill my page horizontally with as many blocks as possible and center the result. My goal is to have a grid that can resize when the window size changes: wide window xxx small window xx x Is it possible to achieve this without using Java ...

Creating an object positioned to the right side of a div (div:right) is a matter of using CSS positioning properties

While we are familiar with pseudo-classes like :before and :after, have you ever wondered why there is no nav ul li a:left or :right? Do you think it's achievable? I'm open to using HTML5, CSS3, and JavaScript to make it happen. ...

When the window is resized, the div containing a table is getting pushed beyond its original

I have been developing a 'calculadora de creditos' or credits calculator. For this project, I used a responsive layout. Despite searching extensively online for solutions to my issue, I came up empty-handed. CSS CODE: (CSS code here) HTML CO ...

Blending Material-UI with Typescript

I am a TypeScript beginner and I'm currently following an example from the material-ui repository. The code snippet in the example is using the root CSS class. However, I am looking to include additional CSS classes alongside 'root'. const ...

Animating a CSS overlay

My goal is to design an overlay using the following HTML and CSS code snippets div.relative { position: relative; width: 400px; height: 200px; border: 3px solid #73AD21; } div.absolute { position: absolute; top: 50%; right: 0; width: 20 ...

Iterating over an array and displaying elements on the webpage

Struggling to access an array and loop through it, but only able to print out one element instead of all. Need a hint on how to solve this issue. See my code below: let toppingsCount; const burger = document.createElement('div'); const toppingsD ...

What is the best way to animate my logo using JavaScript so that it scales smoothly just like an image logo

I dedicated a significant amount of time to create a unique and eye-catching logo animation for my website! The logo animation I designed perfectly matches the size of the logo image and can be seamlessly integrated into the site. The issue arises when th ...

What causes the App component with no content to trigger render() and componentDidMount() functions twice?

I currently have a simple App component set up: class App extends React.Component { componentDidMount() { console.log('didMount') } render() { console.log('render') return <p>Empty component</p> } } The p ...

The formatting of the list is not being correctly displayed in the Ajax Jquery list

Trying to dynamically generate an unordered list from XML onto a jQuery mobile page has been a bit of a challenge for me. While I can display the items on the page, the styling never seems to work properly, only showing up as plain text with blue links. Is ...

Utilizing ReactJS within Asp.net 4.0: A Beginner's Guide

Is it feasible to integrate ReactJS into an Asp.net 4.0 project using only js files? Are there any Nuget packages that need to be installed for this integration? Seeking guidance on how to effectively incorporate ReactJS into my Asp.net 4.0 application. ...

Trouble with jQuery: Background color fade in/fade out effects not functioning

Issue with changing background color during FadeIn and FadeOut effect. Whenever I click on the "whatup" link, my button should blink with alternating red and white colors. Below is the HTML code: <a id="blkwhatsup" href="#" >whatup</a> <in ...

Centering "Label - Value" without tables in web design

It's common to have multiple labels (such as name, age, color) and a corresponding value for each one. One way to ensure that the values (for example Steve, 19, Red) all start in the same horizontal position is by organizing them in a 2 column, 3 row ...

What is the best way to define coordinates or set margins for the autoTable component in jsPdf when using React js?

While working with the jsPdf library to create a PDF in React, I am encountering an issue where I am unable to set margins for the autoTable when there are multiple tables on a single page. Below is the code snippet: import React from "react"; ...

Encountering an issue: When using .NET Core with React and NextJS, receiving a TypeError stating that _interop_require_default._ is not

Working on a new project in .NET Core with SPA using React. Want to incorporate nextJs for some navigation features. Encountering this compile error: Compile Error The error points to app-router-context.shared-runtime.js and specifically mentions the fo ...