Skip to content

Commit 275288c

Browse files
committed
Improvements and small changes to button rendering
1 parent 09fb5a9 commit 275288c

File tree

5 files changed

+45
-24
lines changed

5 files changed

+45
-24
lines changed

HISTORY.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ Develop
88

99
* **BREAKING** Remove everything to do with setting HTML `required` attribute (#337)
1010
* **BREAKING** Remove everything to do with setting HTML `disabled` attribute (#345)
11-
* Several improvements to documentation, tests, and comments. Thanks all!
11+
* Default button type to btn-default
1212
* Add addon_before_class and addon_after_class (#295, thanks @DanWright91 and others)
1313
* Fix handling of error class (#170)
1414
* No size class for checkboxes (#318, thanks @cybojenix)
1515
* Fix warnings during install (thanks @mfcovington)
1616
* Add subresource integrity to external components (thanks @mfcovington and @Alex131089)
17+
* Several improvements to documentation, tests, and comments. Thanks all!
1718

1819

1920
7.1.0 (2016-09-16)

bootstrap3/components.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from django.forms.widgets import flatatt
55
from django.utils.safestring import mark_safe
6-
from bootstrap3.utils import render_tag
6+
from bootstrap3.utils import render_tag, add_css_class
77

88
from .text import text_value
99

@@ -12,15 +12,15 @@ def render_icon(icon, **kwargs):
1212
"""
1313
Render a Bootstrap glyphicon icon
1414
"""
15-
classes = ['glyphicon glyphicon-{icon}'.format(icon=icon)]
16-
if kwargs.get('add_class'):
17-
classes.append(kwargs.get('add_class'))
1815
attrs = {
19-
'class': ' '.join(classes),
16+
'class': add_css_class(
17+
'glyphicon glyphicon-{icon}'.format(icon=icon),
18+
kwargs.get('extra_classes', ''),
19+
)
2020
}
21-
if kwargs.get('title'):
22-
attrs['title'] = kwargs.get('title')
23-
21+
title = kwargs.get('title')
22+
if title:
23+
attrs['title'] = title
2424
return render_tag('span', attrs=attrs)
2525

2626

bootstrap3/forms.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@
1313
get_bootstrap_setting, get_form_renderer, get_field_renderer,
1414
get_formset_renderer
1515
)
16-
from .text import text_concat, text_value
17-
from .exceptions import BootstrapError
18-
from .utils import add_css_class, render_tag, split_css_classes
1916
from .components import render_icon
20-
17+
from .exceptions import BootstrapError
18+
from .text import text_concat, text_value
19+
from .utils import add_css_class, render_tag
2120

2221
FORM_GROUP_CLASS = 'form-group'
2322

@@ -77,8 +76,8 @@ def render_label(content, label_for=None, label_class=None, label_title=''):
7776

7877

7978
def render_button(
80-
content, button_type=None, icon=None, button_class='', size='',
81-
href='', name=None, value=None, title=None):
79+
content, button_type=None, icon=None, button_class='btn-default', size='',
80+
href='', name=None, value=None, title=None, extra_classes=''):
8281
"""
8382
Render a button with content
8483
"""
@@ -98,14 +97,12 @@ def render_button(
9897
'Parameter "size" should be "xs", "sm", "lg" or ' +
9998
'empty ("{}" given).'.format(size))
10099
if button_type:
101-
if button_type == 'submit':
102-
if not any([c.startswith('btn-') for c in split_css_classes(classes)]):
103-
classes = add_css_class(classes, 'btn-primary')
104-
elif button_type not in ('reset', 'button', 'link'):
100+
if button_type not in ('submit', 'reset', 'button', 'link'):
105101
raise BootstrapError(
106102
'Parameter "button_type" should be "submit", "reset", ' +
107103
'"button", "link" or empty ("{}" given).'.format(button_type))
108104
attrs['type'] = button_type
105+
classes = add_css_class(classes, extra_classes)
109106
attrs['class'] = classes
110107
icon_content = render_icon(icon) if icon else ''
111108
if href:
@@ -168,8 +165,8 @@ def is_widget_required_attribute(widget):
168165
return False
169166
if isinstance(
170167
widget, (
171-
AdminFileWidget, HiddenInput, FileInput,
172-
CheckboxInput, CheckboxSelectMultiple)):
168+
AdminFileWidget, HiddenInput, FileInput,
169+
CheckboxInput, CheckboxSelectMultiple)):
173170
return False
174171
return True
175172

bootstrap3/templatetags/bootstrap3.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,9 @@ def bootstrap_button(*args, **kwargs):
558558
Name of an icon to render in the button's visible content. See bootstrap_icon_ for acceptable values.
559559
560560
button_class
561+
The class of button to use. If none is given, btn-default will be used.
562+
563+
extra_classes
561564
Any extra CSS classes that should be added to the button.
562565
563566
size
@@ -608,6 +611,12 @@ def bootstrap_icon(icon, **kwargs):
608611
icon
609612
Icon name. See the `Bootstrap docs <http://getbootstrap.com/components/#glyphicons>`_ for all icons.
610613
614+
extra_classes
615+
Extra CSS classes to add to the icon HTML
616+
617+
title
618+
A title for the icon (HTML title attrivute)
619+
611620
**Usage**::
612621
613622
{% bootstrap_icon icon %}

bootstrap3/tests.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -628,11 +628,11 @@ class ButtonTest(TestCase):
628628
def test_button(self):
629629
res = render_template_with_form("{% bootstrap_button 'button' size='lg' %}")
630630
self.assertEqual(
631-
res.strip(), '<button class="btn btn-lg">button</button>')
631+
res.strip(), '<button class="btn btn-default btn-lg">button</button>')
632632
res = render_template_with_form("{% bootstrap_button 'button' size='lg' href='#' %}")
633633
self.assertIn(
634634
res.strip(),
635-
'<a class="btn btn-lg" href="#">button</a><a href="#" ' +
635+
'<a class="btn btn-default btn-lg" href="#">button</a><a href="#" ' +
636636
'class="btn btn-lg">button</a>')
637637

638638

@@ -660,5 +660,19 @@ def test_button_with_icon(self):
660660
)
661661
self.assertEqual(
662662
res.strip(),
663-
'<button class="btn"><span class="glyphicon glyphicon-info-sign"></span> test</button>'
663+
'<button class="btn btn-default"><span class="glyphicon glyphicon-info-sign"></span> test</button>'
664+
)
665+
res = render_template_with_form(
666+
"{% bootstrap_button 'test' icon='info-sign' button_class='btn-primary' %}"
667+
)
668+
self.assertEqual(
669+
res.strip(),
670+
'<button class="btn btn-primary"><span class="glyphicon glyphicon-info-sign"></span> test</button>'
671+
)
672+
res = render_template_with_form(
673+
"{% bootstrap_button 'test' icon='info-sign' button_type='submit' %}"
674+
)
675+
self.assertEqual(
676+
res.strip(),
677+
'<button class="btn btn-default" type="submit"><span class="glyphicon glyphicon-info-sign"></span> test</button>'
664678
)

0 commit comments

Comments
 (0)