Repository navigation
Add wrapper=False to render a field without its wrapper #882
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
Open
shreyanshj10
wants to merge
7
commits into
zostera:main
Choose a base branch
from
shreyanshj10:fix/issue-438-optional-wrapper
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0004da0
Add wrapper=False to render a field without its wrapper
shreyanshj10 02bb036
Merge remote-tracking branch 'origin/main' into pr882f
dyve 8cf47ff
Keep labels out of the input-group example
shreyanshj10 e96e03e
Merge remote-tracking branch 'upstream/main' into fix/issue-438-optio…
shreyanshj10 2b91e21
Merge remote-tracking branch 'upstream/main' into fix/issue-438-optio…
shreyanshj10 7ba59a5
Merge main, and give each field in an input group its own accessible …
dyve b741e65
Merge main after #879
dyve File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -50,6 +50,53 @@ Bootstrap 5 offers Input groups to combine fields and add-ons (both before and a | |||||||||||||||||||||
| Note: input-group needs has-validation | ||||||||||||||||||||||
| https://github.com/twbs/bootstrap/blob/6b3254536bac263c39e3a536c3c13945210d91b2/site/content/docs/5.0/migration.md | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| To put more than one field in a single input group, render the fields without their wrappers by | ||||||||||||||||||||||
| passing ``wrapper=False``. Bootstrap lays out every child of an input group as a flex item, so a | ||||||||||||||||||||||
| field's own label would be stretched into the row instead of sitting above it. | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Each input still needs an accessible name. A single ``<label>`` names one control, so a shared label | ||||||||||||||||||||||
| outside the group leaves the other fields without one, and assistive technology falls back to the | ||||||||||||||||||||||
| ``placeholder``, which disappears as soon as the field has content. Give each field its own | ||||||||||||||||||||||
| ``aria-label`` and put the shared, readable text in an ``input-group-text``, as Bootstrap's own | ||||||||||||||||||||||
| "Multiple inputs" example does. ``bootstrap_field`` has no ``attrs`` argument, so the attribute goes | ||||||||||||||||||||||
| on the widget: | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| .. code:: python | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| class NameForm(forms.Form): | ||||||||||||||||||||||
| first = forms.CharField(widget=forms.TextInput(attrs={"aria-label": "First name"})) | ||||||||||||||||||||||
| last = forms.CharField(widget=forms.TextInput(attrs={"aria-label": "Last name"})) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| .. code:: html | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| <div class="input-group"> | ||||||||||||||||||||||
| <span class="input-group-text">First and last name</span> | ||||||||||||||||||||||
| {% bootstrap_field form.first wrapper=False show_label='skip' %} | ||||||||||||||||||||||
| {% bootstrap_field form.last wrapper=False show_label='skip' %} | ||||||||||||||||||||||
| </div> | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Each field then renders its widget, errors and help text, but no wrapper ``div`` and no label: | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| .. code:: html | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| <div class="input-group"> | ||||||||||||||||||||||
| <span class="input-group-text">First and last name</span> | ||||||||||||||||||||||
| <input type="text" name="first" aria-label="First name" class="form-control" id="id_first"> | ||||||||||||||||||||||
| <input type="text" name="last" aria-label="Last name" class="form-control" id="id_last"> | ||||||||||||||||||||||
| </div> | ||||||||||||||||||||||
|
Comment on lines
+82
to
+86
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Matching the input above, and matching what Bootstrap's input-group examples actually look like. |
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Where the group holds a single field, keep a real label instead. Bootstrap puts it before the group | ||||||||||||||||||||||
| rather than inside it, so ``show_label='skip'`` on the field and your own | ||||||||||||||||||||||
| ``<label class="visually-hidden" for="...">`` above the ``div`` matches what Bootstrap documents. | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| The classes the wrapper carried are not rendered anywhere, because the element that carried them is | ||||||||||||||||||||||
| gone: ``wrapper_class``, the ``row`` class of the horizontal layout, and the ``error_css_class``, | ||||||||||||||||||||||
| ``success_css_class`` and ``required_css_class`` indicator classes. The element you wrap the fields in | ||||||||||||||||||||||
| is yours, so put what you need on it, including ``has-validation`` when the group contains a field that | ||||||||||||||||||||||
| can show validation feedback. The ``is-valid``/``is-invalid`` classes on the widgets themselves are | ||||||||||||||||||||||
| unaffected. With ``layout="floating"``, the widget and its label keep their own ``form-floating`` | ||||||||||||||||||||||
| element inside the group, as Bootstrap's floating labels require. | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Floating labels | ||||||||||||||||||||||
| --------------- | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The group needs a label of its own, outside it, since the fields no longer render one. Once #879 lands with the
renderers.pyfix,show_label='visually-hidden'is the better value here: it keeps a per-field label for screen readers without disturbing the flex layout.