Skip to content

Commit 4dd625a

Browse files
awearygaearon
authored andcommitted
Correctly render placeholder for textarea in IE11 (#8020)
* Check if textContent should be set for textarea shouldSetNodeTextContent returns whether a node.textContent should be updated. Currently it only covers one case, which is to avoid setting the textContent if the text is empty and a placeholder exists. * Only set node.value if it's equal to initialValue In IE11 textContent is populated when the placeholder attribute is set. Without this check, we end up setting node.value equal to the placeholder text, causing the textarea to actually render with the text inside. This check makes sure that textContent is equal to our expected initialValue, which should be the case when using defaultValue. * Remove placeholder/textarea check, use contentToUse instead (cherry picked from commit e644faa)
1 parent 278409d commit 4dd625a

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

src/renderers/dom/client/wrappers/ReactDOMTextarea.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,15 @@ var ReactDOMTextarea = {
167167
// This is in postMount because we need access to the DOM node, which is not
168168
// available until after the component has mounted.
169169
var node = ReactDOMComponentTree.getNodeFromInstance(inst);
170-
171-
// Warning: node.value may be the empty string at this point (IE11) if placeholder is set.
172-
node.value = node.textContent; // Detach value from defaultValue
170+
var textContent = node.textContent;
171+
172+
// Only set node.value if textContent is equal to the expected
173+
// initial value. In IE10/IE11 there is a bug where the placeholder attribute
174+
// will populate textContent as well.
175+
// https://developer.microsoft.com/microsoft-edge/platform/issues/101525/
176+
if (textContent === inst._wrapperState.initialValue) {
177+
node.value = textContent;
178+
}
173179
},
174180
};
175181

src/renderers/dom/shared/ReactDOMComponent.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -828,12 +828,18 @@ ReactDOMComponent.Mixin = {
828828
var contentToUse =
829829
CONTENT_TYPES[typeof props.children] ? props.children : null;
830830
var childrenToUse = contentToUse != null ? null : props.children;
831+
// TODO: Validate that text is allowed as a child of this node
831832
if (contentToUse != null) {
832-
// TODO: Validate that text is allowed as a child of this node
833-
if (__DEV__) {
834-
setAndValidateContentChildDev.call(this, contentToUse);
833+
// Avoid setting textContent when the text is empty. In IE11 setting
834+
// textContent on a text area will cause the placeholder to not
835+
// show within the textarea until it has been focused and blurred again.
836+
// https://github.com/facebook/react/issues/6731#issuecomment-254874553
837+
if (contentToUse !== '') {
838+
if (__DEV__) {
839+
setAndValidateContentChildDev.call(this, contentToUse);
840+
}
841+
DOMLazyTree.queueText(lazyTree, contentToUse);
835842
}
836-
DOMLazyTree.queueText(lazyTree, contentToUse);
837843
} else if (childrenToUse != null) {
838844
var mountImages = this.mountChildren(
839845
childrenToUse,

0 commit comments

Comments
 (0)