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

💄 [#1556] Styling for selects with empty option selected #796

Merged
merged 1 commit into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/open_inwoner/scss/components/Form/Select.scss
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,8 @@ select {
option:checked {
color: var(--font-color-body);
}

&.muted {
color: var(--color-mute);
}
}
5 changes: 5 additions & 0 deletions src/open_inwoner/templates/django/forms/widgets/select.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<select name="{{ widget.name }}" {% if widget.value.0 == "" %}class="{{widget.attrs.class}} muted"{% endif %}{% include "django/forms/widgets/attrs.html" %}>{% for group_name, group_choices, group_index in widget.optgroups %}{% if group_name %}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't seem to be possible to style a <select> based on the selected <option> using CSS and I didn't want to create a custom widget that would have to be set on each instance of ...ChoiceField, so I opted for this approach instead

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: We put all our Form Component templates in this project in this particular directory here: src/open_inwoner/components/templates/components/Form
So I don't think we should put anything into the Django templates Dir.

Also: with this approach we need to create a 1st option inside all Selects that should start with some kind of empty default, like here:
http://127.0.0.1:8000/mijn-uitkeringen/
So I guess you should add one there :-)
Not sure what to call it, perhaps "Select month".
Same for:
http://127.0.0.1:8000/mijn-uitkeringen/jaaropgaven/ "Select year"

More pages with selects, that are probably allright:
http://127.0.0.1:8000/mijn-profiel/contacts/
http://127.0.0.1:8000/mijn-profiel/actions/
And ">> Mijn profiel >> Mijn acties >> Bewerk [Actie title]"

benefits-select

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also: with this approach we need to create a 1st option inside all Selects that should start with some kind of empty default, like here:

Just to be sure: do you mean that having Sep 2023 not muted as the initial value would be okay as well? I could modify the widget to check if the initial value is empty or not and apply the class with the muted styling based on that

More pages with selects, that are probably allright:
http://127.0.0.1:8000/mijn-profiel/contacts/

I checked the contacts page, the change I mentioned above would cause "Alle" to be displayed muted, because it's value is "". Is this correct?
image

Note: We put all our Form Component templates in this project in this particular directory here: src/open_inwoner/components/templates/components/Form
So I don't think we should put anything into the Django templates Dir.

I put the template in templates/django/forms/widgets/select.html to override the default widget that Django uses under the hood, which makes this change available for all selects without having to manually add classes to each occurring select. I'm not sure if there is another way to override a default Django widget without having to monkey patch stuff, which seems like it would be unnecessary in this case in my opinion.

If it turns out we do not want this styling applied to each select or if there is no generic solution, the only solution is probably to add the muted class to all selects manually (which could be kind of a chore, I'm not sure how many selects there are 😛)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed: the first option in the selects in the Maandopgaven/Jaaropgaven should not be muted because it's already showing a real option - the muted style is a UX indication, so should only be applied to a default option to indicate to the user something can be selected/filtered.
I'm not sure anymore but I tried to tackle this with CSS-only a couple of months ago and I think I found that some selects weren't using the value attribute at all, which made them empty, which meant that too many of them had a zero value so it would show the mute style too often.

In my previous answer I posted all of the places that have a Select; so currently i'd think there's only a maximum of 6 selects in the entire project.
And yes: I'd say 'alle' for Contacts should be muted because it means nothing has been filtered by the select yet.

As for the location of the templates and if we just simply wish to override: I am not sure - I'd have to ask @alextreme or @Bartvaderkin.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my previous answer I posted all of the places that have a Select; so currently i'd think there's only a maximum of 6 selects in the entire project.

Ah alright, in that case I think all of them are displayed correctly:

image

image

image

image

image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that's true then i will approve this :-) for new selects we'll need to make a correct model with values I guess.

<optgroup label="{{ group_name }}">{% endif %}{% for option in group_choices %}
{% include option.template_name with widget=option %}{% endfor %}{% if group_name %}
</optgroup>{% endif %}{% endfor %}
</select>
Loading