mirror of
https://github.com/wagtail/wagtail.git
synced 2024-11-29 09:33:54 +01:00
Introduce id_for_label method on Block
This commit is contained in:
parent
f55b7210ca
commit
4e64a5f3b9
@ -1,6 +1,8 @@
|
||||
{% load wagtailadmin_tags %}
|
||||
<div class="field {{ field|fieldtype }} widget-{{ field|widgettype }}{% if name %} fieldname-{{ name }}{% endif %}{% if classes %} {{ classes }}{% endif %}{% if errors %} error{% endif %}">
|
||||
{{ label_tag }}
|
||||
{% if label %}
|
||||
<label{% if id_for_label %} for="{{ id_for_label }}"{% endif %}>{{ label }}</label>
|
||||
{% endif %}
|
||||
<div class="field-content">
|
||||
<div class="input">
|
||||
{{ widget }}
|
||||
|
@ -272,6 +272,13 @@ class Block(six.with_metaclass(BaseBlock, object)):
|
||||
|
||||
return errors
|
||||
|
||||
def id_for_label(self, prefix):
|
||||
"""
|
||||
Return the ID to be used as the 'for' attribute of <label> elements that refer to this block,
|
||||
when the given field prefix is in use. Return None if no 'for' attribute should be used.
|
||||
"""
|
||||
return None
|
||||
|
||||
def deconstruct(self):
|
||||
# adapted from django.utils.deconstruct.deconstructible
|
||||
module_name = self.__module__
|
||||
|
@ -22,17 +22,12 @@ class FieldBlock(Block):
|
||||
class Meta:
|
||||
default = None
|
||||
|
||||
def id_for_label(self, prefix):
|
||||
return self.field.widget.id_for_label(prefix)
|
||||
|
||||
def render_form(self, value, prefix='', errors=None):
|
||||
widget = self.field.widget
|
||||
|
||||
if self.label:
|
||||
label_html = format_html(
|
||||
"""<label for={label_id}>{label}</label> """,
|
||||
label_id=widget.id_for_label(prefix), label=self.label
|
||||
)
|
||||
else:
|
||||
label_html = ''
|
||||
|
||||
widget_attrs = {'id': prefix, 'placeholder': self.label}
|
||||
|
||||
field_value = self.value_for_form(value)
|
||||
@ -47,9 +42,9 @@ class FieldBlock(Block):
|
||||
return render_to_string('wagtailadmin/block_forms/field.html', {
|
||||
'name': self.name,
|
||||
'label': self.label,
|
||||
'id_for_label': self.id_for_label(prefix),
|
||||
'classes': self.meta.classname,
|
||||
'widget': widget_html,
|
||||
'label_tag': label_html,
|
||||
'field': self.field,
|
||||
'errors': errors if (not widget_has_rendered_errors) else None
|
||||
})
|
||||
|
@ -733,8 +733,8 @@ class TestListBlock(unittest.TestCase):
|
||||
def test_render_form_labels(self):
|
||||
html = self.render_form()
|
||||
|
||||
self.assertIn('<label for=links-0-value-title>Title</label>', html)
|
||||
self.assertIn('<label for=links-0-value-link>Link</label>', html)
|
||||
self.assertIn('<label for="links-0-value-title">Title</label>', html)
|
||||
self.assertIn('<label for="links-0-value-link">Link</label>', html)
|
||||
|
||||
def test_render_form_values(self):
|
||||
html = self.render_form()
|
||||
|
Loading…
Reference in New Issue
Block a user