0

How can I solve the overlap issue in InputLabel and OutlinedInput in Material UI?

codesandbox

<FormControl>
    <InputLabel htmlFor="my-input">Email address</InputLabel>
    <OutlinedInput id="my-input" aria-describedby="my-helper-text" />
    <FormHelperText id="my-helper-text">
        We'll never share your email.
    </FormHelperText>
</FormControl>

<hr />
<br />
<FormControl>
    <InputLabel shrink={true} htmlFor="my-input">
        Email address
    </InputLabel>
    <OutlinedInput id="my-input" aria-describedby="my-helper-text" />
    <FormHelperText id="my-helper-text">
        We'll never share your email.
    </FormHelperText>
</FormControl>

Result:

enter image description here

I tried the shrink={true} but it's not working.

2 Answers 2

2

You have to add "label" attribute in OutlinedInput tag.

 <OutlinedInput id="my-input" aria-describedby="my-helper-text"   label="Email address"/>

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. One would expect that because of the id and htmlFor, material ui can calculate this and there should be no need to duplicate that label.
0

Key Points: The direction: "ltr" ensures the label's text aligns properly for left-to-right languages. The rules such as &.MuiFormLabel-root:not(.MuiFormLabel-filled):not(.Mui-focused) let you target labels in different states. Make sure you apply ThemeProvider to wrap your app or specific components to apply these styles.

const const theme  = createTheme({ 
 components: {
  ...
  MuiInputLabel:{
  defaultProps:{},
  styleOverrides:{
   root:{
    direction:"ltr",      
    width:"100%",
    textAlign:"end",
    fontSize:20,
    "&.MuiFormLabel-root:not(.MuiFormLabel-filled):not(.Muifocused)":{color:'pink'},
    "&.Mui-focused":{left:30,top:-10},
    "&.MuiFormLabel-filled:not(.Mui-focused)":{left:30, top:-6}
  },
 },
},
},

) in component exmple

const OutLineInputWrap = ({
    inputType,
    label,
    textColor,
    textColorStateFilled,
    textColorStateFocused,
     value 
     ,onChangeHndler
    }:OutLineInputWrapType)=>{
return   (
<FormControl  sx={{ m: 1, }} variant="outlined">
     <InputLabel
   variant='outlined' 
   sx={{
 
    "&.MuiFormLabel-root:not(.MuiFormLabel-filled):not(.Mui-focused)":{color:textColor},//init
    "&.Mui-focused ":{color:textColorStateFocused},
    "&.MuiFormLabel-filled:not(.Mui-focused)":{color:textColorStateFilled},
    }} 
    htmlFor={label}>
        {label}
    </InputLabel>
    <OutlinedInput
        id={label}
        label={label}
        type={inputType}
        value={value}
        onChange={onChangeHndler}
   />
    </FormControl>
    )
}
export default OutLineInputWrap
  type HTMLInputTypes = 
  | "button"
  | "checkbox"
  | "color"
  | "date"
  | "datetime-local"
  | "email"
  | "file"
  | "hidden"
  | "image"
  | "month"
 // | "number"  not valid TS see   https://stackoverflow.com/questions/61070803/how-to-handle-number-input-in-typescript
  | "password"
  | "radio"
  | "range"
  | "reset"
  | "search"
  | "submit"
  | "tel"
  | "text"
  | "time"
  | "url"
  | "week";


interface OutLineInputWrapType {
    inputType?: HTMLInputTypes
    label:string
    textColor?:CSSProperties['color']
    textColorStateFocused?:CSSProperties['color']
    textColorStateFilled?:CSSProperties['color']    
    value:string
    onChangeHndler:ChangeEventHandler<HTMLInputElement | HTMLTextAreaElement>

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.