-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow selecting options by passing current values
At the minute the only way to pre-select or check specific items when using the Nunjucks macros for these components is to pass a boolean flag for each item, which then maps directly to the checked / selected HTML attribute for the <input> / <option>. This means the user has to do the work to map the previous form data back to a boolean, resulting in code that might look something like this: ``` {{ govukSelect({ id: "sort", name: "sort", label: { text: "Sort by" }, items: [ { value: "published", text: "Recently published", selected: (data['sort'] == "published") }, { value: "updated", text: "Recently updated", selected: (data['sort'] == "updated") }, { value: "views", text: "Most views", selected: (data['sort'] == "views") }, { value: "comments", text: "Most comments", selected: (data['sort'] == "comments") } ] }) }} ``` This is prone to introducing errors (especially when prototyping) – for example, any changes to the name of the field or the value of the item also need corresponding changes made to the boolean logic, potentially in multiple places. Allow the user to pass the current value (or array of values, for checkboxes), and using that to work out which item(s) should be checked or selected. The above example can then be done like this: ``` {{ govukSelect({ id: "sort", name: "sort", label: { text: "Sort by" }, items: [ { value: "published", text: "Recently published" }, { value: "updated", text: "Recently updated" }, { value: "views", text: "Most views" }, { value: "comments", text: "Most comments" } ], value: data[‘sort’] }) }} ```
- Loading branch information
Showing
9 changed files
with
148 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters