refactor: Enhance form and button label flexibility
- Make submit and cancel button labels optional in form and widgets - Refactor chatmark header construction for conditional button labels - Implement assertion for Radio widget's default_selected option
This commit is contained in:
parent
57da9e9841
commit
7d5887e98c
@ -14,8 +14,8 @@ class Form:
|
|||||||
self,
|
self,
|
||||||
components: List[Union[Widget, str]],
|
components: List[Union[Widget, str]],
|
||||||
title: Optional[str] = None,
|
title: Optional[str] = None,
|
||||||
submit_button_name="Submit",
|
submit_button_name: Optional[str] = None,
|
||||||
cancel_button_name="Cancel",
|
cancel_button_name: Optional[str] = None
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
components: components in the form, can be widgets (except Button) or strings
|
components: components in the form, can be widgets (except Button) or strings
|
||||||
@ -78,8 +78,12 @@ class Form:
|
|||||||
|
|
||||||
self._rendered = True
|
self._rendered = True
|
||||||
|
|
||||||
|
chatmark_header = "```chatmark"
|
||||||
|
chatmark_header += f" submit={self._submit}" if self._submit else ""
|
||||||
|
chatmark_header += f" cancel={self._cancel}" if self._cancel else ""
|
||||||
|
|
||||||
lines = [
|
lines = [
|
||||||
f"```chatmark submit={self._submit} cancel={self._cancel}",
|
chatmark_header,
|
||||||
self._in_chatmark(),
|
self._in_chatmark(),
|
||||||
"```",
|
"```",
|
||||||
]
|
]
|
||||||
|
@ -9,7 +9,7 @@ class Widget(ABC):
|
|||||||
Abstract base class for widgets
|
Abstract base class for widgets
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, submit: Optional[str] = None, cancel: str = "Cancel"):
|
def __init__(self, submit: Optional[str] = None, cancel: Optional[str] = None):
|
||||||
self._rendered = False
|
self._rendered = False
|
||||||
# Prefix for IDs/keys in the widget
|
# Prefix for IDs/keys in the widget
|
||||||
self._id_prefix = self.gen_id_prefix()
|
self._id_prefix = self.gen_id_prefix()
|
||||||
@ -42,10 +42,9 @@ class Widget(ABC):
|
|||||||
|
|
||||||
self._rendered = True
|
self._rendered = True
|
||||||
|
|
||||||
if self._submit is None:
|
chatmark_header = "```chatmark"
|
||||||
chatmark_header = "```chatmark"
|
chatmark_header += f" submit={self._submit}" if self._submit else ""
|
||||||
else:
|
chatmark_header += f" cancel={self._cancel}" if self._cancel else ""
|
||||||
chatmark_header = f"```chatmark submit={self._submit} cancel={self._cancel}"
|
|
||||||
|
|
||||||
lines = [
|
lines = [
|
||||||
chatmark_header,
|
chatmark_header,
|
||||||
@ -261,9 +260,8 @@ class Radio(Widget):
|
|||||||
default_selected: index of the option to be selected by default, default to None
|
default_selected: index of the option to be selected by default, default to None
|
||||||
title: title of the widget
|
title: title of the widget
|
||||||
"""
|
"""
|
||||||
# TODO: implement default_selected after the design is ready
|
if default_selected is not None:
|
||||||
# if default_selected is not None:
|
assert 0 <= default_selected < len(options)
|
||||||
# assert 0 <= default_selected < len(options)
|
|
||||||
|
|
||||||
super().__init__(submit_button_name, cancel_button_name)
|
super().__init__(submit_button_name, cancel_button_name)
|
||||||
|
|
||||||
@ -299,7 +297,7 @@ class Radio(Widget):
|
|||||||
for idx, option in enumerate(self._options):
|
for idx, option in enumerate(self._options):
|
||||||
key = self.gen_id(self._id_prefix, idx)
|
key = self.gen_id(self._id_prefix, idx)
|
||||||
if self._selection is not None and self._selection == idx:
|
if self._selection is not None and self._selection == idx:
|
||||||
lines.append(f"> X ({key}) {option}")
|
lines.append(f"> x ({key}) {option}")
|
||||||
else:
|
else:
|
||||||
lines.append(f"> - ({key}) {option}")
|
lines.append(f"> - ({key}) {option}")
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user