Andriy Kornatskyy avatar Andriy Kornatskyy committed e9c8738

Added radio widget

Comments (0)

Files changed (3)

src/wheezy/html/factory.py

     def __call__(self, *tags):
         """ Call function let combine several tags into fragment.
 
-            >>> tag(tag.b('1'), tag.i('2'))
+            >>> tag((tag.b('1'), tag.i('2')))
             <b>1</b><i>2</i>
         """
         return Fragment(*tags)

src/wheezy/html/markup.py

 
     __slots__ = ['tags']
 
-    def __init__(self, *tags):
+    def __init__(self, tags):
         self.tags = tags
 
     def __unicode__(self):  # pragma: nocover, python 3
         """
-            >>> assert str_type(Fragment(1, 2)) == str_type('12')
+            >>> assert str_type(Fragment((1, 2))) == str_type('12')
         """
         return ''.join(map(str_type, self.tags))
 
     def __str__(self):
         """
-            >>> print(Fragment(1, 2))
+            >>> print(Fragment((1, 2)))
             12
         """
         return ''.join(map(str, self.tags))
 
     def __repr__(self):
         """
-            >>> Fragment(1, 2)
+            >>> Fragment((1, 2))
             12
         """
         return ''.join(map(repr, self.tags))

src/wheezy/html/widgets.py

 from wheezy.html.comp import iteritems
 from wheezy.html.markup import Fragment
 from wheezy.html.markup import Tag
+from wheezy.html.utils import html_escape
 
 
 id = lambda name: name.replace('_', '-')
         tag_attrs['checked'] = 'checked'
     if attrs:
         tag_attrs.update(attrs)
-    return Fragment(
+    return Fragment((
             Tag('input', attrs={'name': name, 'type': 'hidden'}),
             Tag('input', attrs=tag_attrs)
-    )
+    ))
 
 
 def label(name, value, attrs=None):
                     'value': key
             }
         options.append(Tag('option', inner=text, attrs=tag_attrs))
-    options = Fragment(*options)
+    options = Fragment(options)
     tag_attrs = {
             'id': id(name),
             'name': name
     return Tag('select', inner=options, attrs=tag_attrs)
 
 
+def radio(name, value, attrs=None):
+    """
+        >>> from operator import itemgetter
+        >>> scm = sorted({'git': 'Git', 'hg': 'Mercurial'}.items(),
+        ...         key=itemgetter(1))
+
+        >>> scm
+        [('git', 'Git'), ('hg', 'Mercurial')]
+
+        >>> radio('scm', 'hg',
+        ...         attrs={'choices': scm})  #doctest: +NORMALIZE_WHITESPACE
+        <label><input type="radio" name="scm" value="git"
+        />Git</label><label><input checked="checked" type="radio" name="scm"
+        value="hg" />Mercurial</label>
+    """
+    choices = attrs.pop('choices')
+    elements = []
+    append = elements.append
+    for key, text in choices:
+        i = id(name) + '-' + key
+        tag_attrs={
+                'name': name,
+                'type': 'radio',
+                'value': key
+        }
+        if key == value:
+            tag_attrs['checked'] = 'checked'
+        append(Tag('label',
+            Fragment((Tag('input', attrs=tag_attrs), text))))
+    return Fragment(elements)
+
 default = {
         'hidden': hidden,
         'textbox': textbox,
         'textarea': textarea,
         'checkbox': checkbox,
         'label': label,
-        'dropdown': dropdown
+        'dropdown': dropdown,
+        'select': dropdown,
+        'radio': radio
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.