Skip to content

Commit

Permalink
A11y: add date and time labels in MultiWidget (Z#23132744) (#3718)
Browse files Browse the repository at this point in the history
* A11y: add date and time labels in MultiWidget

* fix code style issues
  • Loading branch information
wiffbi committed Dec 20, 2023
1 parent 7f0ed37 commit 608d82c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/pretix/base/forms/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,10 @@ def time_placeholder():

date_attrs['placeholder'] = lazy(date_placeholder, str)
time_attrs['placeholder'] = lazy(time_placeholder, str)

date_attrs['aria-label'] = _('Date')
time_attrs['aria-label'] = _('Time')
if 'aria-label' in attrs:
del attrs['aria-label']
widgets = (
forms.DateInput(attrs=date_attrs, format=date_format),
forms.TimeInput(attrs=time_attrs, format=time_format),
Expand Down
24 changes: 21 additions & 3 deletions src/pretix/control/forms/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# <https://www.gnu.org/licenses/>.
#
from bootstrap3.text import text_value
from django.forms import CheckboxInput
from django.forms import CheckboxInput, CheckboxSelectMultiple, RadioSelect
from django.forms.utils import flatatt
from django.utils.html import format_html
from django.utils.safestring import mark_safe
Expand All @@ -30,7 +30,7 @@
from pretix.base.forms.renderers import FieldRenderer, InlineFieldRenderer


def render_label(content, label_for=None, label_class=None, label_title='', optional=False):
def render_label(content, label_for=None, label_class=None, label_title='', label_id='', optional=False):
"""
Render a label with content
"""
Expand All @@ -41,6 +41,8 @@ def render_label(content, label_for=None, label_class=None, label_title='', opti
attrs['class'] = label_class
if label_title:
attrs['title'] = label_title
if label_id:
attrs['id'] = label_id

if text_value(content) == '&#160;':
# Empty label, e.g. checkbox
Expand All @@ -61,6 +63,7 @@ class ControlFieldRenderer(FieldRenderer):
def __init__(self, *args, **kwargs):
kwargs['layout'] = 'horizontal'
super().__init__(*args, **kwargs)
self.is_group_widget = isinstance(self.widget, (CheckboxSelectMultiple, RadioSelect, )) or (self.is_multi_widget and len(self.widget.widgets) > 1)

def add_label(self, html):
label = self.get_label()
Expand All @@ -73,14 +76,29 @@ def add_label(self, html):
else:
required = self.field.field.required

if self.is_group_widget:
label_for = ""
label_id = "legend-{}".format(self.field.html_name)
else:
label_for = self.field.id_for_label
label_id = ""

html = render_label(
label,
label_for=self.field.id_for_label,
label_for=label_for,
label_class=self.get_label_class(),
label_id=label_id,
optional=not required and not isinstance(self.widget, CheckboxInput)
) + html
return html

def wrap_label_and_field(self, html):
if self.is_group_widget:
attrs = ' role="group" aria-labelledby="legend-{}"'.format(self.field.html_name)
else:
attrs = ''
return '<div class="{klass}"{attrs}>{html}</div>'.format(klass=self.get_form_group_class(), html=html, attrs=attrs)


class BulkEditMixin:

Expand Down

0 comments on commit 608d82c

Please sign in to comment.