Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Changed country area choices to datalist

primal10067a7de0fe370
diff --git a/saleor/userprofile/i18n.py b/saleor/userprofile/i18n.py
index c48ef3226d..0599c08915 100644
--- a/saleor/userprofile/i18n.py
+++ b/saleor/userprofile/i18n.py
@@ -9,7 +9,7 @@ from phonenumber_field.formfields import PhoneNumberField
from .models import Address
from .validators import validate_possible_number
-from .widgets import PhonePrefixWidget
+from .widgets import PhonePrefixWidget, DatalistTextWidget
COUNTRY_FORMS = {}
UNKNOWN_COUNTRIES = set()
@@ -51,6 +51,9 @@ class PossiblePhoneNumberFormField(PhoneNumberField):
def to_python(self, value):
return value
+class CountryAreaChoiceField(forms.ChoiceField):
+ widget = DatalistTextWidget
+
class AddressMetaForm(forms.ModelForm):
# This field is never visible in UI
@@ -183,7 +186,7 @@ def get_form_i18n_lines(form_instance):
def update_base_fields(form_class, i18n_rules):
if i18n_rules.country_area_choices:
- form_class.base_fields['country_area'] = forms.ChoiceField(choices=i18n_rules.country_area_choices)
+ form_class.base_fields['country_area'] = CountryAreaChoiceField(choices=i18n_rules.country_area_choices)
labels_map = {
'country_area': i18n_rules.country_area_type,
diff --git a/saleor/userprofile/widgets.py b/saleor/userprofile/widgets.py
index 643c524447..5a6028d251 100644
--- a/saleor/userprofile/widgets.py
+++ b/saleor/userprofile/widgets.py
@@ -22,3 +22,30 @@ class PhonePrefixWidget(PhoneNumberPrefixWidget):
def __init__(self, attrs=None):
widgets = (Select(attrs=attrs, choices=phone_prefixes), TextInput())
super(PhoneNumberPrefixWidget, self).__init__(widgets, attrs)
+
+class DatalistTextWidget(Select):
+ template_name = "userprofile/snippets/datalist.html"
+ input_type = "text"
+
+ def render(self, *args, **kwargs):
+ return super(DatalistTextWidget, self).render(*args, **kwargs)
+
+ def get_context(self, name, value, attrs):
+ context = super(DatalistTextWidget, self).get_context(name, value, attrs)
+ context['widget']['type'] = self.input_type
+ return context
+
+ def format_value(self, value):
+ value = super(DatalistTextWidget, self).format_value(value)
+ value = value[0]
+ if value:
+ for choice in self.choices:
+ if any(c.lower() == value.lower() for c in choice):
+ return choice[0]
+ for choice in self.choices:
+ if any(c.lower().startswith(value.lower()) for c in choice):
+ return choice[0]
+ for choice in self.choices:
+ if any(value.lower() in c.lower() for c in choice):
+ return choice[0]
+ return value
diff --git a/templates/userprofile/snippets/datalist.html b/templates/userprofile/snippets/datalist.html
new file mode 100644
index 0000000000..60ed5638c1
--- /dev/null
+++ b/templates/userprofile/snippets/datalist.html
@@ -0,0 +1,8 @@
+<input type="{{ widget.type }}" name="{{ widget.name }}" list="{{widget.name}}_list" {% if widget.value != None and widget.value != "" %} value="{{ widget.value }}"{% endif %}{% include "django/forms/widgets/attrs.html" %} />
+
+<datalist id="{{widget.name}}_list">
+{% for group_name, group_choices, group_index in widget.optgroups %}{% if group_name %}
+ <optgroup label="{{ group_name }}">{% endif %}{% for option in group_choices %}
+ {% include option.template_name with widget=option %}{% endfor %}{% if group_name %}
+ </optgroup>{% endif %}{% endfor %}
+</datalist>