-
Notifications
You must be signed in to change notification settings - Fork 738
add default values option for Dialog #1563
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -175,6 +175,7 @@ def radiolist_dialog( | |
ok_text: str = "Ok", | ||
cancel_text: str = "Cancel", | ||
values: Optional[List[Tuple[_T, AnyFormattedText]]] = None, | ||
default_values: List[_T] = [], | ||
style: Optional[BaseStyle] = None, | ||
) -> Application[_T]: | ||
""" | ||
|
@@ -189,7 +190,7 @@ def radiolist_dialog( | |
def ok_handler() -> None: | ||
get_app().exit(result=radio_list.current_value) | ||
|
||
radio_list = RadioList(values) | ||
radio_list = RadioList(values=values, default_values=default_values) | ||
|
||
dialog = Dialog( | ||
title=title, | ||
|
@@ -213,6 +214,7 @@ def checkboxlist_dialog( | |
ok_text: str = "Ok", | ||
cancel_text: str = "Cancel", | ||
values: Optional[List[Tuple[_T, AnyFormattedText]]] = None, | ||
default_values: List[_T] = [], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment here about mutable defaults. |
||
style: Optional[BaseStyle] = None, | ||
) -> Application[List[_T]]: | ||
""" | ||
|
@@ -227,7 +229,7 @@ def checkboxlist_dialog( | |
def ok_handler() -> None: | ||
get_app().exit(result=cb_list.current_values) | ||
|
||
cb_list = CheckboxList(values) | ||
cb_list = CheckboxList(values=values, default_values=default_values) | ||
|
||
dialog = Dialog( | ||
title=title, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -686,14 +686,25 @@ class _DialogList(Generic[_T]): | |
multiple_selection: bool = False | ||
show_scrollbar: bool = True | ||
|
||
def __init__(self, values: Sequence[Tuple[_T, AnyFormattedText]]) -> None: | ||
def __init__( | ||
self, values: Sequence[Tuple[_T, AnyFormattedText]], default_values: List[_T] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment about mutable defaults here. |
||
) -> None: | ||
assert len(values) > 0 | ||
|
||
self.values = values | ||
# current_values will be used in multiple_selection, | ||
# current_value will be used otherwise. | ||
self.current_values: List[_T] = [] | ||
self.current_value: _T = values[0][0] | ||
candidate: List[_T] = [value for (value, _) in values] | ||
self.current_values: List[_T] = [ | ||
default_value | ||
for default_value in default_values | ||
if (default_value in candidate) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no parentheses are required around this expression. |
||
] | ||
self.current_value: _T = ( | ||
default_values[0] | ||
if len(default_values) and default_values[0] in candidate | ||
else values[0][0] | ||
) | ||
self._selected_index = 0 | ||
|
||
# Key bindings. | ||
|
@@ -859,7 +870,8 @@ class Checkbox(CheckboxList[str]): | |
|
||
def __init__(self, text: AnyFormattedText = "", checked: bool = False) -> None: | ||
values = [("value", text)] | ||
CheckboxList.__init__(self, values) | ||
default_values = ["value"] | ||
CheckboxList.__init__(self, values=values, default_values=default_values) | ||
self.checked = checked | ||
|
||
@property | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mutable values should not be used as keyword arguments. State could leak from one call to the next.
Better is to do:
and in the body of the function: