Skip to content

Conversation

lehni
Copy link
Contributor

@lehni lehni commented Nov 24, 2017

Currently, if I pass undefined as value to the toggle button, it internally throws an error due to the call of toString() on undefined for the value of ariaChecked. This fix converts it to a boolean instead and renders that as a string automatically.

@euvl
Copy link
Owner

euvl commented Nov 24, 2017

Undefined should not be allowed by validator. Using undefined on purpose is an anti-pattern in javascript and a huge source of bugs.

Two options here are:

  • do !!value for the value of your prop (in your code, not in plugin).
  • make sure validator does except only Bool values

src/Button.vue Outdated
@@ -1,7 +1,7 @@
<template>
<label role="checkbox"
:class="className"
:aria-checked="ariaChecked">
:aria-checked="!!toggled">
Copy link
Owner

Choose a reason for hiding this comment

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

aria-checked requires an actual word "true" or "false". This introduces a bug.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No it doesn't, I tested it. This coerces the value to a boolean, and the boolean then gets rendered as a string by Vue.

Copy link
Owner

@euvl euvl Nov 24, 2017

Choose a reason for hiding this comment

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

Example:

https://jsfiddle.net/63hjbL5p/1/ - look in inspector.

Requirement:

https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_checkbox_role

aria-checked="true": The checkbox is checked
aria-checked="false": The checkbox is not checked
aria-checked="mixed": The checkbox is partially checked
Example:
<span role="checkbox" aria-checked="false" tabindex="0" id="chk1"></span>

What i am saying is that if :aria-checked="false", argument will be removed from the Element. By requirement it should be present and should be aria-checked="false". Thats just how aria-checked works.

Dont get me wrong, your code also works, it just does not do what browser will expect form aria-checked argument.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I'm sorry, it does get removed you're right. I'll adjust it.

@lehni
Copy link
Contributor Author

lehni commented Nov 24, 2017

Yes I agree, undefined shouldn't be passed, but if it happens to be passed, I don't think it should throw an exception from a random place in the code, which it currently does.

This PR is to address the exception. I'm fine with adding more strict checks to the validator as well.

@lehni
Copy link
Contributor Author

lehni commented Nov 24, 2017

The problem with !!value in the code that uses the component is that this doesn't work in conjunction with v-model. Every other input component I've encountered so far is fine with the value being "falsey", I think vue-js-toggle-button should also not enforce this.

@lehni lehni force-pushed the fix/undefined-as-false branch from 1f0d085 to 642680c Compare November 24, 2017 11:52
@lehni
Copy link
Contributor Author

lehni commented Nov 24, 2017

I think this is the better approach then: 642680c

@euvl
Copy link
Owner

euvl commented Nov 24, 2017

Yep, looks good to me 👍

@euvl euvl merged commit 2206c5d into euvl:master Nov 24, 2017
@lehni
Copy link
Contributor Author

lehni commented Nov 24, 2017

Thanks @euvl !

@lehni lehni deleted the fix/undefined-as-false branch December 20, 2017 17:19
lehni added a commit to lehni/vue-js-toggle-button that referenced this pull request Dec 20, 2017
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.

2 participants