[Zope3-checkins] CVS: Zope3/src/zope/app/browser/form - widget.py:1.14

Steve Alexander steve@cat-box.net
Thu, 20 Feb 2003 09:46:15 -0500


Update of /cvs-repository/Zope3/src/zope/app/browser/form
In directory cvs.zope.org:/tmp/cvs-serv991/src/zope/app/browser/form

Modified Files:
	widget.py 
Log Message:
Made the labels on radio button widgets work as expected, rather than
having clicking the 'label' for the whole set of buttons select the
first button.


=== Zope3/src/zope/app/browser/form/widget.py 1.13 => 1.14 ===
--- Zope3/src/zope/app/browser/form/widget.py:1.13	Tue Feb 11 10:59:32 2003
+++ Zope3/src/zope/app/browser/form/widget.py	Thu Feb 20 09:45:43 2003
@@ -445,6 +445,7 @@
 
         # FIXME: what if we run into multiple items with same value?
         rendered_items = []
+        count = 0
         for item in items:
             try:
                 item_value, item_text = item
@@ -453,17 +454,20 @@
                 item_text = item
 
             if item_value == value:
-                rendered_item = self.renderSelectedItem(item_text,
+                rendered_item = self.renderSelectedItem(count,
+                                                        item_text,
                                                         item_value,
                                                         name,
                                                         cssClass)
             else:
-                rendered_item = self.renderItem(item_text,
+                rendered_item = self.renderItem(count,
+                                                item_text,
                                                 item_value,
                                                 name,
                                                 cssClass)
 
             rendered_items.append(rendered_item)
+            count += 1
 
         return rendered_items
 
@@ -485,11 +489,11 @@
                               contents = "\n".join(renderedItems),
                               extra = self.getValue('extra'))
 
-    def renderItem(self, text, value, name, cssClass):
+    def renderItem(self, index, text, value, name, cssClass):
         return renderElement('option', contents=text, value=value,
                               cssClass=cssClass)
 
-    def renderSelectedItem(self, text, value, name, cssClass):
+    def renderSelectedItem(self, index, text, value, name, cssClass):
         return renderElement('option', contents=text, value=value,
                               cssClass=cssClass, selected=None)
 
@@ -508,23 +512,31 @@
         else:
             return '<br />'.join(rendered_items)
 
-    def renderItem(self, text, value, name, cssClass):
-        return renderElement('input',
-                              type = "radio",
-                              cssClass = cssClass,
-                              name = name,
-                              id = name,
-                              value = value) + text
+    def _renderItem(self, index, text, value, name, cssClass, checked):
+        id = '%s.%s' % (name, index)
+        if checked:
+            element = renderElement('input',
+                                    type="radio",
+                                    cssClass=cssClass,
+                                    name=name,
+                                    id=id,
+                                    value=value,
+                                    checked=None)
+        else:
+            element = renderElement('input',
+                                    type="radio",
+                                    cssClass=cssClass,
+                                    name=name,
+                                    id=id,
+                                    value=value)
 
-    def renderSelectedItem(self, text, value, name, cssClass):
-        return renderElement('input',
-                              type="radio",
-                              cssClass=cssClass,
-                              name = name,
-                              id = name,
-                              value = value,
-                              checked = None) + text
+        return '%s<label for="%s">%s</label>' % (element, id, text)
+
+    def renderItem(self, index, text, value, name, cssClass):
+        return self._renderItem(index, text, value, name, cssClass, False)
 
+    def renderSelectedItem(self, index, text, value, name, cssClass):
+        return self._renderItem(index, text, value, name, cssClass, True)
 
 class MultiItemsWidget(ItemsWidget):
     """A widget with a number of items that has multiple selectable items."""
@@ -547,6 +559,7 @@
         items = self.context.allowed_values
         cssClass = self.getValue('cssClass')
         rendered_items = []
+        count = 0
         for item in items:
             try:
                 item_value, item_text = item
@@ -555,17 +568,20 @@
                 item_text = item
 
             if item_value in value:
-                rendered_item = self.renderSelectedItem(item_text,
+                rendered_item = self.renderSelectedItem(count,
+                                                        item_text,
                                                         item_value,
                                                         name,
                                                         cssClass)
             else:
-                rendered_item = self.renderItem(item_text,
+                rendered_item = self.renderItem(count,
+                                                item_text,
                                                 item_value,
                                                 name,
                                                 cssClass)
 
             rendered_items.append(rendered_item)
+            count += 1
 
         return rendered_items
 
@@ -587,10 +603,10 @@
                               contents = "\n".join(rendered_items),
                               extra = self.getValue('extra'))
 
-    def renderItem(self, text, value, name, cssClass):
+    def renderItem(self, index, text, value, name, cssClass):
         return renderElement('option', contents=text, value=value)
 
-    def renderSelectedItem(self, text, value, name, cssClass):
+    def renderSelectedItem(self, index, text, value, name, cssClass):
         return renderElement('option', contents=text, value=value,
                               selected=None)
 
@@ -609,7 +625,7 @@
         else:
             return "<br />".join(rendered_items)
 
-    def renderItem(self, text, value, name, cssClass):
+    def renderItem(self, index, text, value, name, cssClass):
         return renderElement('input',
                               type = "checkbox",
                               cssClass = cssClass,
@@ -617,7 +633,7 @@
                               id = name,
                               value = value) + text
 
-    def renderSelectedItem(self, text, value, name, cssClass):
+    def renderSelectedItem(self, index, text, value, name, cssClass):
         return renderElement('input',
                               type = "checkbox",
                               cssClass = cssClass,