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

B5 Responsive Forms #35519

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from
23 changes: 3 additions & 20 deletions corehq/apps/hqwebapp/crispy.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import re
from contextlib import contextmanager
from django.forms.widgets import DateTimeInput
from django.template.loader import render_to_string
Expand All @@ -14,9 +13,7 @@
from crispy_forms.utils import flatatt, get_template_pack, render_field

CSS_LABEL_CLASS = 'field-label'
CSS_LABEL_CLASS_BOOTSTRAP5 = 'field-label'
CSS_FIELD_CLASS = 'field-control'
CSS_FIELD_CLASS_BOOTSTRAP5 = 'field-control'
CSS_ACTION_CLASS = CSS_FIELD_CLASS


Expand All @@ -28,12 +25,7 @@ class HQFormHelper(FormHelper):
def __init__(self, *args, **kwargs):
super(HQFormHelper, self).__init__(*args, **kwargs)
from corehq.apps.hqwebapp.utils.bootstrap import get_bootstrap_version, BOOTSTRAP_5
bootstrap_version = get_bootstrap_version()
use_bootstrap5 = bootstrap_version == BOOTSTRAP_5
if use_bootstrap5:
self.label_class = CSS_LABEL_CLASS_BOOTSTRAP5
self.field_class = CSS_FIELD_CLASS_BOOTSTRAP5
self.use_bootstrap5 = use_bootstrap5
self.use_bootstrap5 = get_bootstrap_version() == BOOTSTRAP_5

if 'autocomplete' not in self.attrs:
self.attrs.update({
Expand Down Expand Up @@ -75,15 +67,7 @@ class ErrorsOnlyField(Field):


def get_form_action_class():
"""This is only valid for bootstrap 5"""
return CSS_FIELD_CLASS_BOOTSTRAP5


def _get_offsets(context):
label_class = context.get('label_class', '')
use_bootstrap5 = context.get('use_bootstrap5')
return (label_class.replace('col', 'offset') if use_bootstrap5
else re.sub(r'(xs|sm|md|lg)-', r'\g<1>-offset-', label_class))
return CSS_FIELD_CLASS


class FormActions(OriginalFormActions):
Expand All @@ -101,11 +85,10 @@ def render(self, form, context, template_pack=None, **kwargs):
template_pack=template_pack,
)
fields_html = mark_safe(fields_html) # nosec: just concatenated safe fields
offsets = _get_offsets(context)
context.update({
'formactions': self,
'fields_output': fields_html,
'offsets': offsets,
'offsets': '',
'field_class': context.get('field_class', '')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: can offsets be removed altogether form this dict?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mabye, I'll take another look. I'm not sure how many places use (require?) it. It's hard to search for because it's a common word. I also need to check if crispy-boostrap3to5 needs it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

})
return render_to_string(self.template, context.flatten())
Expand Down
10 changes: 2 additions & 8 deletions corehq/apps/hqwebapp/templatetags/hq_shared_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,19 +246,13 @@ def can_use_restore_as(request):

@register.simple_tag
def css_label_class():
from corehq.apps.hqwebapp.crispy import CSS_LABEL_CLASS, CSS_LABEL_CLASS_BOOTSTRAP5
from corehq.apps.hqwebapp.utils.bootstrap import get_bootstrap_version, BOOTSTRAP_5
if get_bootstrap_version() == BOOTSTRAP_5:
return CSS_LABEL_CLASS_BOOTSTRAP5
from corehq.apps.hqwebapp.crispy import CSS_LABEL_CLASS
return CSS_LABEL_CLASS


@register.simple_tag
def css_field_class():
from corehq.apps.hqwebapp.crispy import CSS_FIELD_CLASS, CSS_FIELD_CLASS_BOOTSTRAP5
from corehq.apps.hqwebapp.utils.bootstrap import get_bootstrap_version, BOOTSTRAP_5
if get_bootstrap_version() == BOOTSTRAP_5:
return CSS_FIELD_CLASS_BOOTSTRAP5
from corehq.apps.hqwebapp.crispy import CSS_FIELD_CLASS
return CSS_FIELD_CLASS


Expand Down
Loading