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

Fixes chekedLink inconsistency (controlled inputs) #2220

Closed
wants to merge 1 commit into from

Conversation

cirocosta
Copy link

As stated on a thread in ReactJS mailing list, there is an inconsistency when using checkedLink and valueLink. With the second are totally able to create a controlled component, whilst with the first, we aren't.

This PR fixes it by getting the value from the LinkedValueUtils just like how it is done for valueLink, making it consistent.

@zpao
Copy link
Member

zpao commented Sep 22, 2014

cc @petehunt

I think this is right but I haven't looked at the valuelink stuff in a long time. Pete might have a better idea.

it('should make text input controlled with valueLink', function() {
var TextInput = React.createClass({
getInitialState: function () {
return { text: 'initial' };
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: no spaces within braces

@sophiebits
Copy link
Collaborator

Looks good except a few style nits – mind fixing those up?

@syranide
Copy link
Contributor

Just bumped into this myself 👍

DOMPropertyOperations.setValueForProperty(
rootNode,
'checked',
this.props.checked || false
checked || false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!!checked ... but that shouldn't be needed either, I'm pretty sure checked is enough.

@cirocosta
Copy link
Author

Sorry for taking so long on this, had a crazy week full of tests at the university.

Just made those changes pointed and squashed

@syranide
Copy link
Contributor

@cirocosta The implementation looks great to me.

For completeness, perhaps we should copy/reuse the checkbox test and apply it for radio as well, while we're at it?

@cirocosta
Copy link
Author

@syranide , agree with you! I added a test for it. What do you think about the location of the test? I think where i put it (not right after the first test for radios) because in this we are relying on ReactLink, which is tested later, so i think it makes sense adding this test after it.

This test is also a little bigger than the test for checkbox as radio inputs behave differently from checkboxes, as i stated on the commit description. What do you think? Thanks!

expect(radioA.checked).toBe(true);
expect(radioB.checked).toBe(false);

radioA.checked = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While not technically wrong, setting only radioB.checked = true; is enough.

@syranide
Copy link
Contributor

@cirocosta Checking that ReactLink works before checking specific details of it seems sane yes. I'm not all that familiar with tests myself yet, but apart from my two minor nits it looks good to me at least. :)

},

handleChange: function (value) {
this.forceUpdate();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't actually be needed I think. It should be safe to just put null instead of this.handleChange for ReactLink (applies to all of them).

@cirocosta cirocosta force-pushed the checked_link-controlled branch 2 times, most recently from 2579ed1 to 3ed44a9 Compare September 29, 2014 16:08
@cirocosta
Copy link
Author

Made those changes @syranide . Thinking again it was actually redundant to do those checks. And yeah, assigning var node = stub.getDOMNode() makes much more sense as the operation was being done everywhere. Thanks for the tips!

@@ -281,6 +281,66 @@ describe('ReactDOMInput', function() {

});

it('should make radio input controlled with checkedLink', function() {
var RadioInput = React.createClass({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    var RadioInput = React.createClass({
      render: function () {
        return (
          <input
            type="radio"
            checkedLink={new ReactLink(false, null)} />
        );
      }
    });

Should be enough I think.
Technically this should even be enough:

var stub = ReactTestUtils.renderIntoDocument(
  <input type="radio" checkedLink={new ReactLink(false, null)} /> 
);

But perhaps we don't want to do that? @zpao @spicyj

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually the second arg must be a function (emptyFunction could be used) or it will throw as i remember. I'll take a look 👍


Thanks for the catch. Much more concise now @syranide :)

Getting the checked value directly from `this.props.checked` was causing
an inconsistency between the expected behavior when using `checkedLink`.

This commit addresses that inconsistent behavior and adds test to check
if it is producing controlled inputs as we expect for checkbox, text and
radio types.
@cirocosta cirocosta force-pushed the checked_link-controlled branch from 3ed44a9 to 4e9dbb2 Compare September 29, 2014 17:27
@syranide
Copy link
Contributor

There's no doubt to me that the fix is correct 👍

In light of separate discussions about possibly dropping ReactLink support from core, having tests for non-ReactLink controlled inputs too would probably be a good idea. But I'll let @zpao weigh in on that (sorry for being annoying :)).

@jimfb
Copy link
Contributor

jimfb commented Nov 9, 2015

This PR is over a year old, and doesn't merge cleanly. Plus, we're getting rid of value-linking in the core. So... I'm going to close this out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants