My goal is to assign unique class names to each button in a list located inside a drawer, allowing them to be called individually when needed

 return (
        <Box
            sx={{
                display: "flex",
                justifyContent: "space-between",
                width: "100%",
                height: "100%",
                overflow: "hidden",
            }}
        >
            <Drawer
                variant="permanent"
                anchor="left"
                hideBackdrop
                sx={{
                    width: DRAWER_WIDTH,
                    flexShrink: 0,
                    ".MuiPaper-root": {
                        bgcolor: "info.main",
                        width: DRAWER_WIDTH,
                        position: "absolute",
                        height: "100%",
                    },
                }}
            >
                <List>
                    {Object.values(Types.InputMode).map((v) => (
                        <Fragment key={v}> 
                    
                            <ListItem
                                button
                                onClick={() => setView(v)}
                                
                                sx={{ bgcolor: v === view ? "action.selected" : undefined }}
                                className="button-class"
                            >
                                {v}
                            </ListItem>
                        </Fragment>
                    ))}
                </List>
            </Drawer>
            <Route path={path + Types.InputRoutes[Types.InputMode.Create]}>
                <CreateProject />
            </Route>
            <Route path={path + Types.InputRoutes[Types.InputMode.Open]}>
                <OpenProject />
            </Route>
            <Route path={path + Types.InputRoutes[Types.InputMode.Import]}>
                <ImportProject />
            </Route>
        </Box>
    );

I am looking to assign specific class names to each of the buttons within a list inside a drawer, allowing me to target them individually. I'm seeking guidance on where and how to add these class names for the three buttons in question. Any assistance would be greatly appreciated.

The list of 3 buttons can be viewed here

Answer №1

If you're unsure about the structure of your Types.InputMode object, you can incorporate simple JavaScript within your rendering function.

    Object.values(Types.InputMode).map((v) => {

        const getClassName = function (value) {
            if (value === "foo") {
                return 'bar';
            }
            return 'default';
        }

        const className = getClassName(v);
        return (<Fragment key={v}>

            <ListItem button onClick={() => setView(v)}
                className={className}
                sx={{ bgcolor: v === view ? "action.selected" : undefined }}
            >
                {v}
            </ListItem>
        </Fragment>)
    })

You have the option to delegate the rendering function for improved code quality, but I trust this guidance will be beneficial to you.

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

Discover the ultimate guide to creating an interactive website using solely JavaScript, without relying on any server-side

I'm facing a challenge: I have a desire to create a website that is mainly static but also includes some dynamic components, such as a small news blog. Unfortunately, my web server only supports static files and operates as a public dropbox directory. ...

Is there a different way to retrieve the tag name of an element besides using

Currently, I am dealing with an outdated version (Chromium 25) of chromium. My goal is to utilize the tagName method in order to retrieve the name of the specific HTML tag being used. While I am aware that Element.tagName functions for versions 43 and ab ...

Checking Sudoku Solutions on Codewars

I have come across this JavaScript code which seems to be functioning correctly. However, I am curious about the line board[3][8] != board[8][3] and how it checks for repeating row and column numbers. Can someone please provide an explanation? Thank you! ...

Is there a way to modify the rounded corners of a checkbox in material-ui?

After importing material-ui into my react app, I was able to change the color and size of a checkbox as shown. However, I am having trouble changing the icon's border radius. How can I achieve this? import FormGroup from '@mui/materi ...

Creating an interactive text using Javascript functions

I am attempting to create an interactive text using JavaScript. While I can achieve this by creating a new function for each element, I realize that this approach will result in an excessive number of functions. Could anyone point out what is incorrect wi ...

Issues with embedding iframes

I have a webpage with an embedded iframe that is making ajax requests. However, when I click on a link, it opens in the iframe instead of the main window. I tried using this JavaScript code: window.parent.location.href = url of link but it didn't wo ...

Insert a fresh directive within an ng-repeat loop

I currently have an array of data that is being shown using a directive within an ng-repeat. Whenever the user clicks on a button, a new object is added to this array which requires a new instance of the directive to be executed. Here is my current HTML se ...

Tips for incorporating icons in material-ui version 1.3 are as follows:

I'm feeling a bit lost when it comes to utilizing icons in the current version of material-ui. After referring to the material-ui documentation on icons, specifically regarding SVG Material icons: We offer a separate NPM package, @material-ui/icons, ...

A Step-by-Step Guide to Setting Up a Scoreboard for Your Rock Paper Scissors Game

I'm new to JavaScript and I want to create a rock, paper, scissors game from scratch. My Game Plan: Once the user clicks on an option (rock, paper, scissors), the game will display their score and the computer's score along with the result of t ...

Three.js: How to Make a Camera Circle a Sphere?

How can we use Three.js and JavaScript/ WebGL to create a camera that orbits a sphere at a fixed height, with a fixed forward speed, and maintaining a fixed orientation in relation to the sphere? The user should only be able to steer the camera left and ri ...

Acquiring Device Data in React-Native for iOS

Hello, I am currently attempting to retrieve device information from an iPad. I attempted to use the library found at https://github.com/rebeccahughes/react-native-device-info, however, it caused issues after performing a pod install. My main goal is to ob ...

Why does the session variable keep changing after two page reloads?

CodeSnippet.php <?php session_start(); msgbox("Confirm your action: Are you sure?", "confirm"); $Result = $_SESSION['id'] ; print "<p id='txt'> </p>"; if($Result == 1 ) echo "Result is true"; elseif ( $Result == 2 ) ...

Inserting POST request data into subdocument arrays of a database

I am struggling to add data to a nested mongoose sub-document when creating a new schema from the client side. Only the data that is not nested inside another schema/array is being sent over to the database. My database setup includes MongoDB with Mongoos ...

Adding spacing breakpoints in Material-UI 5 sx properties

Currently in the process of updating my components to utilize the latest MUI version. I have been converting old classes into the sx props and I find this new styling method to be more readable in my opinion. However, one thing that concerns me is handl ...

What are the best methods for integrating Django with html, CSS, and JS?

While creating my website, I discovered that Django is typically used for the backend while HTML, CSS, and JS are used for the front end. For now, I am focused on designing my pages using HTML, CSS, and JS and have not yet begun developing the backend. I ...

Revisiting Async Await: A guide to implementing callbacks

I have the following code snippet: const myImage = document.querySelector('img'); const myRequest = new Request('flowers.jpg'); fetch(myRequest).then((response) => { console.log(response.type); // returns basic by default respo ...

Creating a test suite with Jasmine for an Angular ui-grid component compiled without using $scope

I have encountered an issue while using $compile to compile a ui-grid for Jasmine testing. Initially, everything worked smoothly when I passed $scope as a parameter to the controller. However, I am now transitioning to using vm, which has resulted in $comp ...

Guide on making a JavaScript button with both "light and dark" modes

Currently, I am attempting to change the color scheme of the page by adjusting the :root variables using a function called changeBackgrondColor(). This function is then assigned to the fontAwesome moon "button". However, when I click on the moon, the pag ...

Angular2+ allows users to easily drag and drop an image onto the screen, complete with

Check out this stackblitz link for more details: https://stackblitz.com/edit/angular6-ledera?file=app%2Fapp.component.ts I'm attempting to drag an image from the desktop and drop it directly onto the dropzone div. 1) Obtain a preview of the image 2) ...

Creating a Dual Y-Axis Chart with Two Sets of Data in chart.js

I utilized the chart.js library to write the following code snippet that generated the output shown below. My primary concern is how to effectively manage the labels on the horizontal axis in this scenario. CODE <!DOCTYPE html> <html lang="en"& ...