Skip to content
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

[InputLabel][material-ui] InputLabel supports ownerState.focused for styleOverrides #39470

Merged
merged 2 commits into from
Oct 18, 2023

Conversation

mj12albert
Copy link
Member

@mj12albert mj12albert commented Oct 16, 2023

Resolves #37069, Resolves #36550, Resolves #39427

This PR enables users to more easily customize the maxWidth of a TextField's label based on it's focused state to control truncation of the label

Screenshot 2023-10-16 at 9 25 55 PM

The before/after end result look same, but after this fix you are no longer required to manually add a focused state to achieve the customization, and it can be done in the theme

Before: https://codesandbox.io/s/https-github-com-mui-material-ui-issues-39427-vx2gvd?file=/demo.tsx

After: https://codesandbox.io/s/https-github-com-mui-material-ui-pull-39470-y7gr8x

@mj12albert mj12albert added component: text field This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material Port to v6.x labels Oct 16, 2023
@mui-bot
Copy link

mui-bot commented Oct 16, 2023

Netlify deploy preview

https://deploy-preview-39470--material-ui.netlify.app/

Bundle size report

Details of bundle changes (Toolpad)
Details of bundle changes

Generated by 🚫 dangerJS against 629cb06

Comment on lines 132 to 136
...(props.ownerState.focused === true && {
[`&.${classes.focused}`]: {
color: colorRed,
},
}),
Copy link
Member Author

@mj12albert mj12albert Oct 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently color isn't the best property to use here as it's the one property that has a specificity conflict with FormLabel: https://github.com/mui/material-ui/blob/master/packages/mui-material/src/FormLabel/FormLabel.js#L47-L49

@mj12albert mj12albert marked this pull request as ready for review October 16, 2023 16:18
@mj12albert mj12albert requested a review from DiegoAndai October 16, 2023 16:19
Copy link
Member

@DiegoAndai DiegoAndai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice addition 🎉

@mj12albert mj12albert merged commit 3dae3db into mui:master Oct 18, 2023
6 checks passed
@mj12albert mj12albert deleted the material/input-label-style-override branch October 18, 2023 05:50
@mj12albert
Copy link
Member Author

Ported to v6 in #39483

@MelDommer
Copy link

It still looks a bit odd when you enter something into the field then tab to the next, the label then shrinks. It would be good to keep the - 0 if the field has a value.

I played around with the sandbox demo you made and figured out if you check the formControl.filled you can keep the full label up top when it is not focused. Since the only time you really want to shrink is when the label moves back down into the placeholder area.

Hope this helps anyone else that finds this PR

components: {
    MuiInputLabel: {
      styleOverrides: {
        root: (props: any) => {
          const hasValue = props.ownerState.formControl?.filled === true; // filled or not
          return {
            maxWidth: `calc(100% - ${props.ownerState.focused || hasValue ? 0 : 48}px)`,
          };
        },
      },
    },
  },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: text field This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material
Projects
None yet
4 participants