-
Notifications
You must be signed in to change notification settings - Fork 45
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
controlled input on select tag requires both select value AND option selected #37
Comments
It looks like Give this a try: const select = (options, value) =>
yo`<select>
${options.map((o) =>
yo`<option value="${o.value}"
selected="${value === o.value}">
${o.label}
</option>`
)}
</select>` More documentation on boolean attributes would be good! |
That's still not working for me. I'll put together a demo when I get a chance. |
@modernserf Try |
this should work? |
Hmm yep, |
FYI, this works for me /**
* Create a <select> element
* @param {string[]|Object[]} items List of value strings or objects with value and label properties
* @param {string} selected Value of selected item
* @param {Object} attributes Object of attributes to apply to <select> element
*/
module.exports = ({ items = [], selected, attributes = {} }) => {
return html`
<select ${attributes}>
${items.map((item) => {
const optAttributes = {
value: item.value || item
}
if (selected && selected === optAttributes.value) {
optAttributes.selected = 'selected'
}
const label = item.label || item
return html`<option ${optAttributes}>${label}</option>`
})}
</select>`
} |
Thanks! I'll take a look and see if I can find out why it's not working. I wonder if it's related to Babel parsing the template strings? The main reason I prefer the syntax that supplies the attributes |
Agreed @shama, actually the only reason I used an expression is because |
I've also found that both |
I guess is has to do with yo-yo copying the value of the existing |
@Hihaj I'm not sure I follow: what issue are you experiencing? |
@yoshuawuyts I was just making the same observation as @modernserf in the original question; that you need to use both ( |
@Hihaj thanks; that makes sense - feel we should address this indeed ✨ |
More of a documentation request than a feature request -- this is how to do a "controlled" (in the React parlance) version of a select tag:
Note that you need to both set a value on the
<select>
tag and set theselected
attribute on the selected<option>
.The text was updated successfully, but these errors were encountered: