Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set which item in a select collection is pre-selected? #153

Closed
david-mears opened this issue Jun 30, 2020 · 4 comments · Fixed by #154
Closed

How to set which item in a select collection is pre-selected? #153

david-mears opened this issue Jun 30, 2020 · 4 comments · Fixed by #154

Comments

@david-mears
Copy link

david-mears commented Jun 30, 2020

The Rails helper options_for_select takes a second argument which specifies which collection item is selected, allowing us to dynamically and automatically set it depending on e.g. the user's previous selection, saving them time.

I can't see a way to achieve the same outcome using GovUK Design System Formbuilder. This means that migrating our form to GovUK Design System Formbuilder would be a regression in user experience. Does a way exist to achieve this?

@david-mears david-mears changed the title govuk_selection_collect How to set which item in a select collection is pre-selected? Jun 30, 2020
peteryates added a commit that referenced this issue Jun 30, 2020
The govuk_collection_select method, which is a wrapper for Rails'
collection_select, was missing documentation for the `options` and
`html_options` keyword args. They are passed through to the wrapped
method directly.

The spec covers example use of pre-selecting an option and ensures
attributes are correctly set.

Refs #153
@peteryates
Copy link
Member

Hi David. This is possible but your issue highlighted a shortfall in the docs which I've fixed in dbfdc6c.

I think the cleanest solution is to add your options to a collection object and pass it into #govuk_collection_select along with the preselected id in the options hash. Example here.

Let me know how you get on. If for some reason this approach isn't suitable I think there would be a case for implemeting #govuk_select as a wrapper for #select.

@david-mears
Copy link
Author

Setting options: { selected: foo } works a treat. Thank you. But I have now run up against the issue described here (I do not work on this product):

https://github.com/DFE-Digital/get-help-with-tech/blob/c74f8fda8d4e8b728a277ffc7649d72cbb0d8649/app/webpacker/styles/overrides.scss

Namely, I want to set the class of the resulting select, but something seems to eat the class option somewhere along the way.

I have tried adding class: 'foo' to options and to html_options.

@david-mears
Copy link
Author

david-mears commented Jun 30, 2020

More specifically:

Currently, we apply classes and other attributes (e.g. disabled) to both the .govuk-form-group and the select it contains.

That might cash out to two separate issues: one to be able to add classes to the containing div, and one to add attributes to the select.

We'd like to have both so we can replicate what we already have here (pre-GDS Formbuilder). Both are requirements for GDS Formbuilder to be usable in this case (we use it elsewhere on our service).

We want to be able to do something vaguely like this:

= f.govuk_collection_select :radius,
      radius_filter_options,
      :second,
      :first,
      label: { text: t('jobs.filters.within_radius') },
      # Going off the rails from here onwards
      selected: @vacancies_search.radius,
      disabled: @vacancies_search.disable_radius?,
      div_classes: 'govuk-!-width-full', # applies to the div.govuk-form-group
      select_classes: 'govuk-inset-text' # applies to the select

@peteryates
Copy link
Member

Ah, classes supplied in html_options are being overwritten. That's clearly not correct, fix incoming.

I'm slightly reluctant to add support for setting classes on the form group, it's a much bigger change - we'd want consistent behaviour across all helpers that wrap their content in a FormGroup.

However, if there's a widespread need for it, it can be done.

For the moment I'd suggest wrapping the #govuk_collection_select in an element that you can use to target the CSS:

.full-width-wrapper > .govuk-form-group {
  // blah
}

peteryates added a commit that referenced this issue Jul 1, 2020
Previously the classes were overwritten when the form builder's options
were merged into the supplied HTML options

Refs #153
peteryates added a commit that referenced this issue Jul 1, 2020
Previously the classes were overwritten when the form builder's options
were merged into the supplied HTML options

Refs #153
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants