Skip to content

Commit

Permalink
[InputLabel][material-ui] InputLabel supports ownerState.focused for …
Browse files Browse the repository at this point in the history
…styleOverrides (#39470)
  • Loading branch information
mj12albert authored Oct 18, 2023
1 parent f962aa2 commit 3dae3db
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/mui-material/src/InputLabel/InputLabel.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const InputLabelRoot = styled(FormLabel, {
ownerState.size === 'small' && styles.sizeSmall,
ownerState.shrink && styles.shrink,
!ownerState.disableAnimation && styles.animated,
ownerState.focused && styles.focused,
styles[ownerState.variant],
];
},
Expand Down Expand Up @@ -141,7 +142,7 @@ const InputLabel = React.forwardRef(function InputLabel(inProps, ref) {
const fcs = formControlState({
props,
muiFormControl,
states: ['size', 'variant', 'required'],
states: ['size', 'variant', 'required', 'focused'],
});

const ownerState = {
Expand All @@ -152,6 +153,7 @@ const InputLabel = React.forwardRef(function InputLabel(inProps, ref) {
size: fcs.size,
variant: fcs.variant,
required: fcs.required,
focused: fcs.focused,
};

const classes = useUtilityClasses(ownerState);
Expand Down
31 changes: 31 additions & 0 deletions packages/mui-material/src/InputLabel/InputLabel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
import { expect } from 'chai';
import { describeConformance, act, createRenderer } from '@mui-internal/test-utils';
import { ClassNames } from '@emotion/react';
import { ThemeProvider, createTheme } from '@mui/material/styles';
import FormControl from '@mui/material/FormControl';
import Input from '@mui/material/Input';
import FormLabel from '@mui/material/FormLabel';
Expand Down Expand Up @@ -118,6 +119,36 @@ describe('<InputLabel />', () => {
setProps({ shrink: true });
expect(getByTestId('root')).to.have.class(classes.shrink);
});

it('provides ownerState.focused in styleOverrides', () => {
const theme = createTheme({
components: {
MuiInputLabel: {
styleOverrides: {
root: (props) => {
return {
...(props.ownerState.focused === true && {
fontWeight: '700',
}),
};
},
},
},
},
});

const { getByText } = render(
<ThemeProvider theme={theme}>
<FormControl focused>
<InputLabel>Bold Test Label</InputLabel>
</FormControl>
</ThemeProvider>,
);

const label = getByText('Bold Test Label');

expect(getComputedStyle(label).fontWeight).to.equal('700');
});
});
});

Expand Down

0 comments on commit 3dae3db

Please sign in to comment.