inputs icon indicating copy to clipboard operation
inputs copied to clipboard

Allow the initial value to be set with a key, even if the data is not a Map.

Open Fil opened this issue 4 years ago • 4 comments

in maybeSelection we call valueof on all the values https://github.com/observablehq/inputs/blob/bf525033948bbdda452e65bc06c09a6b42f3b06c/src/chooser.js#L89

This means that we need to instantiate all the values, which 1) means we need to avoid complex calculations (can be solved by returning a simple key, and have another cell depend on that key…) 2) prohibits doing something like:

viewof projection = Select(["geoOrthographic", "geoMercator"], { valueof: d => d3[d](), value: ………mercator……… })

since d3["geoMercator"]() !== d3["geoMercator"]() (they are two separate instances).

https://observablehq.com/d/9a51a919c547e8e2

Fil avatar Apr 01 '21 08:04 Fil

The example feels slightly contrived since you could do

viewof projection = Select(
  ["geoOrthographic", "geoMercator", "geoEqualEarth"],
  {
    valueof: d => d3[d],
    value: d3.geoMercator
  }
)

And then

map(projection())

That said, I feel like this could be made to work:

viewof projection = Select(
  ["geoOrthographic", "geoMercator", "geoEqualEarth"],
  {
    valueof: d => d3[d](),
    key: "geoMercator"
  }
)

Currently the key option is only supported when the data is a Map:

https://github.com/observablehq/inputs/blob/853b17d0aada3f756df5a524f2852d9682e2b60a/src/chooser.js#L30

But we could have some special initialization that scans the computed keys for the given key if data is not a Map.

mbostock avatar Apr 29 '21 14:04 mbostock

How about this approach? (The wrapping in arrow functions is optional if you don’t want to specify any additional parameters to the projection and just want to use the projection as-is.)

viewof projection = Select(
  new Map([
    ["geoEqualEarth", () => d3.geoEqualEarth()],
    ["geoMercator", () => d3.geoMercator()],
    ["geoOrthographic", () => d3.geoOrthographic()],
  ]),
  {key: "geoMercator"}
)

mbostock avatar Apr 29 '21 14:04 mbostock

yes this will work for my purposes! thanks!

Fil avatar Apr 29 '21 17:04 Fil

Okay, I’m going to leave this open though since I still think we can generalize the key option as described in https://github.com/observablehq/inputs/issues/128#issuecomment-829272312.

mbostock avatar Apr 29 '21 17:04 mbostock