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

Custom element 'Array/Object' props Bug #4382

Closed
sawden opened this issue Aug 18, 2021 · 3 comments
Closed

Custom element 'Array/Object' props Bug #4382

sawden opened this issue Aug 18, 2021 · 3 comments

Comments

@sawden
Copy link

sawden commented Aug 18, 2021

Version

3.2.4

Reproduction link

https://codesandbox.io/s/vue-3-2-custom-element-array-bug-sqpip

Steps to reproduce

  1. Define component
const CustomSelect = Vue.defineCustomElement({
    props: {
        options: Array
    },
    template: `
      <select :value="value" @input="clicked">
        <option
            v-for="option in options"
            :key="option"
            :value="option"
        >
          {{ option }}
        </option>
      </select>`,
});

customElements.define('custom-select', CustomSelect);
  1. Use component
<custom-select options="['1', '2', '3']" value="1"/>
  1. Show in console
[Vue warn]: Invalid prop: type check failed for prop "options". Expected Array, got String with value "['1', '2', '3']". 
  at <CustomSelect options="['1', '2', '3']" value="1" >

What is expected?

Convert the value of the 'options' prop to an Array.

What is actually happening?

The prop type 'Array' is ignored.

@edison1105
Copy link
Member

similar to #4370

@LinusBorg
Copy link
Member

LinusBorg commented Aug 18, 2021

you are passing a string, so you get a string.

the recommended best practice for custom elements seems to be to use DOM properties for passing object/arrays values. Which Vue already does.

https://developers.google.com/web/fundamentals/web-components/best-practices#always-accept-primitive-data-strings,-numbers,-booleans-as-either-attributes-or-properties.

https://developers.google.com/web/fundamentals/web-components/best-practices#aim-to-only-accept-rich-data-objects,-arrays-as-properties.

So this will work if used in a Vue app:

<custom-select :options="['1', '2', '3']" value="1"/>

Other frameworks may have other ways of setting a DOM property. And yes, in vanilla JS you would have to use JS to set that prop and can't use an attribute.

@posva
Copy link
Member

posva commented Aug 18, 2021

Probably worth adding a note to docs at https://v3.vuejs.org/guide/web-components.html#definecustomelement. Could you open a PR or issue in the vuejs/docs repo?

@posva posva closed this as completed Aug 18, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Oct 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants
@posva @LinusBorg @edison1105 @sawden and others