saleor-dashboard/src/components/ControlledSwitch.tsx
Dominik Żegleń 935a6f4542
Reduce bundle size (#1103)
* Add analysis tools

* Use deep imports to reduce bundle size

* Remove tslint config

* Remove unused packages

* Remove lodash-es references

* Use root level mui imports

* Remove mui from restricted imports
2021-05-14 10:15:15 +02:00

70 lines
1.5 KiB
TypeScript

import { FormControlLabel, Switch } from "@material-ui/core";
import { makeStyles } from "@saleor/theme";
import React from "react";
const useStyles = makeStyles(
() => ({
labelText: {
fontSize: 14
}
}),
{ name: "ControlledSwitch" }
);
interface ControlledSwitchProps {
checked: boolean;
disabled?: boolean;
label: string | React.ReactNode;
name: string;
secondLabel?: string | React.ReactNode;
uncheckedLabel?: string | React.ReactNode;
onChange?(event: React.ChangeEvent<any>);
}
export const ControlledSwitch: React.FC<ControlledSwitchProps> = props => {
const {
checked,
disabled,
onChange,
label,
name,
secondLabel,
uncheckedLabel
} = props;
const classes = useStyles(props);
return (
<FormControlLabel
control={
<Switch
onChange={() =>
onChange({ target: { name, value: !checked } } as any)
}
checked={checked}
color="primary"
name={name}
/>
}
label={
<div>
{uncheckedLabel ? (
checked ? (
label
) : (
uncheckedLabel
)
) : typeof label === "string" ? (
<span className={classes.labelText}>{label}</span>
) : (
label
)}
<div>{secondLabel ? secondLabel : null}</div>
</div>
}
disabled={disabled}
/>
);
};
ControlledSwitch.displayName = "ControlledSwitch";
export default ControlledSwitch;