-
-
Notifications
You must be signed in to change notification settings - Fork 238
Expand file tree
/
Copy pathform_editor.py
More file actions
59 lines (50 loc) · 1.96 KB
/
form_editor.py
File metadata and controls
59 lines (50 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from typing import Iterable
from rich.text import Text
from textual.app import ComposeResult
from textual.binding import Binding
from textual.containers import Vertical
from posting.collection import FormItem
from posting.widgets.datatable import PostingDataTable
from posting.widgets.key_value import KeyValueEditor, KeyValueInput
from posting.widgets.variable_input import VariableInput
class FormTable(PostingDataTable):
BINDINGS = [
Binding("backspace", action="remove_row", description="Remove row"),
Binding("space", action="toggle_row", description="Toggle row"),
]
def on_mount(self):
self.fixed_columns = 1
self.show_header = False
self.cursor_type = "row"
self.zebra_stripes = True
self.row_disable = True
self.add_columns("Key", "Value")
def to_model(self) -> list[FormItem]:
form_data: list[FormItem] = []
for row_index in range(self.row_count):
row = self.get_row_at(row_index)
form_data.append(
FormItem(
name=row[0].plain if isinstance(row[0], Text) else row[0],
value=row[1].plain if isinstance(row[1], Text) else row[1],
enabled=self.is_row_enabled_at(row_index),
)
)
return form_data
class FormEditor(Vertical):
"""An editor for form body data."""
def compose(self) -> ComposeResult:
yield KeyValueEditor(
FormTable(),
KeyValueInput(
VariableInput(placeholder="Key"),
VariableInput(placeholder="Value"),
),
empty_message="There is no form data.",
)
def to_model(self) -> list[FormItem]:
return self.query_one(FormTable).to_model()
def replace_all_rows(
self, rows: Iterable[Iterable[str]], enableStates: Iterable[bool] | None = None
) -> None:
self.query_one(FormTable).replace_all_rows(rows, enableStates)