What is the method for incorporating a checkmark symbol into a chosen MUI React ToggleButton?

Can anyone help me with a simple question? I am trying to figure out how to add a check icon inside the selected ToggleButton. Unfortunately, Stack Overflow won't allow me to post my question in detail. Any explanations would be greatly appreciated!

export default function TableBusiness() {
  const [alignment, setAlignment] = React.useState('web');

  const handleChange = (event, newAlignment) => {
    setAlignment(newAlignment);
  };
  return (
    <Box
      sx={{
        p: 2,
        display: 'flex',
        flexWrap: 'wrap',
        '& > :not(style)': {

          width: "100%",
          height: 1000,

        },
      }}
    >
        <Box
          display="flex"
          
          justifyContent="space-around"
        >

          <ToggleButtonGroup
            color="primary"
            sx={{ borderRadius: '40px !important', backgroundColor: grey[50]}}
            value={alignment}
            exclusive
            onChange={handleChange}
            aria-label="Platform"
            

          >
            <ToggleButton sx={{borderBottomLeftRadius:20 , borderTopLeftRadius:20}} value="one">از ابتدای تاسیس</ToggleButton>
            <ToggleButton borderRadius= '30px' value="two">یکسال گذشته</ToggleButton>
            <ToggleButton value="three">یکماه گذشته</ToggleButton>
            <ToggleButton sx={{borderBottomRightRadius:20 , borderTopRightRadius:20}} value="four">جدیدترین</ToggleButton>
          </ToggleButtonGroup>
        </Box>

  );
}

I'm looking for help on how to display a check mark to the left of each selected ToggleButton. Thank you!

Answer №1

To confirm if the ToggleButton value matches the selected "alignment", you can use this comparison:

<ToggleButton sx={{borderBottomLeftRadius:20 , borderTopLeftRadius:20}} value="one">
{alignment==='one'&&<CheckIcon/>}Starting from the beginning
</ToggleButton>

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

Can a File Object be transmitted to an AWS Lambda function via API Gateway using JavaScript?

Can a JavaScript File Object be passed from a browser to AWS Lambda via API Gateway (ultimately to S3)? I am working with TypeScript and React. Environment Frontend TypeScript React AWS Amplify (for API module) Backend(AWS Lambda) Node.js Expecta ...

What is the best method for creating and passing a wrapped component to React Router?

I have multiple routes that I need to render different components for, and I want each component to be wrapped in a styled div. However, I'm looking for a way to write this code more efficiently. Is there a strategy to refactor this so that I can eas ...

Converting an array of objects into a flat array of objects with Javascript

My data array requires conversion to a flattened array returning new header and data. For instance, the first object contains a name with three data points: "title, first, last." The desired output is: From : { gender: 'male', name: { ...

Error in TypeScript - Anticipated 1-2 arguments, received either none or multiple. Code Issue TS2556

While working in JavaScript, I had no issues with this code snippet. However, when I attempted to implement it in a TypeScript Project, an error surfaced. The problem seems to revolve around the fetch(...args) function call. const fetcher = (...args) =&g ...

Multicolored progress bar in a React media player

Looking to add some flair to my react media player with a multi-color progress bar. Specifically, I want the colors of the progress bar to change at different points as the video plays. Currently, it only has one color applied to it with the following styl ...

Looking for screen dimensions using React Next.js, encountered an issue with the message: "Error: Hydration failed due to discrepancy between initial UI and server-rendered content."

I am attempting to retrieve window dimensions, but I keep receiving the error message "Error: Hydration failed because the initial UI does not match what was rendered on the server." const hasWindow = typeof window !== 'undefined'; function getW ...

Encapsulating the React Material-ui Datepicker and Timepicker OnChange event callback functionging

I'm new to React and currently incorporating Material-UI into my project for additional UI components. I've noticed some repetitive code that I'm finding difficult to refactor due to constraints within a Material-UI component's implemen ...

Enhance your application by utilizing additional hooks in the Context API

I'm exploring API and react hooks and have a query related to dispatching API fetch to ContextAPI component. Is there a way to consolidate all useState hooks into a single ContextAPI component? The objective is code refactoring by breaking it down int ...

Upon modifying the selection, I am notified to utilize the 'defaultValue' or 'value' props on the <select> tag, rather than applying 'selected' on the <option> tag

I am currently working with a material select component: <FormControl fullWidth variant="outlined" className={classes.formControl}> <InputLabel ref={inputLabel} htmlFor="outlined-age-native-simple"> Filial </InputLabel> <Sel ...

A convenient utility for generating React components with pre-populated Tailwind CSS classes

When it comes to extracting local Tailwind-styled components, I tend to do it like this: const Container: React.FC = ({ children }) => ( <div className="bg-white px-5 py-5"> {children} </div> ); To simplify this process, I ...

Utilizing React for Conditional Rendering and Navigation Bar Implementation

When developing my applications in react-native, I am using the state and a switch statement in the main render function to determine which component should be displayed on the screen. While this is a react structure question, it has implications for my sp ...

"Encountered a TypeError: Cannot read property 'params

I've encountered an issue with passing the id to my product page. Despite trying various solutions and searching for answers, I still can't get it to work. Below is my index.js code: import React from "react"; import {render} from &quo ...

Error with Firebase authentication on a Next.js project using TypeScript

I recently started a personal project using Next.js, Typescript, and Firebase for authentication and database management. While working on a sign-in page with Google integration, I encountered an error labeled as auth/argument-error. According to the Fireb ...

Unable to extract query parameters from URL using Express JS as req.query returns an empty object

I came across discussions about this issue here and here, but unfortunately, the solutions provided didn't work for me. I'm attempting to extract parameters from the URL using req.query. In my server.js file, I've implemented the following: ...

Is it possible to set data using a React Hooks' setter before the component is rendered?

Instead of making a real API call, I am exporting a JS object named Products to this file to use as mock data during the development and testing phase. My goal is to assign the state of the function to this object, but modified with mapping. The current st ...

What is the best way to set a javascript variable as the props value for a component in React?

I decided to create a new component called CheckboxFilter. In order to utilize it and assign properties to it, I need to pass props with the value of a JavaScript variable : const FilterWrapper = ({ component, filterLabel, requiredField, ...others }) => ...

Is there a way to place two input fields from different forms side by side on the same line?

Here are two forms extracted from an html page: <form method="get" action="search/s" id="number"> <div style="text-align: center;"> <input type="text" id="regNo" name="regNo" size="30" maxLength="50" > or ...

Preserve the selected Material UI Tab in ReactJS even after refreshing the page

Hey there, I've been using React Material UI Tab and ran into an issue where the tab selection is lost on page refresh. Does anyone know how to fix this problem? This pertains to a code snippet for the material ui tab component. class SimpleTabs exte ...

Developing Pathways in React

I used Materials UI to create my drawer, but I'm having trouble creating links to other pages. I even tried setting the link to https://www.google.com/, but it still didn't work. Here is a snippet of my code: <List component="na ...

Is there a way to prevent the arrow up and down events from functioning on the MUI Menu?

My menu features a text field and a button. The text field is of type number, so ideally when I click on it and use the arrow up and down keys, the value should increase and decrease. However, instead of that functionality, the menu items are being selecte ...