Skip to content

Commit

Permalink
feat: add searchable combo-box
Browse files Browse the repository at this point in the history
  • Loading branch information
kennedykori committed Sep 2, 2021
1 parent e8ec0e2 commit a74394b
Show file tree
Hide file tree
Showing 14 changed files with 3,317 additions and 15,331 deletions.
3 changes: 3 additions & 0 deletions fahari/common/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from .dashboard import get_fahari_facilities_queryset
from .models import Facility, FacilityAttachment, FacilityUser, Organisation, System
from .widgets import SearchableComboBox


class BaseModelForm(ModelForm):
Expand Down Expand Up @@ -101,6 +102,7 @@ class FacilityAttachmentForm(BaseModelForm):
class Meta:
model = FacilityAttachment
fields = "__all__"
widgets = {"facility": SearchableComboBox()}


class FacilityUserForm(BaseModelForm):
Expand All @@ -118,3 +120,4 @@ def __init__(self, *args, **kwargs):
class Meta(BaseModelForm.Meta):
model = FacilityUser
fields = "__all__"
widgets = {"facility": SearchableComboBox()}
20 changes: 20 additions & 0 deletions fahari/common/widgets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""
Custom widgets to supplement/replace existing widgets.
"""
from django import forms


class SearchableComboBox(forms.Select):
"""A combo box with search capabilities.
This is suitable as a replacement for select with many options.
"""

input_type = "select"
option_inherits_attrs = False

def build_attrs(self, base_attrs, extra_attrs=None):
# The `data-live-search` attribute is needed to enable the search
# functionality.
base_attrs.update({"data-live-search": "true"})
return super().build_attrs(base_attrs, extra_attrs)
8 changes: 8 additions & 0 deletions fahari/ops/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from fahari.common.dashboard import get_fahari_facilities_queryset
from fahari.common.forms import BaseModelForm
from fahari.common.models import FacilityUser
from fahari.common.widgets import SearchableComboBox

from .models import (
ActivityLog,
Expand Down Expand Up @@ -35,6 +36,9 @@ def __init__(self, *args, **kwargs):

class Meta(BaseModelForm.Meta):
model = FacilitySystem
widgets = {
"facility": SearchableComboBox(),
}


class FacilitySystemTicketForm(BaseModelForm):
Expand Down Expand Up @@ -68,6 +72,7 @@ def __init__(self, *args, **kwargs):
class Meta(BaseModelForm.Meta):
model = FacilitySystemTicket
widgets = {
"facility_system": SearchableComboBox(),
"raised": DateTimeInput(
attrs={
"readonly": "readonly",
Expand Down Expand Up @@ -139,6 +144,7 @@ def __init__(self, *args, **kwargs):
class Meta(BaseModelForm.Meta):
model = StockReceiptVerification
widgets = {
"facility": SearchableComboBox(),
"pack_size": TextInput(
attrs={
"size": 128,
Expand Down Expand Up @@ -236,6 +242,7 @@ class Meta(BaseModelForm.Meta):
"type": "date",
}
),
"site": SearchableComboBox(),
}


Expand All @@ -262,6 +269,7 @@ def __init__(self, *args, **kwargs):
class Meta(BaseModelForm.Meta):
model = DailyUpdate
widgets = {
"facility": SearchableComboBox(),
"date": TextInput(
attrs={
"type": "date",
Expand Down
4 changes: 4 additions & 0 deletions fahari/static/css/project.css
Original file line number Diff line number Diff line change
Expand Up @@ -11447,3 +11447,7 @@ h3 {
.row.align-header {
display: grid !important;
}

.dropdown-menu.show {
max-width: 80%;
}
2 changes: 1 addition & 1 deletion fahari/static/css/project.min.css

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions fahari/static/js/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@
$(".navbar-collapse a").click(function() {
$(".navbar-collapse").collapse("hide");
});

// Initialize the bootstrap select plugin
$("select").selectpicker();
})(jQuery);
4 changes: 2 additions & 2 deletions fahari/static/js/project.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a74394b

Please sign in to comment.