11from django .forms .widgets import Widget
2- from django .template .loader import get_template
2+ from django .template .loader import get_template , render_to_string
33from django .utils .functional import cached_property
44
55
@@ -8,25 +8,37 @@ class Button(Widget):
88 button_type = 'button'
99 action = None
1010 button_variant = None
11+ button_size = None
1112 icon_path = None
13+ icon_char = None
1214 icon_left = None
1315
1416 def __init__ (
1517 self ,
1618 attrs = None ,
1719 action = None ,
1820 button_variant = None ,
21+ button_size = None ,
1922 auto_disable = False ,
23+ omit_restore = False ,
2024 icon_path = None ,
25+ icon_char = None ,
2126 icon_left = False
2227 ):
2328 if action is not None :
2429 self .action = action
2530 if button_variant :
2631 self .button_variant = button_variant
32+ if button_size :
33+ self .button_size = button_size
2734 self .auto_disable = auto_disable
35+ self .omit_restore = omit_restore
36+ if icon_path and icon_char :
37+ raise ValueError ("Specify either icon_path or icon_char, not both." )
2838 if icon_path :
2939 self .icon_path = icon_path
40+ if icon_char :
41+ self .icon_char = icon_char
3042 self .icon_left = icon_left
3143 super ().__init__ (attrs )
3244
@@ -36,20 +48,26 @@ def build_attrs(self, base_attrs, extra_attrs=None):
3648 attrs ['df-click' ] = self .action
3749 if self .auto_disable :
3850 attrs ['auto-disable' ] = True
51+ if self .omit_restore :
52+ attrs ['omit-restore' ] = True
3953 return attrs
4054
4155 def get_context (self , name , value , attrs ):
4256 context = super ().get_context (name , value , attrs )
4357 context ['label' ] = context ['widget' ]['attrs' ].pop ('label' , None ) # for buttons, the label is the value
4458 context ['widget' ]['type' ] = self .button_type
4559 context ['widget' ]['variant' ] = self .button_variant
60+ context ['widget' ]['size' ] = self .button_size
4661 context ['icon_element' ] = self .icon_element
4762 context ['icon_left' ] = self .icon_left
4863 return context
4964
5065 @cached_property
5166 def icon_element (self ):
52- if self .icon_path :
53- template = get_template (self .icon_path )
54- return template .render ()
55- return ''
67+ if self .icon_char :
68+ icon = self .icon_char
69+ elif self .icon_path :
70+ icon = render_to_string (self .icon_path )
71+ else :
72+ return ''
73+ return icon
0 commit comments