Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncheck Radio buttons with Material UI

I wish to be able to uncheck radio buttons, Idea is like this: if I click on some radio button, it is going to be checked, if I click on another field, this another field is going to be checked instead BUT if I click on field which is already checked, I wish to uncheck it so all fields are empty. I tried to catch the moment of being checked or unchecked but seems like opposite to checkboxes, Radio buttons don't have this field. does anyone has idea how to achieve that?

setTests = (key, e) => {
    console.log(e.checked)
    if (e.checked) {
      // this.setState({[key]: null})
      console.log('works')
    }
  }
RadioGroup
            value={this.state.test_mode}
            style={{ display: "block" }}
            onChange={e => this.setTests({ "test_mode", e.target })}
          >
            <FormControlLabel value="before" control={<Radio color="primary"/>} label="before tests" />
            <FormControlLabel value="progressing" control={<Radio color="primary"/>} label="progressing" />
            <FormControlLabel value="done" control={<Radio color="primary"/>} label="done" />

          </RadioGroup>
like image 430
Jacki Avatar asked Jul 31 '19 12:07

Jacki


People also ask

How do I uncheck a radio button?

To set a radio button to checked/unchecked, select the element and set its checked property to true or false , e.g. myRadio. checked = true . When set to true , the radio button becomes checked and all other radio buttons with the same name attribute become unchecked.

Can you deselect a radio button UX?

Radio buttons are an essential element of forms. They are used when there is a list of two or more options that are mutually exclusive and the user must select exactly one choice. Clicking a non-selected radio button will deselect whatever other button was previously selected in the list.


1 Answers

Below is an example of how to do this. Instead of using the onChange of the RadioGroup, you use the onClick event of the Radio. If the new value matches the current value in state, then set the value to empty string.

import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import Radio from "@material-ui/core/Radio";
import RadioGroup from "@material-ui/core/RadioGroup";
import FormControlLabel from "@material-ui/core/FormControlLabel";
import FormControl from "@material-ui/core/FormControl";
import FormLabel from "@material-ui/core/FormLabel";

const useStyles = makeStyles(theme => ({
  root: {
    display: "flex"
  },
  formControl: {
    margin: theme.spacing(3)
  },
  group: {
    margin: theme.spacing(1, 0)
  }
}));

export default function RadioButtonsGroup() {
  const classes = useStyles();
  const [value, setValue] = React.useState("female");

  function handleClick(event) {
    if (event.target.value === value) {
      setValue("");
    } else {
      setValue(event.target.value);
    }
  }

  return (
    <div className={classes.root}>
      <FormControl component="fieldset" className={classes.formControl}>
        <FormLabel component="legend">Gender</FormLabel>
        <RadioGroup
          aria-label="gender"
          name="gender1"
          className={classes.group}
          value={value}
        >
          <FormControlLabel
            value="female"
            control={<Radio onClick={handleClick} />}
            label="Female"
          />
          <FormControlLabel
            value="male"
            control={<Radio onClick={handleClick} />}
            label="Male"
          />
          <FormControlLabel
            value="other"
            control={<Radio onClick={handleClick} />}
            label="Other"
          />
        </RadioGroup>
      </FormControl>
    </div>
  );
}

Edit Uncheck Radio

like image 157
Ryan Cogswell Avatar answered Oct 08 '22 18:10

Ryan Cogswell