@material-ui/core/#Checkbox JavaScript Examples

The following examples show how to use @material-ui/core/#Checkbox. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DeviceDialog.js    From budgie-stream with MIT License 4 votes vote down vote up
DeviceDialog = (props) => {
  const { onClose, open } = props;

  const { playback } = useContext(ClientContext);
  const [state, setState] = playback;
  const [selected, setSelected] = useState(null);
  const loading = state.devices.length === 0;

  const handleChange = (deviceName) => {
    const deviceIndex = state.devices.findIndex(
      (device) => device.name === deviceName
    );
    state.devices[deviceIndex].selected = !state.devices[deviceIndex].selected;
    setSelected({ ...state });
  };
  const handleOnClose = () => {
    if (selected !== null) {
      setState({ ...selected });
    }
    onClose();
  };

  const refreshDeviceList = () => {
    setState({
      devices: [],
      playing: false,
    });
    fetch();
  };

  return (
    <Dialog
      onClose={handleOnClose}
      maxWidth="xs"
      fullWidth
      aria-labelledby="simple-dialog-title"
      open={open}
    >
      <CustomDialogTitle onRefresh={refreshDeviceList} id="simple-dialog-title">
        Select Devices
      </CustomDialogTitle>
      <DialogContent>
        <List>
          {loading ? (
            <CircularProgress
              style={{ marginLeft: "180px" }}
              color="secondary"
            />
          ) : (
            state.devices.map((device) => (
              <ListItem
                button
                onClick={() => handleChange(device.name)}
                key={device.name}
              >
                <ListItemIcon>
                  <SpeakerIcon />
                </ListItemIcon>
                <ListItemText
                  id="switch-list-label-wifi"
                  primary={device.name}
                />
                <ListItemSecondaryAction>
                  <Checkbox
                    checked={device.selected}
                    name={device.name}
                    onClick={() => handleChange(device.name)}
                  />
                </ListItemSecondaryAction>
              </ListItem>
            ))
          )}
        </List>
        <DialogActions>
          <Button onClick={handleOnClose} color="primary" autoFocus>
            SAVE
          </Button>
        </DialogActions>
      </DialogContent>
    </Dialog>
  );
}