|
| 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