Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ability to customize action button color #2288

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions docs/actions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,20 @@ a single record::
else:
self.datamodel.delete(items)
return redirect(self.get_redirect())

To customize the button color when the ``single`` parameter is ``True``, you can assign
the ``btn_class`` value based on the intended action.
::

@action("delete", "Delete", "Delete all Really?", "fa-rocket", btn_class="btn-danger")
def muldelete(self, items):
if isinstance(items, list):
self.datamodel.delete_all(items)
self.update_redirect()
else:
self.datamodel.delete(items)
return redirect(self.get_redirect())

In this example, the ``btn_class`` parameter
is explicitly set to **btn-danger** to reflect the critical nature of the **Delete** action, ensuring
the button visually communicates its purpose effectively.
19 changes: 16 additions & 3 deletions flask_appbuilder/actions.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
class ActionItem(object):
def __init__(self, name, text, confirmation, icon, multiple, single, func):
def __init__(
self, name, text, confirmation, icon, multiple, single, btn_class, func
):
self.name = name
self.text = text or name
self.confirmation = confirmation
self.icon = icon
self.multiple = multiple
self.single = single
self.btn_class = btn_class
self.func = func

def __repr__(self):
Expand All @@ -17,7 +20,15 @@ def __repr__(self):
)


def action(name, text, confirmation=None, icon=None, multiple=True, single=True):
def action(
name,
text,
confirmation=None,
icon=None,
multiple=True,
single=True,
btn_class="btn-primary",
):
"""
Use this decorator to expose actions

Expand All @@ -34,10 +45,12 @@ def action(name, text, confirmation=None, icon=None, multiple=True, single=True)
If true will display action on list view
:param single:
If true will display action on show view
:param btn_class:
Bootstrap button class name. Defaults to btn-primary
"""

def wrap(f):
f._action = (name, text, confirmation, icon, multiple, single)
f._action = (name, text, confirmation, icon, multiple, single, btn_class)
return f

return wrap
2 changes: 1 addition & 1 deletion flask_appbuilder/templates/appbuilder/general/lib.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<a
id="btn-action-{{ endpoint }}-{{ action.name }}"
href="#"
class="btn btn-sm btn-primary"
class="btn btn-sm {{ action.btn_class }}"
>
<i class="fa {{action.icon}}"></i>
{{_(action.text)}}
Expand Down
Loading