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

Use == for comparing select option values for better ko compatibility #155

Merged
merged 2 commits into from
Jan 9, 2022

Conversation

danieldickison
Copy link
Collaborator

Knockout uses == for comparing select option values against the value binding value, and this PR restores that behavior for better compatibility. This is critical if you have bindings like this:

<select data-bind="value: typeID">
  <option value="1">foo</option>
  <option value="2">bar</option>
  ...

I'll try to make a test for this if I have time.

@mbest
Copy link
Member

mbest commented Sep 3, 2021

The problem with this change is that although typeID might start as a number, it will get updated to a string when the user selects something. Instead, set your options to have the numeric value:

<select data-bind="value: typeID">
  <option data-bind="value: 1">foo</option>
  <option data-bind="value: 2">bar</option>
  ...

@mbest mbest closed this Sep 3, 2021
@mbest mbest reopened this Sep 7, 2021
@mbest
Copy link
Member

mbest commented Sep 7, 2021

I reopened this because the compatibility issue is still relevant.

@brianmhunt
Copy link
Member

Thanks for the PR @danieldickison and for your thoughts @mbest !

I haven't got my head wrapped around this. Just to help me understand the problem, might this help:

    const strictEqual = optionValue === value
    const blankEqual = optionValue === '' && value === undefined
    const numericEqual = typeof value === 'number' && Number(optionValue) === value
    if (strictEqual || blankEqual || numericEqual) {

It'd really help me if we had a test that fails and illustrates the issue.

@danieldickison
Copy link
Collaborator Author

@brianmhunt just added a test. It'll fail if you revert the change to selectExtension.ts.

@brianmhunt
Copy link
Member

Thanks @danieldickison -- what do you think of the more explicit equality test in my comment above? I'd like to avoid linters complaining about the "evil twins" and I think the more explicit we can be the easier to maintain 🤞🏻

@danieldickison
Copy link
Collaborator Author

Personally, I think just going with == along with a lint-suppression comment would be clearer than reimplementing that behavior explicitly, but that's a loosely-held opinion :)

Specifically, I think your solution should work fine, though maybe this bit:
const blankEqual = optionValue === '' && value === undefined
should be loosened up a bit to accept false/null/0 values:
const blankEqual = optionValue === '' && !value

@brianmhunt
Copy link
Member

😁 Hmm... I think the clarity of the code is important, but more important is defining and testing the conditions.

Do we want 0 and false to be considered "blank"? I think undefined certainly, but null might also be a "positive" value.

@mbest any thoughts?

Not to drag the discussion out, but just want to be sure we end up with the expected behaviour.

@nmocruz
Copy link

nmocruz commented Nov 5, 2021 via email

@mbest
Copy link
Member

mbest commented Nov 6, 2021

I prefer the more specific comparison.

@brianmhunt brianmhunt mentioned this pull request Jan 9, 2022
@brianmhunt brianmhunt merged commit 0545261 into knockout:main Jan 9, 2022
@danieldickison danieldickison deleted the select-value-equality branch January 9, 2022 18:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants