Skip to content

Commit

Permalink
feat: added select2 widget
Browse files Browse the repository at this point in the history
  • Loading branch information
tymees committed Dec 7, 2023
1 parent 3b19d5d commit 1cc2c2f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
14 changes: 14 additions & 0 deletions dev/main/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@ class FormStylesForm(forms.Form):
widget=forms.RadioSelect,
)

searchable_select = forms.ChoiceField(
choices=[
(1, "Train"),
(2, "Bus"),
(3, "Aeroplane"),
(4, "Bike"),
(5, "Feet"),
(6, "Magical Unicorn"),
(6, "Broom"),
(6, "Thestrals"),
],
widget=core_fields.SearchableSelectWidget,
)

integer = forms.IntegerField()

float = forms.FloatField(help_text="Floating away")
Expand Down
18 changes: 18 additions & 0 deletions src/cdh/core/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,24 @@ def get_context(self, name, value, attrs):
return super().get_context(name, value, attrs)


class SearchableSelectWidget(BootstrapSelect):
"""A JS-baced widget for a searchable select. Currently using Select2 as
the backend."""

class Media:
js = [
'cdh.core/js/widget/searchable-select.js',
]

def get_context(self, name, value, attrs):
if "class" not in attrs:
attrs["class"] = ""

attrs["class"] += " dsc-select2"

return super().get_context(name, value, attrs)


class BootstrapCheckboxInput(CheckboxInput):
"""Override of Django's version to use the right Bootstrap classes"""

Expand Down
3 changes: 3 additions & 0 deletions src/cdh/core/static/cdh.core/js/widget/searchable-select.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$(() => {
$('.dsc-select2').select2();
})

0 comments on commit 1cc2c2f

Please sign in to comment.