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

Append custom custom classes to labels #397

Merged
merged 2 commits into from
Nov 9, 2022
Merged

Conversation

cpjmcquillan
Copy link
Collaborator

Before, custom classes passed in the label hash would override any existing classes.

For example:

<%= form_with model: @model do |f| %>
  <%= f.govuk_text_area :details, label: { text: "Some details", class: "special-class" } %>
<% end %>

would output:

<!-- form markup --!>

<label for="model-details-field" class="special-class">
  Some details
</label>

<!-- form markup --!>

Taking into consideration how the rest of the library behaves, this behvaiour was unexpected and meant users would need to explicitly provide default label classes.

This change makes use of the HTMLAttributes and HTMLClasses traits so that label elements build classes as usual —except any custom classes are appended.

For example:

<%= form_with model: @model do |f| %>
  <%= f.govuk_text_area :details, label: { text: "Some details", class: "special-class" } %>
<% end %>

would now output:

<!-- form markup --!>

<label for="model-details-field" class="govuk-label special-class">
  Some details
</label>

<!-- form markup --!?

This change in behaviour will affect users of the gem who are customising label classes but don't want default classes to be applied.

I'm not sure if that is something we need to worry about though, since the form builder is intended to build GOV.UK (or other brand) forms.

Before, custom classes passed in the `label` hash would override any existing
classes.

For example:

```erb
<%= form_with model: @model do |f| %>
  <%= f.govuk_text_area :details, label: { text: "Some details", class: "special-class" } %>
<% end %>
```

would output:

```html
<!-- form markup --!>

<label for="model-details-field" class="special-class">
  Some details
</label>

<!-- form markup --!>
```

Taking into consideration how the rest of the library behaves, this behvaiour
was unexpected and meant users would need to explicitly provide default label
classes.

This change makes use of the `HTMLAttributes` and `HTMLClasses` traits so that label
elements build classes as usual —except any custom classes are appended.

For example:

```erb
<%= form_with model: @model do |f| %>
  <%= f.govuk_text_area :details, label: { text: "Some details", class: "special-class" } %>
<% end %>
```

would now output:

```html
<!-- form markup --!>

<label for="model-details-field" class="govuk-label special-class">
  Some details
</label>

<!-- form markup --!?
```

This change in behaviour will affect users of the gem who are customising label
classes but _don't_ want default classes to be applied.

I'm not sure if that is something we need to worry about though, since the form
builder is intended to build GOV.UK (or other brand) forms.
@netlify
Copy link

netlify bot commented Nov 4, 2022

👷 Deploy Preview for govuk-form-builder processing.

Name Link
🔨 Latest commit 63ae6b8
🔍 Latest deploy log https://app.netlify.com/sites/govuk-form-builder/deploys/636663df6a2d740008e03544

@cpjmcquillan
Copy link
Collaborator Author

Hey @peteryates

I'm lucky enough to be back using this wonderful gem, and I found myself banging my head just now trying to figure out why all the default classes were being overriden 😆.

Is there a reason the existing behaviour was the way it was?

I'd also be interested to know how this might impact other users.

Let me know what you think, and if you need anything from me!

@peteryates
Copy link
Member

I think I just missed it 😞

Amazed nobody's raised a bug in all that time. Will properly review and get this merged over the weekend.

Thanks for the fix, glad to have you back!

Continuing from the prevoius commit, this change makes the additional
classes passed in via the hint, legend and caption hashes add to the
default rather than overwrite it. This makes it more consistent with the
behaviour in the rest of the library.
@peteryates
Copy link
Member

Perfect, thanks @cpjmcquillan. While reviewing I noticed that legends, captions and hints suffered from the same problem, so I followed up the commit with one that extends your implementation to them too.

@cpjmcquillan
Copy link
Collaborator Author

Nice one @peteryates, thanks.

I'm not sure what your plans are for releasing this.

Do you want me to test this branch on our application to check for any regressions?

I'm happy to just fire it off into the world and see what happens though 😆

@peteryates
Copy link
Member

I'll try and get a release out tomorrow, should be able to find a bit of time.

@peteryates peteryates merged commit 0f5a1c5 into main Nov 9, 2022
@peteryates peteryates deleted the cm/label-html-classes branch November 9, 2022 18:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants