1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Use new <Switch/> inside deprecated material switcher

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
Alex Andreev 2022-08-18 16:54:28 +03:00
parent 5249a8ebed
commit 04d89c970d
2 changed files with 17 additions and 64 deletions

View File

@ -7,6 +7,7 @@ import React from "react";
import type { FormControlLabelProps } from "@material-ui/core/FormControlLabel";
import FormControlLabel from "@material-ui/core/FormControlLabel";
import { makeStyles } from "@material-ui/styles";
import { Switch } from "./switch";
const useStyles = makeStyles({
root: {
@ -23,15 +24,12 @@ const useStyles = makeStyles({
/**
* @deprecated Use <Switch/> instead from "../switch.tsx".
*/
export function FormSwitch(props: FormControlLabelProps) {
export function FormSwitch(props: FormControlLabelProps & { children?: React.ReactNode }) {
const classes = useStyles();
return (
<FormControlLabel
control={props.control}
labelPlacement="start"
label={props.label}
className={classes.root}
/>
);
const ClonedElement = React.cloneElement(props.control, {
children: props.label,
});
return ClonedElement;
}

View File

@ -4,10 +4,8 @@
*/
import React from "react";
import type { Theme } from "@material-ui/core/styles";
import { createStyles, withStyles } from "@material-ui/core/styles";
import type { SwitchClassKey, SwitchProps } from "@material-ui/core/Switch";
import Switch from "@material-ui/core/Switch";
import { Switch } from "./switch";
interface Styles extends Partial<Record<SwitchClassKey, string>> {
focusVisible?: string;
@ -15,64 +13,21 @@ interface Styles extends Partial<Record<SwitchClassKey, string>> {
export interface SwitcherProps extends SwitchProps {
classes: Styles;
children?: React.ReactNode;
}
/**
* @deprecated Use <Switch/> instead from "../switch.tsx".
*/
export const Switcher = withStyles((theme: Theme) =>
createStyles({
root: {
width: 40,
height: 24,
padding: 0,
margin: "0 0 0 8px",
},
switchBase: {
padding: 1,
paddingLeft: 4,
"&$checked": {
transform: "translateX(14px)",
color: "white",
"& + $track": {
backgroundColor: "#52d869",
opacity: 1,
border: "none",
},
},
"&$focusVisible $thumb": {
color: "#52d869",
border: "6px solid #fff",
},
},
thumb: {
width: 18,
height: 18,
marginTop: 2,
boxShadow: "none",
},
track: {
borderRadius: 26 / 2,
backgroundColor: "#72767b",
opacity: 1,
transition: theme.transitions.create(["background-color", "border"]),
},
checked: {},
focusVisible: {},
}),
)(({ classes, ...props }: SwitcherProps) => {
export const Switcher = () => (({ disabled, checked, onChange, name, children }: SwitcherProps) => {
return (
<Switch
focusVisibleClassName={classes.focusVisible}
disableRipple
classes={{
root: classes.root,
switchBase: classes.switchBase,
thumb: classes.thumb,
track: classes.track,
checked: classes.checked,
}}
{...props}
/>
disabled={disabled}
checked={checked}
name={name}
onChange={(checked, event) => onChange?.(event, checked)}
>
{children}
</Switch>
);
});