Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ajout des boutons radio riches #168

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

christophehenry
Copy link
Contributor

@christophehenry christophehenry commented Aug 28, 2024

🎯 Objectif

Voici ma tentative d'ajouter des boutons radio riches. Je suis un peu sceptique sur cette solution parce qu'elle repose sur une nouvelle forme d'enums, plus complexe et qui repose pas mal sur la métaprogrammation, ce qui peut être difficile à maintenir. Mais pour l'instant, c'est la solution la plus user-friendly que j'ai trouvée. L'idée, c'est de l'utiliser comme ça :

from django import forms
from dsfr.forms import DsfrBaseForm
from dsfr.widgets import RichRadioButtonWidget
from django.utils.safestring import mark_safe


class ComplexChoices(RichRadioButtonChoices):
    ITEM_1 = {
        "value": "Item_1",
        "label": "Item 1",
        "pictogram": "/static/images/item1.png",
        "html_label": mark_safe("<span>Item 1</span>"),
    }
    ITEM_2 = {
        "value": "Item_2",
        "label": "Item 2",
        "pictogram": "/static/images/item2.png",
        "html_label": mark_safe("<span>Item 2</span>"),
    }


class ComplexForm(DsfrBaseForm):
    color_full = forms.ChoiceField(
        label="Choisissez une couleur",
        required=False,
        choices=ComplexChoices.choices,
        widget=RichRadioButtonWidget(rich_choices=ComplexChoices),
    )

@christophehenry
Copy link
Contributor Author

Je veux bien un premier retour histoire d'être sûr que je vais dans la bonne direction.

dsfr/models.py Outdated Show resolved Hide resolved
@christophehenry christophehenry force-pushed the rich-radio-buttons branch 2 times, most recently from ed8a084 to 90262f2 Compare September 9, 2024 10:29
dsfr/enums.py Outdated
Comment on lines 57 to 71
if not PY311 and isinstance(value, (list, tuple)):
# Prior to Python 3.11, EnumDict does not interpret auto() when
# wrapped in a tuple so we need to set the value alone a first time
# before wrapping it in a tuple.
if len(value) == 1:
return super().__setitem__(member, value[0])
elif len(value) == 2:
value, label = value
else:
*value, label = value

super().__setitem__(member, value)
dict.__setitem__(self, member, (self[member], label))
return
Copy link
Contributor Author

@christophehenry christophehenry Sep 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Y'a des petites bizarreries dans le support des Choices dans Django mais, en gros, ça :

class TestEnum(IntEnum):
    WEEE = auto(), "Weee"
    OOOH = auto(), "Oooh"

ça crée une exception avant python 3.11… Je sais pas trop pourquoi c'est pas supporté au niveau de django.db.models.enums.ChoicesMeta.

Comment on lines +83 to +91
if PY311:
if "label" in value:
super().__setitem__(
member, (value.pop("value"), value.pop("label"))
)
else:
super().__setitem__(member, value.pop("value"))
else:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pareil que pour #r1750009159. Ça pourra être supprimé quand Django dropera le support de Python 3.11 dans quelques années.

@christophehenry christophehenry force-pushed the rich-radio-buttons branch 2 times, most recently from 312b7e1 to 9c20344 Compare September 9, 2024 12:17
@Ash-Crow
Copy link
Collaborator

Ash-Crow commented Sep 17, 2024

Ça m'a l'air pas mal !

Je note cependant que si on valide le formulaire et qu'il est réaffiché (par exemple, s'il y a des erreurs de validation sur d'autres champs), les valeurs ne sont pas conservées.

Edit: tu vas devoir rebase, j'ai vu au passage et corrigé un bug sur le champ précédent (checkbox multiple…)

@Ash-Crow Ash-Crow linked an issue Sep 17, 2024 that may be closed by this pull request
class RichRadioSelect(_RichChoiceWidget, RadioSelect):
"""
Widget for producing riche radio buttons. This widget works with
`dsfr.enums.RichRadioButtonChoices`.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Est-ce que c'est possible de faire un lien vers la documentation de ce composant, ici ?

dsfr/widgets.py Outdated
## `html_label`

The `html_label` instance member can be used to put HTML code inside `<label>`. It
is automatically marked as HTML-safe with [`django.utils.safestring.mark_safe`][1]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Est-ce que la forme

[lien`][1]
[1]: https://

fonctionne dans la doc ?

dsfr/enums.py Outdated
... COLD = {"value": "COLD"}
```

See [this section on how to provide default values for additionnal attributes](#default-values)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

J'essaie de faire une référence vers une autre section de la doc de ce composant. Ça fonctionne ?

dsfr/enums.py Outdated
... }
```

See `dsfr.widgets.RichRadioSelect` for more details.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C'est possible de faire un réf à la doc de ce composant ici ?

@christophehenry
Copy link
Contributor Author

Je note cependant que si on valide le formulaire et qu'il est réaffiché (par exemple, s'il y a des erreurs de validation sur d'autres champs), les valeurs ne sont pas conservées.

C'est bon, c'est réglé.

@christophehenry
Copy link
Contributor Author

J'ai traduit la doc en français, plutôt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ajouter les boutons radio riches dans les formulaires
2 participants