foundryvtt / foundryvtt

Public issue tracking and documentation for Foundry Virtual Tabletop - software connecting RPG gamers in a shared multiplayer environment with an intuitive interface and powerful API.
https://foundryvtt.com/
241 stars 9 forks source link

`HandlebarsHelpers.selectOption` does not work with an array #9244

Closed caewok closed 1 year ago

caewok commented 1 year ago

Where did you find it, or where does it belong for requests?

V10 API Documentation (https://foundryvtt.com/api/v10/)

What page or file needs a look at?

https://foundryvtt.com/api/classes/client.HandlebarsHelpers.html#selectOptions

What core version are you reporting this for?

Version 10.291

What is wrong, in your opinion?

Documentation suggests you can pass an array as choices to the Handlebars selectOptions helper:

let choices = [{a: "Choice A"}, {b: "Choice B"}];

In testing and per this Discord discussion, passing an array of choices results in a blank (undefined) list of selections in the dropdown. Passing an object appears to work fine.

aaclayton commented 1 year ago

There are a number of supported formats for the input to selectOptions. What you are looking for in this case would be an input structure that looks like either:

Option 1

let choices = {
  a: "Choice A",
  b: "Choice B"
}

with an invocation of:

{{selectOptions choices}}

OR

Option 2

let choices = [
  {id: "a", label: "Choice A"},
  {id: "b", label: "Choice B"}
]

For the second format your invocataion needs to look like:

{{selectOptions choices nameAttr="id" labelAttr="label"}}

I don't think the example data structure posted in the original issue should be supported, I think both of the above options are more clear with regards to defining the options.

Perhaps we have a documentation problem here rather than a code problem.

caewok commented 1 year ago

Yes, having seen your suggested fix, I agree this is a documentation problem with how selectOption handles array objects.