observablehq / stdlib

The Observable standard library.
https://observablehq.com/@observablehq/standard-library
ISC License
966 stars 83 forks source link

Support DOM.select pretty names #2

Closed tmcw closed 6 years ago

tmcw commented 6 years ago

Something like:

var select = DOM.select([
  ["red", "Paint it red"],
  ["green", "Not easy being"],
  ["blue", "A case of"]]);

To generate something like

<select>
  <option value="red">Paint it red</option>
  <option value="green">Not easy being</option>
  <option value="blue">A case of</option>
</select>

The gist is, well, there are plenty of cases where values don't work well as labels in a UI.

We could also do it with objects, but thinking [[k, v]...] format is how you can create a Map object, so it isn't too exotic. Question here is how 'high-level' stdlib aims to be.

mbostock commented 6 years ago

I think the value of DOM.select is somewhat questionable and I wonder whether we should provide it at all. You can create a SELECT element easily with an html tagged template literal:

viewof choice = html`<select>
  <option value="red">Paint it red</option>
  <option value="green">Not easy being</option>
  <option value="blue">A case of</option>
</select>`

Or using array.map:

options = [
  ["red", "Paint it red"],
  ["green", "Not easy being"],
  ["blue", "A case of"]
]
viewof choice = html`<select>${options.map(([value, label]) => `
  <option value="${value}">${label}</option>`)
}</select>`
mbostock commented 6 years ago

Closing in favor of html.