Search code examples
reactjsmaterial-ui

Add element inside TextField component - Material UI


I want to create input element which will have select property but also to be able to write custom text in it. I'm using React and Material-UI.

Is it possible to add element inside TextField component (inside div just below input) in Material-UI.

Currently: Usluga grupni Pos ....

With added element:

<div class="MuiFormControl-root-142 ...>
  <label class="MuiFormLabel-root-151 ...>Usluga</label>
  <div class="MuiInput-root-156 ...>
    <input aria-invalid="false" ... list="services" value="">
    <datalist id="services">
      <li tabindex="-1" ...>grupni<span class="MuiTouchRipple-root-82"></span> 
      </li>
      <li tabindex="-1" ...>Pos<span class="MuiTouchRipple-root-82"></span>
      </li>
    ....
  </div>
</div>

React currently:

<TextField
  id="service"
  label="Usluga"
  className={classes.textField}
  margin="normal"
  onChange={handleChange}
  inputProps={{
   list: "services"
  }}
/>
<datalist id="services">
 {
   services.map(option => (
     <MenuItem key={option.id} value={option.service}>
      { option.service }
     </MenuItem>
    ))
 }
</datalist>

If that's not possible, what is the other way to make this?


Solution

  • You can try this method, it worked for me :)

    <TextField
      required
      id='password'
      label='Password'
      onChange={handleOnChange}
      type={toggle.passwordVisibility ? 'text' : 'password'}
      InputProps={{
        endAdornment: (
          <InputAdornment position='end'>
            <IconButton
              aria-label='toggle password visibility'
              onClick={handlePasswordVisibility}
              onMouseDown={handleMouseDownPassword}>
              {toggle.passwordVisibility && <Visibility />}
              {!toggle.passwordVisibility && <VisibilityOff />}
            </IconButton>
          </InputAdornment>
        ),
      }}
    />