Skip to content

Commit

Permalink
[FormLabel] Add support for CSS variables (#32602)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeeshanTamboli authored May 4, 2022
1 parent ffe229f commit c6ab79c
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 5 deletions.
109 changes: 109 additions & 0 deletions docs/pages/experiments/material-ui/form-label.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import * as React from 'react';
import {
Experimental_CssVarsProvider as CssVarsProvider,
useColorScheme,
} from '@mui/material/styles';
import CssBaseline from '@mui/material/CssBaseline';
import Box from '@mui/material/Box';
import Button from '@mui/material/Button';
import Container from '@mui/material/Container';
import Moon from '@mui/icons-material/DarkMode';
import Sun from '@mui/icons-material/LightMode';
import FormLabel from '@mui/material/FormLabel';
import FormControl from '@mui/material/FormControl';
import FormGroup from '@mui/material/FormGroup';
import FormControlLabel from '@mui/material/FormControlLabel';
import FormHelperText from '@mui/material/FormHelperText';
import Checkbox from '@mui/material/Checkbox';

const ColorSchemePicker = () => {
const { mode, setMode } = useColorScheme();
const [mounted, setMounted] = React.useState(false);
React.useEffect(() => {
setMounted(true);
}, []);
if (!mounted) {
return null;
}

return (
<Button
variant="outlined"
onClick={() => {
if (mode === 'light') {
setMode('dark');
} else {
setMode('light');
}
}}
>
{mode === 'light' ? <Moon /> : <Sun />}
</Button>
);
};

export default function CssVarsTemplate() {
const [state, setState] = React.useState({
gilad: true,
jason: false,
antoine: false,
});

const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setState({
...state,
[event.target.name]: event.target.checked,
});
};

const { gilad, jason, antoine } = state;
const error = [gilad, jason, antoine].filter((v) => v).length !== 2;
return (
<CssVarsProvider>
<CssBaseline />
<Container sx={{ my: 5 }}>
<Box sx={{ pb: 2 }}>
<ColorSchemePicker />
</Box>
<Box sx={{ display: 'flex' }}>
<FormControl sx={{ m: 3 }} component="fieldset" variant="standard">
<FormLabel component="legend">Assign responsibility</FormLabel>
<FormGroup>
<FormControlLabel
control={<Checkbox checked={gilad} onChange={handleChange} name="gilad" />}
label="Gilad Gray"
/>
<FormControlLabel
control={<Checkbox checked={jason} onChange={handleChange} name="jason" />}
label="Jason Killian"
/>
<FormControlLabel
control={<Checkbox checked={antoine} onChange={handleChange} name="antoine" />}
label="Antoine Llorca"
/>
</FormGroup>
<FormHelperText>Be careful</FormHelperText>
</FormControl>
<FormControl required error={error} component="fieldset" sx={{ m: 3 }} variant="standard">
<FormLabel component="legend">Pick two</FormLabel>
<FormGroup>
<FormControlLabel
control={<Checkbox checked={gilad} onChange={handleChange} name="gilad" />}
label="Gilad Gray"
/>
<FormControlLabel
control={<Checkbox checked={jason} onChange={handleChange} name="jason" />}
label="Jason Killian"
/>
<FormControlLabel
control={<Checkbox checked={antoine} onChange={handleChange} name="antoine" />}
label="Antoine Llorca"
/>
</FormGroup>
<FormHelperText>You can display an error</FormHelperText>
</FormControl>
</Box>
</Container>
</CssVarsProvider>
);
}
10 changes: 5 additions & 5 deletions packages/mui-material/src/FormLabel/FormLabel.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ export const FormLabelRoot = styled('label', {
};
},
})(({ theme, ownerState }) => ({
color: theme.palette.text.secondary,
color: (theme.vars || theme).palette.text.secondary,
...theme.typography.body1,
lineHeight: '1.4375em',
padding: 0,
position: 'relative',
[`&.${formLabelClasses.focused}`]: {
color: theme.palette[ownerState.color].main,
color: (theme.vars || theme).palette[ownerState.color].main,
},
[`&.${formLabelClasses.disabled}`]: {
color: theme.palette.text.disabled,
color: (theme.vars || theme).palette.text.disabled,
},
[`&.${formLabelClasses.error}`]: {
color: theme.palette.error.main,
color: (theme.vars || theme).palette.error.main,
},
}));

Expand All @@ -60,7 +60,7 @@ const AsteriskComponent = styled('span', {
overridesResolver: (props, styles) => styles.asterisk,
})(({ theme }) => ({
[`&.${formLabelClasses.error}`]: {
color: theme.palette.error.main,
color: (theme.vars || theme).palette.error.main,
},
}));

Expand Down

0 comments on commit c6ab79c

Please sign in to comment.