max-mapper / max-mapper/yo-yo

Adding select options always sets select value to last item

Open
#57 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
1.3k
Forks
46
PR merge metrics
No merged PRs in 30d

Description

If you have an empty <select> box and then later add items to it, the "selection" (value) of the select box always seems to be set to the final item. I would expect it to be set to the first item, as it is when you first load a select box with items in it and no explicit value set.

In my case, I'd like to keep it focused on the option that says "Select..." until the user actually selects something.

Here's a small test case. I think requirebin's gatekeeper server is down so I can't post a demo.

const yo = require('yo-yo')

const View = (items) => yo`
  <select>
    ${items.length
      ? yo`<option value="">Select...</option>`
      : ''}
    ${items.map((item) => yo`
      <option>${item}</option>
    `)}
  </select>
`

const noItems = []
const viewA = View(noItems)

document.body.appendChild(viewA)

window.setTimeout(() => {
  const aFewItems = ['a', 'b', 'c']
  const viewB = View(aFewItems)
  yo.update(viewA, viewB)
}, 3000)

Any idea what's wrong?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the JavaScript reproduction from the issue and trace the yo.update(viewA, viewB) path that handles the select element and its options. Done means updating an initially empty select produces the first option as the value rather than the final item, while preserving the intended "Select..." behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.