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

Updating the Spline/canvas rendering when switching pages in Next.js/ReactJS

After creating a 3D design Spline, I wanted to export it to my website. The Spline worked well on the Home page, but when switching to the About page, it had to re-render causing some issues. Here are screenshots for reference: View from the Home page T ...

When a function is called, retrieve a specific number of elements from an array using the slice method in

If I have an array of 15 objects, but I am only displaying 5 on the browser using this code: const displayedArray = array.slice(0,4) I also have a function that will load another 5 objects each time a user scrolls down. displayedArray.concat(array.slice ...

How do I adjust the ul size to be proportionate to the header?

I have a list with corresponding hyperlinks in my code, and I am trying to make these elements the same size as the header. I attempted adding auto padding and height auto but it did not produce the desired result. Here is the HTML snippet: <header id ...

What could be causing the slow compilation of my Next.js pages within the app directory, and what steps can be taken to improve the speed of this

Currently, I am working on a Next.js project that uses the 'app' directory structure. However, during local development, I have been facing significant delays in compile times. Here's a breakdown of the compile times I am encountering: - Th ...

What steps should I take to show a particular set of data upon selecting a checkbox component?

I have a table with a column named status, which can be in progress, pending, or dispensed. My goal is to filter the data based on the checkbox that is selected above the table. For instance, if I check the "pending" checkbox, only the data with the pendi ...

"Struggling to divide CSS Code with PHP containing nested brackets? Here's how you can tackle it

I'm currently attempting to break down my CSS code stored in variables, but I consistently encounter issues when dealing with media queries or similar elements. While I don't require every single rule to be outlined, I am looking for a solution ...

The React/Redux application is experiencing difficulties with API calls, as they are returning empty responses and the actions are not being triggered

Hey there, I'm currently working on a React Native app and running into some issues with making API get requests. It seems like the response is throwing an error and the action isn't executing properly. I'll share my code below, so if anyone ...

Using the 'active' class in Bootstrap-4 Navbar specifically for the parent element

As I style the navbar in Bootstrap 4, I encounter an issue with dropdown items. Specifically, when I apply the 'active' class to highlight a dropdown item, all sub-items (children) end up with the same highlighting effect. This results in an unap ...

In VS Code, the color of everything following the react fragment remains consistent

https://i.stack.imgur.com/rJtte.png Upon closer inspection, I noticed that everything after the closed fragment appears in a strange color. Initially, I suspected it was a theme problem, but upon further investigation, I realized it was not. Finding a sol ...

Changing the appearance of a website by switching CSS stylesheets according to the size of the browser

Trying to build two versions of my website: one for mobile devices or small browsing windows, and another for large browser windows. Looking to link HTML to different style sheets based on browser size. Current attempt in code only addresses total screen ...

Issue with @reach/router where the URL path is updated but the component fails to render after clicking a Link

I recently switched to using @reach/router for a new project after receiving a recommendation on the react-router-dom GitHub page. Despite what should be a simple use case, I am encountering some issues. Initially, I attempted to integrate the @mui BottomN ...

How can I send a current variable through PHP?

I have a pressing deadline and I need some guidance before moving forward. Can someone please advise if what I'm attempting is feasible or if there's a quicker solution? I encountered some problems with an accordion menu, so as a temporary fix, ...

Image transformation not rotating the image

I'm having trouble making an image of an X rotate 180 degrees when it's hovered over. Instead of rotating, the image just moves up and to the right. What am I missing that's preventing this from looking like a smooth 180-degree spin? .bl ...

Is there a way to automatically insert page numbers into every internal link on an HTML page?

When in print mode, I want to display links like this: <a href="#targetPage">link</a> but I'd prefer them to appear as: <a href="#targetPage">link (page 11)</a> (assuming the target page is on page 11 in the print preview). ...

What is the best way to place this dropdown in a fixed position within a parent element with overflow scrolling

I am facing an issue with a dropdown menu inside a parent div. The parent div has a fixed height and is set to overflow: auto. My goal is to have the menu extend beyond the boundaries of the parent when opened, but currently it just increases the height of ...

Learn how to set default values for UI TextField, Select, and checkbox material fields in React using react-hook-form

Currently, I am facing an issue with assigning default values to a form that utilizes material UI and react-hook-form for managing the form. The problem lies in the fact that select fields do not get assigned values, and even when they do, the labels of Te ...

Clicking on a single checkbox causes the entire input to become deactivated due to the way the system is

I'm encountering a puzzling issue that has me feeling like I know the solution, yet I don't. I set "State" to [checked]. The problem arises when, upon turning it into a map and clicking just one checkbox, the entire selection is clicked. To addre ...

I recently designed a form using a combination of HTML and JavaScript in order to display different dialogues depending on the number of selections made. Unfortunately, the alert function does not seem to be functioning

The concept is to have users select options and then receive employment sector suggestions based on their selections. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

Updating the state in child component with React Native

I'm curious about updating a state in a parent functional component from a child component using a function. I want to set the value of {item} in the child component to the state in the parent component, but it seems like I may have done it incorrectl ...

The StartAdornment feature in Material UI does not function properly when used in conjunction with a

The regular version of 'TextField' is experiencing issues when an icon is added to the 'StartAdornment'. When I include StartAdornment, the label remains in place. I would like the label to function as a placeholder and move up when th ...