Skip to content

Default casting of Boolean prop #3

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

Closed
DonNicoJs opened this issue Feb 26, 2018 · 3 comments · Fixed by #58
Closed

Default casting of Boolean prop #3

DonNicoJs opened this issue Feb 26, 2018 · 3 comments · Fixed by #58

Comments

@DonNicoJs
Copy link

Using the version of vue-web-component-wrapper included with Vue cli version 3.0.0-beta.1

I have noticed a strange / not documented behaviour of the auto casting of Boolean Props:

a prop defined as:

props: {
  aBooleanProp: {
    type: Boolean,
    default: true
  }
}

will be set to false if the prop is omitted in the web component tag:

<my-web-component />

I understand that this may be the expected behaviour in the case of web-components but since it differ from what happens in normal Vue development I think it should be documented.

@sylvainpolletvillard
Copy link
Contributor

+1, default values should apply when omitting a boolean property.

Another example:

MyButton.vue:

<template>
  <button :class="{ red: red }">My Button</button>
</template>

<style scoped>
button.red {
  background: red;
}
</style>

<script>
export default {
  name: "MyButton",
  props: {
    red: {
      type: Boolean,
      default: true
    }
  }
};
</script>

main.js:

import Vue from 'vue'
import wrap from '@vue/web-component-wrapper'
import MyButton from "./components/MyButton.vue"

new Vue({
  render: h => h(MyButton),
}).$mount('#app')


const CustomElement = wrap(Vue, MyButton)
window.customElements.define('my-button', CustomElement)

index.html:

<body>
  <div id="app"></div>
  <my-button></my-button>
  <!-- built files will be auto injected -->
</body>

result:
image

This is not consistent and makes the use of boolean properties very difficult for the moment.

@haoqunjiang
Copy link
Member

This is an anti-pattern actually: https://eslint.vuejs.org/rules/no-boolean-default.html

@sylvainpolletvillard
Copy link
Contributor

Interesting... This anti-pattern comes from a very popular library which is using eslint-vue. I wonder why it has not been spotted before.

Anyway, the issue remains that this case should be handled the same way between vue and vue-web-component-wrapper.

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 a pull request may close this issue.

3 participants