From 514445a76dc6fc8bb906f58775a9743aedd9555b Mon Sep 17 00:00:00 2001 From: Lukas Anzinger Date: Thu, 21 Nov 2024 14:29:47 +0100 Subject: [PATCH] Select widget: Add support for mixture of grouped and non-grouped choices --- CHANGES.rst | 2 ++ src/wtforms/widgets/core.py | 14 ++++++++++---- tests/conftest.py | 24 ++++++++++++++++++++++++ tests/test_widgets.py | 12 ++++++++++++ 4 files changed, 48 insertions(+), 4 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index f34903fc9..82d3f6d4a 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -6,6 +6,8 @@ Version 3.2.1 Released 2024-10-21 - Fix :class:`~fields.SelectMultipleBase` import. :issue:`861` :pr:`862` +- Support for mixture of grouped and non-grouped choices in + :class:`~widgets.Select`. :pr:`870` Version 3.2.0 ------------- diff --git a/src/wtforms/widgets/core.py b/src/wtforms/widgets/core.py index 832918e82..dd000d58e 100644 --- a/src/wtforms/widgets/core.py +++ b/src/wtforms/widgets/core.py @@ -343,7 +343,11 @@ class Select: It also must provide a `has_groups()` method which tells whether choices are divided into groups, and if they do, the field must have an `iter_groups()` method that yields tuples of `(label, choices)`, where - `choices` is a iterable of `(value, label, selected)` tuples. + `choices` is a iterable of `(value, label, selected)` tuples. If `label` + is `None`, the choices are rendered next to the groups. + + .. versionadded:: 3.2.1 + Support for mixture of grouped and ungrouped options. """ validation_attrs = ["required", "disabled"] @@ -363,12 +367,14 @@ def __call__(self, field, **kwargs): html = [f"' + '' + '' + '' + '' + ) + + def test_render_option(): # value, label, selected assert (