Skip to content

SelectField choice refactoring - #739

Merged
azmeuk merged 1 commit into
pallets-eco:masterfrom
azmeuk:select-option-customization
Oct 5, 2023
Merged

azmeuk merged 1 commit into
pallets-eco:masterfrom
azmeuk:select-option-customization

Conversation

@azmeuk

@azmeuk azmeuk commented Apr 28, 2022

Copy link
Copy Markdown
Member

Fixes #692. Related to #381.

SelectField choice argument can take a list of 3 items tuples, the last one being a dict that will be rendered as <option> HTML parameter.

@azmeuk
azmeuk force-pushed the select-option-customization branch 3 times, most recently from 2c7d45f to 3a1d060 Compare April 28, 2022 19:55
@azmeuk azmeuk added the enhancement New feature, or existing feature improvement label May 3, 2022
@kontur

kontur commented Nov 7, 2022

Copy link
Copy Markdown

This would be very useful 👍

@azmeuk
azmeuk force-pushed the select-option-customization branch from 3a1d060 to 27515f4 Compare December 24, 2022 11:59
@azmeuk

azmeuk commented Jan 13, 2023

Copy link
Copy Markdown
Member Author

@davidism does this sound safe to you?

@azmeuk
azmeuk force-pushed the select-option-customization branch from 27515f4 to 99c0a64 Compare January 13, 2023 14:31
@davidism

davidism commented Jan 13, 2023 •

Copy link
Copy Markdown
Member

Seems good to me. Instead of passing longer tuples, you might want to consider a SelectChoice dataclass with the fields, which might make typing easier later.

@azmeuk

azmeuk commented Jan 13, 2023

Copy link
Copy Markdown
Member Author

Do you mean we should break the compatibility and expect the choices parameter to be a collection of SelectChoice instead of a collection of tuples?
I suppose this would make the code more explicit and easier to read though.

@davidism

Copy link
Copy Markdown
Member

Don't add a 3-tuple data type. Instead, only support this new data in the new dataclass. If you get a string, or a 2-tuple, convert it to the dataclass.

Since choices can be set after init, I'd do this conversion in the render step for now (and maybe in init as well). I'd probably keep the string shortcut, but show a deprecation warning for 2-tuples like "Passing (value, label) is deprecated, pass SelectChoice(...) instead.".

@azmeuk
azmeuk force-pushed the select-option-customization branch 4 times, most recently from 5148434 to 416842a Compare July 22, 2023 19:20
@azmeuk

azmeuk commented Jul 22, 2023

Copy link
Copy Markdown
Member Author

Sorry for the noise with the force pushes, that should be good now.

I refactored the SelectField by adding a Choice dataclass. I deprecated passing choices by tuples or dicts. The Choice dataclass carry the optgroup and render_kw.
This was a good idea @davidism I did not realized of much this would simplify the SelectField behavior 👍

I could use another review 🙏

@azmeuk azmeuk changed the title <option> HTML parameters can be set in SelectField choices argument SelectField choice refactoring Jul 22, 2023
@azmeuk
azmeuk force-pushed the select-option-customization branch from 416842a to cc721a9 Compare July 28, 2023 19:18
Comment thread src/wtforms/fields/choices.py Resolved
@azmeuk
azmeuk force-pushed the select-option-customization branch 2 times, most recently from f9337a5 to 5bc3653 Compare October 5, 2023 09:41
@azmeuk
azmeuk force-pushed the select-option-customization branch from 5bc3653 to fada111 Compare October 5, 2023 09:41
@azmeuk
azmeuk merged commit a6f44f0 into pallets-eco:master Oct 5, 2023
@azmeuk
azmeuk deleted the select-option-customization branch October 5, 2023 09:45
azmeuk added a commit that referenced this pull request Oct 11, 2023
@aminalaee aminalaee mentioned this pull request Oct 11, 2023
@coleifer

Copy link
Copy Markdown

This breaks a few libraries, including wtf-peewee which passes two-tuples.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature, or existing feature improvement

Development

Successfully merging this pull request may close these issues.

SelectField - can choices tuples be expanded to allow for tooltips

5 participants