Skip to content

Commit 20c1a4e

Browse files
authored
Add crispy template for rich choice select field widget (#660)
This adds a field display type for complex dropdown choices and will be used by the project create team and organization selection field. The only relevant commit is 8b79bd5 - Refs #468 - Requires readthedocs/readthedocs.org#12524 - Based on #658
1 parent 1a31d9b commit 20c1a4e

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

readthedocsext/theme/templates/semantic-ui/field.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
{% load crispy_forms_field %}
1+
{% load is_checkbox from crispy_forms_field %}
22

33
{% if field.is_hidden %}
44
{{ field }}
55
{% else %}
6+
{% comment %}
7+
Checkbox fields are rendered without the base field template because we
8+
don't want the same label structure as normal input fields on checkboxes.
9+
The labels are not over the checkbox, they are to the right side of the
10+
checkbox
11+
{% endcomment %}
612
{% if field|is_checkbox %}
713
{% include "semantic-ui/fields/checkbox.html" with field=field %}
814
{% else %}

readthedocsext/theme/templates/semantic-ui/fields/base.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{% load crispy_field is_checkbox is_select is_checkboxselectmultiple is_radioselect is_file from crispy_forms_field %}
2+
{% load is_rich_select from core_tags %}
23
{% load whitespaceless from ext_theme_tags %}
34

45
{% block field-outer %}
@@ -35,7 +36,10 @@
3536
{% endblock field-label %}
3637

3738
{% block field-input %}
38-
{% if field|is_select %}
39+
{% if field|is_rich_select %}
40+
{% include "semantic-ui/layout/rich_select.html" %}
41+
{% include "semantic-ui/layout/field_errors_block.html" %}
42+
{% elif field|is_select %}
3943
{% include "semantic-ui/layout/dropdown.html" %}
4044
{% include "semantic-ui/layout/field_errors_block.html" %}
4145
{% elif field|is_checkboxselectmultiple %}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{% load crispy_field from crispy_forms_field %}
2+
{% load blocktrans trans from i18n %}
3+
4+
{% comment rst %}
5+
Rich select field template
6+
==========================
7+
8+
This template is used by the :py:class:`RichSelect` widget type to display a
9+
complex dropdown select. Normally, select fields are only a value and text
10+
choice option, but the :py:class:`RichSelect` widget type instead displays a
11+
FUI dropdown type with a number of additions, like an image, right foated
12+
description, and an error description. This gives a field type where we can
13+
provide a reason why the choice is disabled, before the user tries to submit
14+
the form.
15+
16+
To use this template, use the RichSelect class and :py:class:`RichChoice` data class to populate the from choices. A brief example is:
17+
18+
.. code:: python
19+
20+
choice = RichChoice(name="Foo", value="foo", error="Unavailable", image_url="...")
21+
field = forms.ChoiceField(
22+
...,
23+
widget=RichSelect(),
24+
choices=[(choice.value, choice)],
25+
)
26+
27+
{% endcomment %}
28+
29+
<div class="ui fluid selection category search dropdown{% if field.attrs.multiple %} multiple{% endif %}"
30+
data-bind="semanticui: { dropdown: {}}">
31+
{{ field.as_hidden }}
32+
<i class="dropdown icon"></i>
33+
<div class="default text">{{ field.attrs.placeholder }}</div>
34+
<div class="menu">
35+
{% for _, choice in field.field.choices %}
36+
{% comment %}
37+
Text data attribute specifies the value shown when the item is selected
38+
in the dropdown. Normally this is a loose copy of the HTML of the item,
39+
including images, error messages, etc. However, display of the selected
40+
item looks a bit different and attributes like the field error appear
41+
in a confusing way. This will show the selected text only instead, but
42+
the dropdown will still have a rich display.
43+
{% endcomment %}
44+
<div class="{% if choice.disabled %}disabled{% endif %} item"
45+
data-value="{{ choice.value }}"
46+
data-text="{{ choice.text }}">
47+
{% if choice.image_url %}
48+
<img class="ui mini avatar image"
49+
src="{{ choice.image_url }}"
50+
{% if choice.image_alt %}alt="{{ choice.image_alt }}"{% endif %} />
51+
{% endif %}
52+
{% if choice.description %}
53+
<span class="description">{{ choice.description }}</span>
54+
{% endif %}
55+
<span class="text">
56+
{{ choice.text }}
57+
{% if choice.error %}
58+
<span class="ui red text">
59+
<i class="fas fa-exclamation-triangle icon"></i>
60+
{{ choice.error }}
61+
</span>
62+
{% endif %}
63+
</span>
64+
65+
</div>
66+
{% endfor %}
67+
</div>
68+
</div>

0 commit comments

Comments
 (0)