-
Notifications
You must be signed in to change notification settings - Fork 48.5k
fix textarea value
of number 0
#274
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
Conversation
@@ -119,7 +119,7 @@ var ReactDOMTextarea = ReactCompositeComponent.createClass({ | |||
DOMPropertyOperations.setValueForProperty( | |||
rootNode, | |||
'value', | |||
this.props.value || '' | |||
this.props.value != null ? this.props.value : '' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Heh, look at the condition surrounding this update. I think you want to do what ReactDOMInput
does:
'' + this.props.value || ''
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ew lol, sorry about that
Actually, I think that |
true, also changed the one in input, although it more or less belong here? |
That code works, but we should have some tests to prove it 😉 |
here are the tests. I combined two checks into one (checking nothing changes, and checking for the specific bug in this issue). Not sure if it's a good idea. |
@@ -91,7 +91,7 @@ var ReactDOMInput = ReactCompositeComponent.createClass({ | |||
DOMPropertyOperations.setValueForProperty( | |||
rootNode, | |||
'value', | |||
'' + this.props.value || '' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Obviously the old code here differs from textarea already, but perhaps this was intentional so that false
becomes ''
; generally in React you can write flag && <Component />
where nothing is inserted if flag
is false so here perhaps we want to support <input value={flag && val} />
here too.
Ok, 1 week is too long to let this sit. I pulled it in last week but it failed a test internally which was depending on Those tests that fail also have tests that are checking that So I need to do a little bit more fixing of some custom components internally before I can pull this in, but soon! |
I can undo the changes if we decide on a consistent way to display |
From IRC last night it sounds like we want to turn booleans and all other non-string/number things into ''. Not sure off the top of my head how this compares to what is currently being done in DOMPropertyOperations in general or for |
I think this is still the right thing to take right now, and then we should revisit the rest of the issues. You aren't making anything worse with these changes, just strictly better. |
Everywhere else in React, true and false evaluate to the strings // <span ... title="true"></span>
React.renderComponent(React.DOM.span({title: true}), container);
// <span ... title="false"></span>
React.renderComponent(React.DOM.span({title: false}), container); Why don't we want to keep this behavior? |
Well, this diff doesn't change anything except making I think we should be consistent (that's always my theme). Do we always allow primitives? Do we only special case Let's take a look at some crazy shit... // It's a String!!!!
<div title={new String('title')}>{true}{false}{null}{undefined}{0}{{lol: 'wat'}}</div>;
// Error: Invariant Violation: escapeTextForBrowser(...): Attempted to escape an object.
// Ok, so {lol: 'wat'} makes sense when you realize that's how keys work... But then we only render string or number literals...
<div>{true}{false}{null}{undefined}{0}</div>;
// <div><span>0</span></div>
// Wait for it...
<div>{true}{false}{null}{undefined}{0}{new String('hi tim')}</div>;
// <div><span>0</span><span>h</span><span>i</span><span></span><span>t</span><span>i</span><span>m</span></div> |
This hurts my brain because nullity in JS confuses the hell out of me. Can we instead make a new module that does something like this?
I understand |
Hey @chenglou, can you rebase? (feel free to squash too if you want). Let's ignore @petehunt's comment since @yungsters accepted and Pete's getting on a plane soon anyway. We'll come back to what he said when we figure out the real consistent behavior here. |
Previously, setting textarea `value` to number 0 is treated as if `value` wasn't set at all (thus the textarea is cleared from 0 to '' upon `onChange`). `false` also renders as `"false"` instead of `""` for both `defaultValue` and `value`, on textarea _and_ input.
|
||
node.value = 'giraffe'; | ||
ReactTestUtils.Simulate.input(node); | ||
expect(node.value).toBe('0'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious why the node.value = 'giraffe'
is ignored here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a controlled component with no onChange handler… (note I'm changing the actual value, not the value
prop).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So ReactTestUtils.Simulate.input(node)
should cause node.value
to be reset? Do you happen to know where that happens?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, the input
event triggers the onChange handler in ReactDOMInput which sets value
on state to the current node value giraffe
, causing a rerender after which componentDidUpdate sets node.value
to match props.value
. Something not working for you?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm trying to upgrade the jsdom
module that we use for our internal test environment. It's a big jump (v0.2.15 to v0.8.6), so no surprise there are problems, but one of them happens to be that node.value
remains "giraffe"
after this input
event. I'll investigate some more. Thanks for confirming this is the expected behavior.
Show commit priority levels in Profiler UI (if available)
previously, setting
value
to number 0 is treated as ifvalue
wasn't set at all (thus the textarea is cleared from 0 to '' upononChange
.