Skip to content

Commit

Permalink
DSFRBaseForm: Set autofocus on the first field with errors (#21)
Browse files Browse the repository at this point in the history
* Set autofocus on the first field with errors

* Update version number for release
  • Loading branch information
Ash-Crow authored Oct 7, 2022
1 parent 68d39bf commit 60243ba
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
11 changes: 10 additions & 1 deletion dsfr/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ def __init__(self, *args, **kwargs):
If a class is already set, we don't force the DSFR-specific classes.
"""
if "class" not in visible.field.widget.attrs:
if type(visible.field.widget) in [forms.widgets.Select, forms.widgets.SelectMultiple]:
if type(visible.field.widget) in [
forms.widgets.Select,
forms.widgets.SelectMultiple,
]:
visible.field.widget.attrs["class"] = "fr-select"
visible.field.widget.group_class = "fr-select-group"
elif type(visible.field.widget) == forms.widgets.RadioSelect:
Expand All @@ -33,3 +36,9 @@ def __init__(self, *args, **kwargs):
visible.field.widget.attrs["dsfr"] = "dsfr"
elif type(visible.field.widget) not in self.WIDGETS_NO_FR_INPUT:
visible.field.widget.attrs["class"] = "fr-input"

# If there are fields in error, set the focus on the first one of them
errorList = list(self.errors)
for item in errorList:
self.fields[item].widget.attrs.update({"autofocus": ""})
break
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ authors = ["Sylvain Boissel <[email protected]>"]
description = "Integrate the French government Design System into a Django app"
license = "MIT"
name = "django-dsfr"
version = "0.9.2"
version = "0.9.3"
classifiers = [
"Development Status :: 3 - Alpha",
"Environment :: Web Environment",
Expand Down

0 comments on commit 60243ba

Please sign in to comment.