From 8dc0bde017b267815358ced52aff3991a2e9c652 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Fri, 12 Apr 2019 09:21:40 +0200
Subject: [PATCH] forms: move hint inside select2 (like normal select) (#32222)

---
 tests/test_form_pages.py                         | 16 ++++++++++++++++
 wcs/qommon/form.py                               |  2 +-
 wcs/qommon/static/js/qommon.forms.js             |  3 ++-
 .../templates/qommon/forms/widgets/select.html   |  2 +-
 4 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/tests/test_form_pages.py b/tests/test_form_pages.py
index 59759c37f..fadd855f6 100644
--- a/tests/test_form_pages.py
+++ b/tests/test_form_pages.py
@@ -4809,6 +4809,7 @@ def test_item_field_autocomplete_json_source(http_requests, pub):
         urlopen.side_effect = lambda *args: StringIO.StringIO(json.dumps(data))
         resp = get_app(pub).get('/test/')
         assert 'data-autocomplete="true"' in resp.body
+        assert resp.form['f0'].value == '1'
         resp.form['f0'] = '2'
         resp = resp.form.submit('submit') # -> validation page
         resp = resp.form.submit('submit') # -> submit
@@ -4816,6 +4817,21 @@ def test_item_field_autocomplete_json_source(http_requests, pub):
         assert formdef.data_class().select()[0].data['0_display'] == 'world'
         assert formdef.data_class().select()[0].data['0_structured'] == data['data'][1]
 
+    # check hint is displayed within
+    formdef.fields[0].hint = 'help text'
+    formdef.store()
+    with mock.patch('qommon.misc.urlopen') as urlopen:
+        data = {'data': [{'id': '1', 'text': 'hello', 'extra': 'foo'},
+                         {'id': '2', 'text': 'world', 'extra': 'bar'}]}
+        urlopen.side_effect = lambda *args: StringIO.StringIO(json.dumps(data))
+        resp = get_app(pub).get('/test/')
+        assert 'data-autocomplete="true"' in resp.body
+        assert 'data-hint="help text"' in resp.body
+        assert resp.form['f0'].value == ''
+
+    formdef.fields[0].hint = ''
+    formdef.store()
+
     # check with possibility of remote query
     data_source.query_parameter = 'q'
     data_source.id_parameter = 'id'
diff --git a/wcs/qommon/form.py b/wcs/qommon/form.py
index 1eee1a99c..597eb80aa 100644
--- a/wcs/qommon/form.py
+++ b/wcs/qommon/form.py
@@ -1532,7 +1532,7 @@ class SingleSelectHintWidget(SingleSelectWidget):
             get_response().add_css_include('../js/select2/select2.css')
 
     def separate_hint(self):
-        return (self.hint and len(self.hint) > 80) or self.select2
+        return (self.hint and len(self.hint) > 80)
 
     def get_options(self):
         if self.options_with_attributes:
diff --git a/wcs/qommon/static/js/qommon.forms.js b/wcs/qommon/static/js/qommon.forms.js
index 5c3653b88..87834dc5c 100644
--- a/wcs/qommon/static/js/qommon.forms.js
+++ b/wcs/qommon/static/js/qommon.forms.js
@@ -141,8 +141,9 @@ $(function() {
         searching: function () { return WCS_I18N.s2_searching; }
       }
     };
+    options.placeholder = $(elem).find('[data-hint]').data('hint');
     if (!required) {
-      options.placeholder = '...';
+      if (!options.placeholder) options.placeholder = '...';
       options.allowClear = true;
     }
     $(elem).select2(options);
diff --git a/wcs/qommon/templates/qommon/forms/widgets/select.html b/wcs/qommon/templates/qommon/forms/widgets/select.html
index ecdf14778..edc535355 100644
--- a/wcs/qommon/templates/qommon/forms/widgets/select.html
+++ b/wcs/qommon/templates/qommon/forms/widgets/select.html
@@ -5,7 +5,7 @@
     {% if widget.required %}data-required="true"{% endif %}
     {% for attr in widget.attrs.items %}{{attr.0}}="{{attr.1}}"{% endfor %}>
   {% if not widget.separate_hint and widget.hint %}
-  <option value="">{{ widget.hint }}</option>
+  <option value="" data-hint="{{ widget.hint }}">{% if not widget.select2 %}{{ widget.hint }}{% endif %}</option>
   {% endif %}
   {% for option in widget.get_options %}
   <option{% for attr in option.attrs.items %} {{attr.0}}="{{attr.1}}"{% endfor %}>{{ option.description }}</option>
-- 
2.20.1

