feat: Add customizable button labels to forms and widgets

- Form and TextEditor classes now accept submit_button_name and cancel_button_name
- Widget's chatmark header updated with customizable submit and cancel labels
- Adjusted initialization of Checkbox and Radio classes with button label parameters
This commit is contained in:
bobo 2024-01-05 10:35:12 +08:00 committed by kagami
parent c5d4272cbb
commit 21217d2bb7
3 changed files with 28 additions and 9 deletions

View File

@ -169,7 +169,7 @@ def get_marked_files(modified_files, staged_files):
form_list.append("Unstaged:\n\n")
form_list.append(unstaged_checkbox)
form = Form(form_list)
form = Form(form_list, submit_button_name="Continue")
# Render the Form and get user input
form.render()
@ -283,7 +283,7 @@ def display_commit_message_and_commit(commit_message):
None
"""
text_editor = TextEditor(commit_message)
text_editor = TextEditor(commit_message, submit_button_name="Commit")
text_editor.render()
new_commit_message = text_editor.new_text

View File

@ -14,6 +14,8 @@ class Form:
self,
components: List[Union[Widget, str]],
title: Optional[str] = None,
submit_button_name = "Submit",
cancel_button_name = "Cancel"
):
"""
components: components in the form, can be widgets (except Button) or strings
@ -27,6 +29,8 @@ class Form:
self._title = title
self._rendered = False
self._submit = submit_button_name
self._cancel = cancel_button_name
@property
def components(self) -> List[Union[Widget, str]]:
@ -75,7 +79,7 @@ class Form:
self._rendered = True
lines = [
"```chatmark type=form",
f"```chatmark submit={self._submit} cancel={self._cancel}",
self._in_chatmark(),
"```",
]

View File

@ -9,10 +9,12 @@ class Widget(ABC):
Abstract base class for widgets
"""
def __init__(self):
def __init__(self, submit: Optional[str] = None, cancel: str = "Cancel"):
self._rendered = False
# Prefix for IDs/keys in the widget
self._id_prefix = self.gen_id_prefix()
self._submit = submit
self._cancel = cancel
@abstractmethod
def _in_chatmark(self) -> str:
@ -39,9 +41,14 @@ class Widget(ABC):
raise RuntimeError("Widget can only be rendered once")
self._rendered = True
if self._submit is None:
chatmark_header = "```chatmark"
else:
chatmark_header = f"```chatmark submit={self._submit} cancel={self._cancel}"
lines = [
"```chatmark",
chatmark_header,
self._in_chatmark(),
"```",
]
@ -89,13 +96,15 @@ class Checkbox(Widget):
options: List[str],
check_states: Optional[List[bool]] = None,
title: Optional[str] = None,
submit_button_name: str = "Submit",
cancel_button_name: str = "Cancel"
):
"""
options: options to be selected
check_states: initial check states of options, default to all False
title: title of the widget
"""
super().__init__()
super().__init__(submit_button_name, cancel_button_name)
if check_states is not None:
assert len(options) == len(check_states)
@ -183,8 +192,12 @@ class TextEditor(Widget):
```
"""
def __init__(self, text: str, title: Optional[str] = None):
super().__init__()
def __init__(self,
text: str,
title: Optional[str] = None,
submit_button_name: str = "Submit",
cancel_button_name: str = "Cancel"):
super().__init__(submit_button_name, cancel_button_name)
self._title = title
self._text = text
@ -239,6 +252,8 @@ class Radio(Widget):
# TODO: implement default_selected after the design is ready
# default_selected: Optional[int] = None,
title: Optional[str] = None,
submit_button_name: str = "Submit",
cancel_button_name: str = "Cancel"
) -> None:
"""
options: options to be selected
@ -249,7 +264,7 @@ class Radio(Widget):
# if default_selected is not None:
# assert 0 <= default_selected < len(options)
super().__init__()
super().__init__(submit_button_name, cancel_button_name)
self._options = options
self._title = title