max-mapper/yo-yo

controlled input on select tag requires both select value AND option selected

Open

#37 opened on Jul 10, 2016

View on GitHub
 (14 comments) (3 reactions) (0 assignees)JavaScript (46 forks)github user discovery
help wanted

Repository metrics

Stars
 (1,322 stars)
PR merge metrics
 (PR metrics pending)

Description

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:

const select = (options, value) =>
    yo`<select value="${value}">
        ${options.map((o) =>
            yo`<option value="${o.value}"
                ${value === o.value ? "selected" : ""}>
                ${o.label}
            </option>`
        )}
    </select>`

Note that you need to both set a value on the <select> tag and set the selected attribute on the selected <option>.

Contributor guide