Skip to content
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

Select field not resizable #13

Open
0x4A-0x41-0x4B opened this issue Mar 26, 2023 · 2 comments
Open

Select field not resizable #13

0x4A-0x41-0x4B opened this issue Mar 26, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@0x4A-0x41-0x4B
Copy link

When rendering a select field its css_class and wrapper_class can be changed.
But its "select" div stays the same and can't be changed without overriding the template.

Current "select.html" template:

{% load crispy_forms_bulma_field %}

<div class="control">
  <div class="select{% if field|is_selectmultiple %} is-multiple{%endif%}{% if field.errors %} is-danger{% endif %}">
    {% crispy_field field %}
  </div>
</div>

If you want to make the select field take up the full width, for example, the control and input field css_class can be set, but the select field will still stay the same and refuse to take the available space. Therefore stopping the input field from actually expanding.

Perhaps i'm just overlooking something with bulma, but as i see it you can't really make a select field take up the full width without overriding the template or creating an unnecessary css rule.

@ckrybus
Copy link
Owner

ckrybus commented Mar 26, 2023

Hi @0x4A-0x41-0x4B,

you are right, this is not supported yet. I've been using a custom field and template as a workaround for a while (I'm undecided if this is the right approach, that's why it's not in main yet):

form_helper.py

from crispy_forms.layout import Field

class SelectField(Field):
    def __init__(self, *args, **kwargs):
        self.control_class = kwargs.pop("control_class", None)
        self.select_class = kwargs.pop("select_class", None)
        super().__init__(*args, **kwargs)

    def render(self, *args, **kwargs):
        extra_context = kwargs.get("extra_context", {})

        if self.control_class:
            extra_context["control_class"] = self.control_class
        if self.select_class:
            extra_context["select_class"] = self.select_class

        kwargs["extra_context"] = extra_context
        return super().render(*args, **kwargs)

bulma/layout/select.html

{% load crispy_forms_bulma_field %}

<div class="control{% if control_class %} {{ control_class }}{% endif %}">
  <div class="select{% if field|is_selectmultiple %} is-multiple{%endif%}{% if select_class %} {{ select_class }}{% endif %}{% if field.errors %} is-danger{% endif %}">
    {% crispy_field field %}
  </div>
</div>

And then in the form:

self.helper.layout = Layout(
    (...)
    SelectField(
        "fieldname",
        control_class="is-expanded",
        select_class="is-fullwidth",
    ),
    (...)
)

@ckrybus
Copy link
Owner

ckrybus commented Mar 26, 2023

Perhaps i'm just overlooking something with bulma, but as i see it you can't really make a select field take up the full width without overriding the template or creating an unnecessary css rule.

the trick is to use both "is-expanded" and "is-fullwidth"

@ckrybus ckrybus added the enhancement New feature or request label Dec 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants