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

[TextField] Add icon prop to Text Field #2932

Closed
arsalazarjr opened this issue Jan 14, 2016 · 31 comments
Closed

[TextField] Add icon prop to Text Field #2932

arsalazarjr opened this issue Jan 14, 2016 · 31 comments
Assignees
Labels
component: text field This is the name of the generic UI component, not the React module! good first issue Great for first contributions. Enable to learn the contribution process. new feature New feature or request
Milestone

Comments

@arsalazarjr
Copy link

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.

@oliviertassinari
Copy link
Member

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.

@SPAHI4
Copy link

SPAHI4 commented Jan 25, 2016

also would be great to see prefix and suffix
image

@renato
Copy link

renato commented Mar 4, 2016

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

@ghost
Copy link

ghost commented Mar 9, 2016

How long will it be before this will be released?

@tintin1343 tintin1343 added the new feature New feature or request label Apr 22, 2016
@mikeatm
Copy link

mikeatm commented May 2, 2016

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

@mibowser
Copy link

mibowser commented Jul 5, 2016

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

@francesco-carrella
Copy link

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

@ozluy
Copy link

ozluy commented Aug 1, 2016

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

@oliviertassinari
Copy link
Member

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 oliviertassinari added component: text field This is the name of the generic UI component, not the React module! and removed component: text field This is the name of the generic UI component, not the React module! labels Oct 19, 2016
@BlockChange
Copy link

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.

@Faolain
Copy link

Faolain commented Dec 17, 2016

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?

@lmesz
Copy link

lmesz commented Jan 8, 2017

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

@andrewkslv
Copy link

andrewkslv commented Jan 20, 2017

@iamzhouyi try to use something like this for autocomplete experience

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

@oshalygin
Copy link

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

@danhardman
Copy link

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.

@mbrookes
Copy link
Member

mbrookes commented Apr 11, 2017

@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 mbrookes added the next label Apr 11, 2017
@mkermani144
Copy link
Contributor

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

@syntacticsugar
Copy link

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. 😞

@bradrisse
Copy link

bradrisse commented Jun 15, 2017

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 />

@aditya2337
Copy link

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

@palaniichukdmytro
Copy link
Contributor

Is it possible to add as props?
<TextField
floatingLabelText='Label'
hintText='Hint Text'
errorText={${<MoreVertIcon />} Error Text}
multiLine
/>

@eldiyar7
Copy link

eldiyar7 commented Jun 30, 2017

import ActionHome from 'material-ui/svg-icons/action/home';
<TextField
floatingLabelText={<SvgIconComponent/>}
floatingLabelFixed={true}
/>

@GerardStelt
Copy link

// textfield with icon
const IconTextField = (props) => (
<span style={{ whiteSpace: 'nowrap', display: 'flex' }}>
<FontIcon style={{ width: 24, height: 24, padding: '12px 16px 0px 0px'}} className="material-icons" >{props.icon}
<TextField
icon={"person"}
hintText={props.label}
type={props.type ? props.type : 'text'}
fullWidth={true}
/>

)

@stunaz
Copy link
Contributor

stunaz commented Nov 19, 2017

I believe this can be closed, right? https://material-ui.com/demos/text-fields/#input-adornments

@oliviertassinari
Copy link
Member

@stunaz I don't think that it's what people are asking for here.

@mbrookes
Copy link
Member

mbrookes commented Dec 3, 2017

@oliviertassinari The only other type I can see in the spec is "Icon signifier":

Icon signifier

Icons describe the type of input a text field requires. They are displayed to the left of the text field.

It seems that can be acheived with only a few lines of code:

image

const styles = theme => ({
  icon: {
    margin: '20px 16px 0 0',
    color: theme.palette.action.active,
  },
});

[...]

<div>
    <Person className={classes.icon} />
    <TextField label="Name" />
</div>

Would an example in the docs allow us to close this issue?

@wongjiahau
Copy link
Contributor

@mbrookes I have an idea. Since text field is not the only component that want an icon decorator, maybe we can make a wrapper that will append icon to an element.

Example

For text field

<IconDecorator icon={<PeopleIcon/>} position="left" iconStyle={customStyle}>
    <TextField label="Name"/>
</IconDecorator>

For button

<IconDecorator icon={<ArrowRightIcon/>} position="right" iconStyle={customStyle}>
    <Button raised={true}>Go</Button>
</IconDecorator>

Advantage

This method will not break or pollute the current API, thus there will be no breaking changes.

Concern

If this IconDecorator is to be implemented, should it be included in this library or another external library?

@mbrookes
Copy link
Member

mbrookes commented Dec 5, 2017

@wongjiahau The one difficulty I see with that is that the vertical alignment is component specific, so it would have to check it's child type and adjust its margins appropriately, but that can be acheived.

If this was to go ahead, it makes sense for it to be part of MUI.

@oliviertassinari sorry to page you, but this needs your insight & experience.

@oliviertassinari oliviertassinari added this to the v1.0.0 milestone Dec 26, 2017
@oliviertassinari oliviertassinari modified the milestones: v1.0.0, v1.0.0-prerelease Feb 1, 2018
@oliviertassinari oliviertassinari modified the milestones: v1.0.0-prerelease, post v1 Feb 11, 2018
@BlakeBurnette
Copy link

BlakeBurnette commented Mar 27, 2018

This appears to still be open? Did it get kicked or is it still looking to be resolved?

@oliviertassinari
Copy link
Member

oliviertassinari commented Mar 27, 2018

@BlakeBurnette This issue is about exposing a specific component for handling this issue case. I'm not 💯 % convinced it's needed given the two following alternatives. Does anyone want to add a demo to the documentation so we can close this issue?

capture d ecran 2018-03-27 a 21 02 12
https://codesandbox.io/s/29yj0lr7p

import React from "react";
import PropTypes from "prop-types";
import { withStyles } from "material-ui/styles";
import Input, { InputLabel, InputAdornment } from "material-ui/Input";
import { FormControl } from "material-ui/Form";
import TextField from "material-ui/TextField";
import Grid from "material-ui/Grid";
import AccountCircle from "material-ui-icons/AccountCircle";

const styles = theme => ({
  margin: {
    margin: theme.spacing.unit
  }
});

function TextFieldIcons(props) {
  const { classes } = props;

  return (
    <div>
      <FormControl className={classes.margin}>
        <InputLabel htmlFor="demo1">First Name</InputLabel>
        <Input
          id="demo1"
          startAdornment={
            <InputAdornment position="start">
              <AccountCircle />
            </InputAdornment>
          }
        />
      </FormControl>
      <TextField
        className={classes.margin}
        id="demo2"
        label="First Name"
        InputProps={{
          startAdornment: (
            <InputAdornment position="start">
              <AccountCircle />
            </InputAdornment>
          )
        }}
      />
      <div className={classes.margin}>
        <Grid container spacing={8} alignItems="flex-end">
          <Grid item>
            <AccountCircle />
          </Grid>
          <Grid item>
            <TextField id="demo2" label="First Name" />
          </Grid>
        </Grid>
      </div>
    </div>
  );
}

TextFieldIcons.propTypes = {
  classes: PropTypes.object.isRequired
};

export default withStyles(styles)(TextFieldIcons);

@oliviertassinari oliviertassinari added the good first issue Great for first contributions. Enable to learn the contribution process. label Apr 1, 2018
@oliviertassinari oliviertassinari self-assigned this Apr 3, 2018
@asharma991

This comment has been minimized.

@mui mui locked as resolved and limited conversation to collaborators Aug 31, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
component: text field This is the name of the generic UI component, not the React module! good first issue Great for first contributions. Enable to learn the contribution process. new feature New feature or request
Projects
None yet
Development

No branches or pull requests