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

Parse attributes from custom element #13

Open
lucasghizoni opened this issue May 22, 2018 · 2 comments
Open

Parse attributes from custom element #13

lucasghizoni opened this issue May 22, 2018 · 2 comments

Comments

@lucasghizoni
Copy link

lucasghizoni commented May 22, 2018

I want to pass attribs in my custom element like this:

<my-custom-element attribute-one="[1,2,3,4]" ></my-custom-element>

and receive that into my preact component as an Array, and not a String. For now, im parsing all my props before rendering:

for(let key in this.props){
	try {
		this.props[key] = JSON.parse(this.props[key])
	} catch (e) {}
}

Do you guys think that is a nice improvement for the project? If you agree, i can make a PR with that, just let me know!

@developit
Copy link
Member

One option here would be to add a hint in the property name - something like <c-e prop$="[1,2,3">

@wbern
Copy link

wbern commented Sep 1, 2020

I implemented some fairly simple logic for this in my org's forked vue wc wrapper around here, where users would pass a JSON to a prop with a -json suffix, then map that -json back to the "real" prop. It helped us keep the seamlessness between consuming components as Web Components or just as plain Vue components.

For clarity:

<x-element something-json="[1,2,3]">...</x-element>

Would get mapped to something inside the component, Array.isArray(something) would evaluate to true.

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

No branches or pull requests

3 participants