-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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 on rerender #783
Comments
+1 I have to use the onBlur event with defaultValue to get the Textfields working smoothly. |
anyone have a workaround to make this work? |
I don't think this an issue any more. See code below: //taken from examples/webpack-example
/** In this file, we create a React component which incorporates components provided by material-ui */
const React = require('react');
const RaisedButton = require('material-ui/lib/raised-button');
const TextField = require('material-ui/lib/text-field');
const Dialog = require('material-ui/lib/dialog');
const ThemeManager = require('material-ui/lib/styles/theme-manager');
const LightRawTheme = require('material-ui/lib/styles/raw-themes/light-raw-theme');
const Colors = require('material-ui/lib/styles/colors');
const Main = React.createClass({
childContextTypes: {
muiTheme: React.PropTypes.object,
},
getInitialState () {
return {
muiTheme: ThemeManager.getMuiTheme(LightRawTheme),
counter: 0,
};
},
getChildContext() {
return {
muiTheme: this.state.muiTheme,
};
},
componentWillMount() {
let newMuiTheme = ThemeManager.modifyRawThemePalette(this.state.muiTheme, {
accent1Color: Colors.deepOrange500,
});
this.setState({muiTheme: newMuiTheme});
},
componentDidMount() {
this.refs.textField.focus();
},
render() {
console.log(this.state.counter);
let containerStyle = {
textAlign: 'center',
paddingTop: '200px',
};
let standardActions = [
{ text: 'Okay' },
];
return (
<div style={containerStyle}>
<Dialog
title="Super Secret Password"
actions={standardActions}
ref="superSecretPasswordDialog">
1-2-3-4-5
</Dialog>
<h1>material-ui</h1>
<h2>example project</h2>
<TextField ref="textField" onChange = {this._incrementStateCounter}/>
<RaisedButton label="Super Secret Password" primary={true} onTouchTap={this._handleTouchTap} />
</div>
);
},
_handleTouchTap() {
this.refs.superSecretPasswordDialog.show();
},
_incrementStateCounter () {
this.setState({counter: this.state.counter + 1});
},
});
module.exports = Main; Here's the app in action (with updated state being logged in the console as the TextField input changes). You can see that the TextField retains focus: |
I still get this. @shaurya947 example didn't reproduced the bug since the TextField didn't have an initial value property. or
|
I am also seeing what appears to be a regression of this issue. My TextFields are losing focus on re-render. I'm setting the I do notice a new warning in the console: 'TextField is changing an uncontrolled input of type text to be controlled'. I don't recall seeing that before, not sure if it's relevant here or not. The warning appears once I enter the first character into the Through a little debugging, it appears that something is causing the |
I was able to narrow this down further by determining that the source of the blur event was the first I've opened a separate issue to capture this: #4387. |
I can confirm this is still an issue... Seeing it in a very simple login form - I'm using this in conjunction with redux forms and when I interact with the form it causes a re-render (touched a field in redux-form parlance) which loses the focus. As a workaround, I'm only re-rendering the actual form if there been a change in the error state. |
You can reproduce this issue easily by putting the So you basically can't use a We noticed that on our project, I can try to push a reproducer. |
Any work around to this? Also noticing this when using a regular input text area inside of a table. |
@dalexander01 Found a "solution", but it seems very redux-form specific as was experiencing the same problem when not using md input components. If its of any use I can post a snippet! |
@deyceg please do, it cant hurt 😄 . Just run into this issue with this combination of a
|
I solved it for my case, you need to provide an unique id to that text field. EDIT: I tested again, just having the "key" on myArray to be the same across rerenders solved the issue.
|
Anyone managed to find a workaround this issue (without using |
I had the following issue too, I solved it though by having taking the SelectField implementation if you see it internally is calling a DropdownMenu https://github.com/callemall/material-ui/blob/ccf712c5733508784cd709c18c29059542d6aad1/src/SelectField/SelectField.js#L192 so I also need it to add a DropDownMenu this internally is wrapping the components in a Menu, I added disableAutoFocus to make my TextField keep the focus on it as show bellow. Maybe we can expose a DropDownMenu component from the SelectField and allow autofocus to be disable with props. |
One workaround for this for those who are struggling with it and just want something to work, though its possible its likely not applicable in some situations and could present issues (I am not entirely certain myself though) is to use a variable on the instance rather than the state. So instead of The only difference I know of between a variable on the instance vs. the state is that the instance var will not trigger a re-render. Though there could be other reasons so I'm hoping other's will catch my slack on this one. |
Still getting this error. Any work arounds? |
The workaround I found was to override the shouldComponentUpdate function to my component and then return false when I changed the state values used by my textfields.
` Doing it this way fixed my issue. The issue seems to be the component rendering every time the state changes which then resets focus. So by using the above function to disable rendering when you do an onChange event to set state values the component doesn't reload. |
@Aspintyl000 this works... somewhat. When I am setting the value of the textfield through the 'value' prop, and I am setting that prop to 'this.state.value', nothing shows up. Although, when I remove that, it seems to work. |
i am using textfields in a table row, and this is a problem :( any progress? |
I was also getting the same error . But after using componentDidMount() { |
I tried to add |
Pretty obnoxious behavior. I had experienced this after wrapping two input boxes with a styled-component. The outer div completely broke the behavior. This is a terrible flaw in the design of this library. |
So this is a pretty terrible workaround but the following works, mostly:
Yeah, not pretty... |
@scotmatson "Pretty obnoxious behavior...terrible flaw in the design of this library". This is free software, made by volunteers...good catch on the relation to |
@scotmatson I can confirm that, at least in my case, the issue is resolved if I turn the Edit: Edit2: Moving all my styled-component declarations to outside of my component resolved the losing focus issues for me. Probably a noob mistake but no one has mentioned that here so I thought I would come back and report. |
The problem I seem to be having is that the focus is lot whenever a TextFields (or Input) is being rendered inside a object.map(). The TextFields outside the maps are working perfectly. Am I missing any props or anything else? I am totally lost I tried almost every solution suggested in this thread without any luck. The rough structure is this Main File
NewUxCard
Worth noting that I am using Redux-React, so the state changes goes through input->action->reducer Any ideas? Thanks EDIT 1: So before, the structure was -> function Steps(props) -> Mapping Class . So now, I removed the function (the middle man) and just Const Steps -> Mapping Class . I can only speculate as to why this is working (perhaps some props were being ignored) but it solved my case. |
@piltsen I just encountered this problem on v1.5.0 while rendering TextFields in a |
This is also a symptom of using render props. Doing something like
can cause this behaviour. To fix, pass a fragment instead if permitted by design:
|
In my case seems not focus was lost but cursor disappear from TextField. I can check that document.activeElement is equal to my TextField, I can use keyboard command and mouse wheel to change value (type number), but every type reducer run render happen and cursor lost. Then in top of render I search by id my TextField and make
, which is not helping. But when I make focus() with some timeout, it seems to be working. Edit: |
@younes0 I can confirm that fixes it for me too. In my case, I had a helper stateless component that would return a TextField with a few common props set and then spread the rest of the props onto it, so I didn't have to specify things like Can this be re-opened, or should a new issue be filed? |
I'm still getting this issue, I'm using 3.2.0 |
@OmarDavilaP Can you create a new issue with a reproduction? |
Yup, it solves the issue in my case, basically I was using:
|
For me the issue was, I created a wrapper function for TextField, however i declared that function within the my component function, once moved outside my function everything worked as expected! I created a sendbox sample https://codesandbox.io/s/material-demo-msbxl?fontsize=14&hidenavigation=1&theme=dark |
I am having this problem with conditional rendering. I have a a javascript object in the render that returns various components in the .js file. I'm not describing it correctly, sorry. When I fix it I'll edit this post, if I remember to. edit: Yay, I fixed it! I believe the way I was conditionally rendering was rendering each component even when it wasn't displayed. I moved the logic outside of the render and it seems to work fine now. Here's the old code: edit: i can't into styling on this thing so nevermind. |
I encountered this issue today and banged my head on it for a good few hours. In my case, what I was doing wrong was wrapping the For posterity, here's a small codesandbox link to reproduce my stupidity: https://codesandbox.io/s/mui-textedit-losefocus-g6xxb |
The question now is, how would I create my custom component wrapping
|
Nevermind, the above approach works fine - I realized I was defined the custom component inside the parent component, which caused the issue. When moved outside of the parent scope (like it should be) it works fine - I updated the previous sandbox to show the correct approach: https://codesandbox.io/s/mui-textedit-losefocus-g6xxb |
This was my problem. Thank you! |
Thank you. This worked! |
@fleischman718 thanks for figuring this one out! Removing the wrapping component function for a TextField inside of a parent component's definition fixed my issue! It took me a sec to figure out what was going on in the sandbox so I put a summary below:
|
[docs] Unexpected docs spacing
Issue for me was styled components. Used standard CSS for for textarea AND the enclosing div and it works fine. |
And you, sir, are a genius. Thanks. |
This solved it for me. I was using a styled tooltip like const DarkBackgroundTooltip = withStyles({
tooltip: {
color: '#FFFFFF',
backgroundColor: '#000F33',
width: '400px',
},
})(Tooltip) But that declaration was inside the component. I moved it out of the component and it works. Thanks! |
Addind |
I've had this issue for days. Just today the dumb here realized that an useEffect inside the component was doing the rerenders. |
This comment was marked as resolved.
This comment was marked as resolved.
This won't work if you have multiple fields... |
Anyone who is still running into this issue: please refer to #783 (comment) Edit 2. Putting styled components outside of the render method did the trick for me. |
I fixed removing the styled wrapper but had to move the styles to css file ` export default function EmptyTextarea({ |
If a textfield that had focus, is rerendered by
setState
, it looses focus.The text was updated successfully, but these errors were encountered: