[formwidgets] let the textarea height be dependant on the actual content (up to a limit) stable
authorAurelien Campeas <aurelien.campeas@logilab.fr>
Thu, 09 Jul 2009 20:13:54 +0200
branchstable
changeset 2366 e4229723b824
parent 2365 9f5e911eab07
child 2367 6ba269240f3b
[formwidgets] let the textarea height be dependant on the actual content (up to a limit)
web/formwidgets.py
web/test/unittest_form.py
--- a/web/formwidgets.py	Thu Jul 09 19:59:41 2009 +0200
+++ b/web/formwidgets.py	Thu Jul 09 20:13:54 2009 +0200
@@ -146,17 +146,22 @@
 
 class TextArea(FieldWidget):
     """<textarea>"""
+
     def render(self, form, field):
         name, values, attrs = self._render_attrs(form, field)
         attrs.setdefault('onkeyup', 'autogrow(this)')
-        attrs.setdefault('cols', 80)
-        attrs.setdefault('rows', 20)
         if not values:
             value = u''
         elif len(values) == 1:
             value = values[0]
         else:
             raise ValueError('a textarea is not supposed to be multivalued')
+        lines = value.splitlines()
+        linecount = len(lines)
+        for line in lines:
+            linecount += len(line) / 80
+        attrs.setdefault('cols', 80)
+        attrs.setdefault('rows', min(15, linecount + 2))
         return tags.textarea(value, name=name, **attrs)
 
 
--- a/web/test/unittest_form.py	Thu Jul 09 19:59:41 2009 +0200
+++ b/web/test/unittest_form.py	Thu Jul 09 20:13:54 2009 +0200
@@ -156,12 +156,12 @@
 <option value="text/html">text/html</option>
 <option value="text/plain">text/plain</option>
 <option selected="selected" value="text/rest">text/rest</option>
-</select><textarea cols="80" id="description:%(eid)s" name="description:%(eid)s" onkeyup="autogrow(this)" rows="20" tabindex="1"/>''')
+</select><textarea cols="80" id="description:%(eid)s" name="description:%(eid)s" onkeyup="autogrow(this)" rows="2" tabindex="1"/>''')
 
 
     def test_richtextfield_2(self):
         self.req.use_fckeditor = lambda: True
-        self._test_richtextfield('<input name="description_format:%(eid)s" style="display: block" type="hidden" value="text/rest"/><textarea cols="80" cubicweb:type="wysiwyg" id="description:%(eid)s" name="description:%(eid)s" onkeyup="autogrow(this)" rows="20" tabindex="0"/>')
+        self._test_richtextfield('<input name="description_format:%(eid)s" style="display: block" type="hidden" value="text/rest"/><textarea cols="80" cubicweb:type="wysiwyg" id="description:%(eid)s" name="description:%(eid)s" onkeyup="autogrow(this)" rows="2" tabindex="0"/>')
 
 
     def test_filefield(self):
@@ -206,7 +206,7 @@
 <input name="data:%(eid)s__detach" type="checkbox"/>
 detach attached file
 <p><b>You can either submit a new file using the browse button above, or choose to remove already uploaded file by checking the "detach attached file" check-box, or edit file content online with the widget below.</b></p>
-<textarea cols="80" name="data:%(eid)s" onkeyup="autogrow(this)" rows="20" tabindex="3">new widgets system</textarea>''' % {'eid': file.eid})
+<textarea cols="80" name="data:%(eid)s" onkeyup="autogrow(this)" rows="3" tabindex="3">new widgets system</textarea>''' % {'eid': file.eid})
 
 
     def test_passwordfield(self):