12
votes

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?

4

4 Answers

24
votes

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>
    ),
  }}
/>
12
votes

You can do it this way: InputProps={{ endAdornment: <YourComponent /> }}

https://material-ui.com/api/input/

1
votes

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

 <TextField
          variant="outlined"
          name="rfc"
          size={'small'}
          label="RFC"
          InputProps={{
                endAdornment: (
                 <datalist id="rfc">
                     <option value="XAXX010101000"></option>
                    <option value="XEXX010101000"></option>                          
                </datalist>
                ),
                inputProps: {
                      list: "rfc"
                }
             }}
             
    />
-2
votes

Yeah, I'm afraid you'll need a different tool to do that job for you.

Have you considered using react-select?