material-ui icon indicating copy to clipboard operation
material-ui copied to clipboard

[TextField] Add icon prop to Text Field

Open arsalazarjr opened this issue 10 years ago • 31 comments

As referenced in #2416, we currently don't have a way to add a left icon to the Text Field component. I'd like to work on a PR for this.

TextField would be called like<TextField icon={icon} /> and would render the icon on the left side in the textField before the input.

arsalazarjr avatar Jan 14 '16 01:01 arsalazarjr

As pointed out by #3032, it could look like screen shot 2016-01-24 at 12 55 58 pm and #3038 we should be able to add an icon on the left and on the right.

oliviertassinari avatar Jan 25 '16 10:01 oliviertassinari

also would be great to see prefix and suffix image

SPAHI4 avatar Jan 25 '16 11:01 SPAHI4

I'd like something like this. Are you working on this?

renato avatar Mar 04 '16 20:03 renato

How long will it be before this will be released?

ghost avatar Mar 09 '16 20:03 ghost

I think this should be extended to the autocomplete input too, would be very useful.

mikeatm avatar May 02 '16 10:05 mikeatm

Could there be a way to add an icon with TextField validations/errors also?
screen shot 2016-07-05 at 1 19 31 pm

slice 1

mibowser avatar Jul 05 '16 20:07 mibowser

I think that should apply to all "form" fields, including SelectField.

cescox avatar Aug 01 '16 13:08 cescox

I accomplish that using TextField inside ListItem or MenuItem. Put a TextField inside ListItem then use leftIcon or rightIcon props to achieve it.

ozluy avatar Aug 01 '16 13:08 ozluy

I accomplish that using TextField inside ListItem or MenuItem.

I think that it's the right approach. I would rather use composition over additional logic in the TextField. However, I'm wondering if ListItem or MenuItem were designed with this use case in mind. I think that we could have a more specified component to do it. Like a <TextFieldIcon />

oliviertassinari avatar Aug 06 '16 11:08 oliviertassinari

I think the best would be to have it in what looks like a ListItem like shown here: https://material.google.com/components/text-fields.html#text-fields-single-line-text-field

And perhaps have it as a similar option to rightCheckbox, but then rightField, and possible support giving it an array of max. 2 elements, like TextField and/or SelectField, like shown in that phone/calendar example.

This could save a lot of space especially on mobile, and hassle, not needing to tweak the css just right to have them all align.

BlockChange avatar Dec 08 '16 10:12 BlockChange

The issue with using a Textfield nested with ListItem is that... unless I am missing something, it becomes impossible to tab to the next Textfield in your form.

For this reason alone it is preferable to have a standalone as suggested by @oliviertassinari or simply an additional prop that can be set on , although the latter seems to be discouraged by #4027. Has anyone found a working workaround?

Faolain avatar Dec 17 '16 05:12 Faolain

Is there any progress or temporary solution to solve the original issue?

lmesz avatar Jan 08 '17 14:01 lmesz

@iamzhouyi try to use something like this for autocomplete experience

<Menu disableAutoFocus>
  <MenuItem leftIcon={<SearchIcon color='#fff'/>} disabled>
    <AutoComplete hintText='Search' />
  </MenuItem>
</Menu> 

andrewkislev avatar Jan 20 '17 08:01 andrewkislev

Anyone with any progress on this? If anyone has started I'd love to help, if not I'd give it a whirl myself?

oshalygin avatar Apr 04 '17 04:04 oshalygin

Doesn't look like it @oshalygin, note it's been added to the checklist on #6566 though so might want to track progress there!

Would be a great addition.

danhardman avatar Apr 11 '17 10:04 danhardman

@oshalygin If you're going to look into this, I'd suggest working off the next branch - we aren't prioritizing new features on master. Also, because it is made of composable components, you should find it easier.

mbrookes avatar Apr 11 '17 20:04 mbrookes

I think it also would be great to have circular progress in text fields for real time checking of the text field value.

mkermani144 avatar Apr 15 '17 04:04 mkermani144

Wow, I see this was opened 1.5 years ago! Any update to this? I've tried adding CSS hacks (&before content) which kind of work, but I need more fine grained control, as I need to style it further, have it disappear or go away depending on state, and more specifically, be inside the textfield. Tried fiddling also w/ the HintText/placeholder, but all of these are totally conflicting and chaotic with each other. 😞

syntacticsugar avatar Jun 06 '17 15:06 syntacticsugar

The easiest way to achieve the icon adjacent to the text field is to wrap the field and icon in a div that can contain the absolute positioned icon.

<div style={{position: 'relative', display: 'inline-block'}}>
      <SearchIcon style={{position: 'absolute', right: 0, top: 15, width: 20, height: 20}}/>
      <TextField
           hintText="Search by Name"
           onChange={_.debounce((event, value) => this.handleSearch(value), 500)}
        />
</div>
search-bar-with-icon

If you want it on the left, simply add a textIndent to the field and change the positioning to the left.

<div style={{position: 'relative', display: 'inline-block'}}>
       <SearchIcon style={{position: 'absolute', left: 0, top: 15, width: 20, height: 20}}/>
       <TextField
              style={{textIndent: 30}}
              hintText="Search by Name"
              onChange={_.debounce((event, value) => this.handleSearch(value), 500)}
        />
</div>
search-bar-with-left-icon

If you want to have dynamic icons, then change the field onChange method to update the state which can change the icon. If you wanted to make this cleaner, you can put this in a component and call it IconTextField like <IconTextField icon={} ....other attributes />

bradrisse avatar Jun 15 '17 23:06 bradrisse

@bradrisse works great, thank god I read the whole thread, found the answer I was looking in the last comment

aditya2337 avatar Jun 17 '17 20:06 aditya2337