Skip to content

Replaced Checkbox with MUI Checkbox #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 60 additions & 24 deletions src/ui/widgets/Checkbox/checkbox.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,53 @@
import React, { useState } from "react";
import { commonCss, Widget } from "../widget";
import { Widget } from "../widget";
import {
InferWidgetProps,
StringPropOpt,
FloatPropOpt,
FontPropOpt,
ColorPropOpt
ColorPropOpt,
BoolPropOpt
} from "../propTypes";
import { PVComponent, PVWidgetPropType } from "../widgetProps";
import { registerWidget } from "../register";
import {
FormControlLabel as MuiFormControlLabel,
Checkbox as MuiCheckbox,
styled
} from "@mui/material";
import { diamondTheme } from "../../../diamondTheme";

export const CheckboxProps = {
label: StringPropOpt,
width: FloatPropOpt,
height: FloatPropOpt,
font: FontPropOpt,
foregroundColor: ColorPropOpt
foregroundColor: ColorPropOpt,
enabled: BoolPropOpt
};

const FormControlLabel = styled(MuiFormControlLabel)({
"&.MuiFormControlLabel-root": {
height: "100%",
width: "100%",
maxHeight: "100%",
maxWidth: "100%",
display: "flex",
justifyContent: "flex-start",
alignItems: "center",
cursor: "pointer",
whiteSpace: "nowrap",
wordBreak: "break-word",
overflow: "hidden",
padding: 0,
margin: 0
},
"&.Mui-disabled": {
cursor: "not-allowed",
pointerEvents: "all !important"
}
});

export type CheckboxComponentProps = InferWidgetProps<typeof CheckboxProps> &
PVComponent;

Expand All @@ -31,33 +61,39 @@ export type CheckboxComponentProps = InferWidgetProps<typeof CheckboxProps> &
export const CheckboxComponent = (
props: CheckboxComponentProps
): JSX.Element => {
const style = {
...commonCss(props as any),
display: "flex",
alignItems: "center",
cursor: "pointer"
};

const { enabled = true } = props;
const [checked, setChecked] = useState(true);

const toggle = (): void => {
const handleChange = (): void => {
setChecked(!checked);
};
const inp = (
<input
style={{ cursor: "inherit" }}
id="cb"
type="checkbox"
checked={checked}
readOnly={true}
/>
);

return (
<form onClick={toggle} style={style}>
{inp}
<label style={{ cursor: "inherit" }}>{props.label}</label>
</form>
<FormControlLabel
disabled={!enabled}
sx={{
color:
props.foregroundColor?.toString() ??
diamondTheme.palette.primary.contrastText,
".MuiFormControlLabel-label": {
fontFamily: props.font?.css() ?? diamondTheme.typography
}
}}
control={
<MuiCheckbox
checked={checked}
onChange={handleChange}
sx={{
padding: 0,
color: diamondTheme.palette.primary.main,
"&.Mui-checked": {
color: diamondTheme.palette.primary.main
}
}}
/>
}
label={props.label}
/>
);
};

Expand Down