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 loses focus when InputAdornment is clicked #16665

Closed
2 tasks done
ee92 opened this issue Jul 20, 2019 · 5 comments · Fixed by #16672
Closed
2 tasks done

TextField loses focus when InputAdornment is clicked #16665

ee92 opened this issue Jul 20, 2019 · 5 comments · Fixed by #16672
Labels
component: text field This is the name of the generic UI component, not the React module! docs Improvements or additions to the documentation good first issue Great for first contributions. Enable to learn the contribution process.

Comments

@ee92
Copy link

ee92 commented Jul 20, 2019

The new version of TextField loses focus when the InputAdornment is clicked. This seems undesirable.

  • This is not a v0.x issue.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior 🤔

The older (v1) TextField retains focus when Adornment is clicked:
ezgif com-video-to-gif

Current Behavior 😯

The new TextField loses focus when Adornment is clicked:
ezgif com-video-to-gif (1)

Steps to Reproduce 🕹

Compare versions
v1: https://v1.material-ui.com/demos/text-fields/#input-adornments
v4: https://material-ui.com/components/text-fields/#input-adornments

Context 🔦

This behavior is annoying for password fields or any other field that the user might want to keep editing after clicking an adornment.

Your Environment 🌎

Tech Version
Material-UI v4.2.1
Browser all
@oliviertassinari oliviertassinari added duplicate This issue or pull request already exists component: text field This is the name of the generic UI component, not the React module! docs Improvements or additions to the documentation good first issue Great for first contributions. Enable to learn the contribution process. and removed duplicate This issue or pull request already exists labels Jul 21, 2019
@oliviertassinari
Copy link
Member

oliviertassinari commented Jul 21, 2019

@ee92 This is the Google login form input, this seems like a great baseline to follow:

Jul-21-2019 11-32-49

The logic was lost in this diff 8b5cd6a#diff-3d0c58268ffed10a2bd03ed4b6799ebb.

diff --git a/docs/src/pages/components/text-fields/InputAdornments.js b/docs/src/pages/components/text-fields/InputAdornments.js
--- a/docs/src/pages/components/text-fields/InputAdornments.js
+++ b/docs/src/pages/components/text-fields/InputAdornments.js
@@ -57,10 +57,6 @@ class InputAdornments extends React.Component {
    this.setState({ [prop]: event.target.value });
  };

- handleMouseDownPassword = event => {
-   event.preventDefault();
- };

  handleClickShowPassword = () => {
    this.setState(state => ({ showPassword: !state.showPassword }));
  };
@@ -130,7 +126,6 @@ class InputAdornments extends React.Component {
                <IconButton
                  aria-label="Toggle password visibility"
                  onClick={this.handleClickShowPassword}
-                 onMouseDown={this.handleMouseDownPassword}
                >
                  {this.state.showPassword ? <VisibilityOff /> : <Visibility />}
                </IconButton>

I can't find any motivation for this change in #12076. I would propose that we go in your direction, that we revert.

Do you want to submit a pull request? :)

@ee92
Copy link
Author

ee92 commented Jul 21, 2019

@oliviertassinari I created PR (#16672) to revert the logic in the InputAdornment demos.

Perhaps this should be a property of InputAdornment? Something like ignoreFocusOnClick. What do you think?

@onzag
Copy link

onzag commented Feb 22, 2020

This solution does not work for me.

The location of the element is lost in chrome, it goes back to the begining (but not on firefox, on firefox it works flawlessly).

I attempted different ways, from focusing manually, but the location always goes back to the beggining.

Google Chrome 80.0.3987.100 beta
Google Chrome 80.0.3987.100
Google Chrome 81.0.4044.26 beta

Tested on all of them, same behaviour.

public toggleVisible(e: React.MouseEvent) {
    e.stopPropagation();
    e.preventDefault();
    this.setState({
      visible: !this.state.visible,
    });
    this.inputRef.focus(); // tested with and withtout these
  }

  // onMouseDown of the addornment, onBlur of the text field
  public preventEverything(e: React.MouseEvent) {
    e.stopPropagation();
    e.preventDefault();
    this.inputRef.focus(); // tested with and without these
  }

@paulgarbas
Copy link

Use onMouseDown instead of onClick and insert e.preventDefault() after the desired logic.

@YOEL311
Copy link

YOEL311 commented Jun 22, 2020

Hi friends I am still having this problem
What should I do

  "@material-ui/core": "^4.10.2",
    "@material-ui/icons": "^4.9.1",
    "@material-ui/lab": "^4.0.0-alpha.56",
    "@material-ui/pickers": "^v4.0.0-alpha.8",
    "@material-ui/styles": "^4.10.0",
 <OutlinedInput
              required
              fullWidth
              type="tel"
              id="number"
              className={classes.inputForm}
              name="number"
              autoComplete="number"
              autoFocus
              onChange={(e) => setPhoneNumber(e.target.value)}
              onBlur={() => setShowError(true)}
              onFocus={() => setShowError(false)}
              error={showError && !isValidNumber}
              value={number}
              disabled={verified}
              
              startAdornment={<InputAdornment position="start">{'Phone'}</InputAdornment>}
              inputProps={{
                'aria-label': 'weight',
                className: classes.unaffected,
              }}
              labelWidth={0}
            />

textField

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: text field This is the name of the generic UI component, not the React module! docs Improvements or additions to the documentation good first issue Great for first contributions. Enable to learn the contribution process.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants