Skip to content

Added SELECT2_THEME settings #103

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

Merged
merged 1 commit into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions django_select2/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ class Select2Conf(AppConf):

SELECT2_CSS = 'assets/css/select2.css'

If you want to add more css (usually used in select2 themes), add a line
in settings.py like this::

SELECT2_CSS = [
'assets/css/select2.css',
'assets/css/select2-theme.css',
]

If you provide your own CSS and would not like Django-Select2 to load any, change
this setting to a blank string like so::

Expand All @@ -96,6 +104,14 @@ class Select2Conf(AppConf):
develop without an Internet connection.
"""

THEME = "default"
"""
Select2 supports custom themes using the theme option so you can style Select2
to match the rest of your application.

.. tip:: When using other themes, you may need use select2 css and theme css.
"""

I18N_PATH = (
"https://cdnjs.cloudflare.com/ajax/libs/select2/{version}/js/i18n".format(
version=LIB_VERSION
Expand Down
18 changes: 10 additions & 8 deletions django_select2/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def build_attrs(self, base_attrs, extra_attrs=None):
default_attrs = {
"lang": self.i18n_name,
"data-minimum-input-length": 0,
"data-theme": settings.SELECT2_THEME,
}
if self.is_required:
default_attrs["data-allow-clear"] = "false"
Expand Down Expand Up @@ -120,18 +121,19 @@ def media(self):
.. Note:: For more information visit
https://docs.djangoproject.com/en/stable/topics/forms/media/#media-as-a-dynamic-property
"""
select2_js = (settings.SELECT2_JS,) if settings.SELECT2_JS else ()
select2_css = (settings.SELECT2_CSS,) if settings.SELECT2_CSS else ()
select2_js = [settings.SELECT2_JS] if settings.SELECT2_JS else []
select2_css = settings.SELECT2_CSS if settings.SELECT2_CSS else []

i18n_file = ()
if isinstance(select2_css, str):
select2_css = [select2_css]

i18n_file = []
if self.i18n_name in settings.SELECT2_I18N_AVAILABLE_LANGUAGES:
i18n_file = (
("%s/%s.js" % (settings.SELECT2_I18N_PATH, self.i18n_name),)
)
i18n_file = [f"{settings.SELECT2_I18N_PATH}/{self.i18n_name}.js"]

return forms.Media(
js=select2_js + i18n_file + ("django_select2/django_select2.js",),
css={"screen": select2_css + ("django_select2/django_select2.css",)},
js=select2_js + i18n_file + ["django_select2/django_select2.js"],
css={"screen": select2_css + ["django_select2/django_select2.css"]},
)


Expand Down
17 changes: 17 additions & 0 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ def test_i18n(self):
"django_select2/django_select2.js",
)

def test_theme_setting(self, settings):
settings.SELECT2_THEME = "classic"
widget = self.widget_cls()
assert 'data-theme="classic"' in widget.render("name", None)


class TestSelect2MixinSettings:
def test_default_media(self):
Expand Down Expand Up @@ -209,6 +214,13 @@ def test_empty_css_setting(self, settings):
result = sut.media.render()
assert "/select2.css" not in result

def test_multiple_css_setting(self, settings):
settings.SELECT2_CSS = ["select2.css", "select2-theme.css"]
sut = Select2Widget()
result = sut.media.render()
assert "select2.css" in result
assert "select2-theme.css" in result


class TestHeavySelect2Mixin(TestSelect2Mixin):
url = reverse("heavy_select2_widget")
Expand Down Expand Up @@ -321,6 +333,11 @@ class NoPickle:
with pytest.raises(NotImplementedError):
widget.set_to_cache()

def test_theme_setting(self, settings):
settings.SELECT2_THEME = "classic"
widget = self.widget_cls(data_view="heavy_data_1")
assert 'data-theme="classic"' in widget.render("name", None)


class TestModelSelect2Mixin(TestHeavySelect2Mixin):
form = forms.AlbumModelSelect2WidgetForm(initial={"primary_genre": 1})
Expand Down