Skip to content

Commit 2203a56

Browse files
authored
select value was not populated on initialization, fixes #22 (#28)
1 parent 6ce8217 commit 2203a56

File tree

3 files changed

+73
-72
lines changed

3 files changed

+73
-72
lines changed

src/edit_python_pe/main.py

Lines changed: 67 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from textual.app import App, ComposeResult
66
from textual.containers import Horizontal, Vertical
77
from textual.events import Event
8+
from textual.types import NoSelection
89
from textual.widgets import (Button, Input, ListItem, ListView, Select, Static,
910
TextArea)
1011

@@ -23,6 +24,66 @@
2324
load_file_into_form)
2425

2526

27+
class SocialEntry(Horizontal):
28+
DEFAULT_CSS = """
29+
SocialEntry Select {
30+
width: 25%;
31+
}
32+
SocialEntry Input {
33+
width: 50%;
34+
}
35+
SocialEntry Button {
36+
width: 25%;
37+
}
38+
"""
39+
40+
def __init__(self, index: int, value: str) -> None:
41+
super().__init__()
42+
self.index = index
43+
self.select = Select(
44+
options=[
45+
GITHUB_OPTION,
46+
GITLAB_OPTION,
47+
BITBUCKET_OPTION,
48+
LINKEDIN_OPTION,
49+
FACEBOOK_OPTION,
50+
INSTAGRAM_OPTION,
51+
X_OPTION,
52+
YOUTUBE_OPTION,
53+
],
54+
prompt=PROMPT_SOCIAL_NETWORK,
55+
value=value,
56+
)
57+
self.url_input = Input(placeholder=PLACEHOLDER_SOCIAL_URL)
58+
self.delete_btn = Button(BUTTON_DELETE, id=f"delete_social_{index}")
59+
60+
def compose(self) -> ComposeResult:
61+
yield self.select
62+
yield self.url_input
63+
yield self.delete_btn
64+
65+
66+
class AliasEntry(Horizontal):
67+
DEFAULT_CSS = """
68+
AliasEntry Input {
69+
width: 75%;
70+
}
71+
AliasEntry Button {
72+
width: 25%;
73+
}
74+
"""
75+
76+
def __init__(self, index: int) -> None:
77+
super().__init__()
78+
self.index = index
79+
self.alias_input = Input(placeholder=PLACEHOLDER_ALIAS)
80+
self.delete_btn = Button(BUTTON_DELETE, id=f"delete_alias_{index}")
81+
82+
def compose(self) -> ComposeResult:
83+
yield self.alias_input
84+
yield self.delete_btn
85+
86+
2687
class MemberApp(App):
2788
"""Single app that toggles between a file list and a form while connected to a GitHub fork+push flow."""
2889

@@ -129,8 +190,8 @@ def on_mount(self) -> None:
129190
self.form_container.display = False
130191

131192
# Some data structures
132-
self.social_entries = []
133-
self.alias_entries = []
193+
self.social_entries: list[SocialEntry] = []
194+
self.alias_entries: list[AliasEntry] = []
134195
self.social_index = 0
135196
self.alias_index = 0
136197
self.current_file = None
@@ -206,47 +267,10 @@ def on_button_pressed(self, event: Button.Pressed) -> None:
206267
index = int(bid.replace("delete_alias_", ""))
207268
self.remove_alias_entry(index)
208269

209-
def add_social_entry(self) -> None:
210-
class SocialEntry(Horizontal):
211-
DEFAULT_CSS = """
212-
SocialEntry Select {
213-
width: 25%;
214-
}
215-
SocialEntry Input {
216-
width: 50%;
217-
}
218-
SocialEntry Button {
219-
width: 25%;
220-
}
221-
"""
222-
223-
def __init__(se, index):
224-
super().__init__()
225-
se.index = index
226-
se.select = Select(
227-
options=[
228-
GITHUB_OPTION,
229-
GITLAB_OPTION,
230-
BITBUCKET_OPTION,
231-
LINKEDIN_OPTION,
232-
FACEBOOK_OPTION,
233-
INSTAGRAM_OPTION,
234-
X_OPTION,
235-
YOUTUBE_OPTION,
236-
],
237-
prompt=PROMPT_SOCIAL_NETWORK,
238-
)
239-
se.url_input = Input(placeholder=PLACEHOLDER_SOCIAL_URL)
240-
se.delete_btn = Button(
241-
BUTTON_DELETE, id=f"delete_social_{index}"
242-
)
243-
244-
def compose(se) -> ComposeResult:
245-
yield se.select
246-
yield se.url_input
247-
yield se.delete_btn
248-
249-
new_entry = SocialEntry(self.social_index)
270+
def add_social_entry(
271+
self, value: str | NoSelection = Select.BLANK
272+
) -> None:
273+
new_entry = SocialEntry(self.social_index, value)
250274
self.social_index += 1
251275
self.social_entries.append(new_entry)
252276
self.social_container.mount(new_entry)
@@ -262,28 +286,6 @@ def remove_social_entry(self, index: int) -> None:
262286
found.remove()
263287

264288
def add_alias_entry(self) -> None:
265-
class AliasEntry(Horizontal):
266-
DEFAULT_CSS = """
267-
AliasEntry Input {
268-
width: 75%;
269-
}
270-
AliasEntry Button {
271-
width: 25%;
272-
}
273-
"""
274-
275-
def __init__(se, index):
276-
super().__init__()
277-
se.index = index
278-
se.alias_input = Input(placeholder=PLACEHOLDER_ALIAS)
279-
se.delete_btn = Button(
280-
BUTTON_DELETE, id=f"delete_alias_{index}"
281-
)
282-
283-
def compose(se) -> ComposeResult:
284-
yield se.alias_input
285-
yield se.delete_btn
286-
287289
row = AliasEntry(self.alias_index)
288290
self.alias_index += 1
289291
self.alias_entries.append(row)

src/edit_python_pe/utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,8 @@ def load_file_into_form(app: "MemberApp", filename: str) -> None:
271271
for match in social_link_pattern.finditer(social_html):
272272
url = match.group(1)
273273
platform = match.group(2)
274-
app.add_social_entry()
274+
app.add_social_entry(platform)
275275
last_entry = app.social_entries[-1]
276-
last_entry.select.value = platform
277276
last_entry.url_input.value = url
278277

279278
# Extract aliases, city, homepage from colon-prefixed lines

tests/test_member_app.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def test_add_social_entry(self):
8080
# Patch mount to accept any object
8181
self.app.social_container.mount = lambda x: None # type: ignore
8282

83-
def stub_add_social_entry():
83+
def stub_add_social_entry(value):
8484
entry = self.StubSocialEntry()
8585
entry.index = self.app.social_index
8686
self.app.social_index += 1
@@ -89,7 +89,7 @@ def stub_add_social_entry():
8989

9090
self.app.add_social_entry = stub_add_social_entry
9191
initial_count = len(self.app.social_entries)
92-
self.app.add_social_entry()
92+
self.app.add_social_entry("")
9393
self.assertEqual(len(self.app.social_entries), initial_count + 1)
9494

9595
def test_add_list_button_clears_form(self):
@@ -350,7 +350,7 @@ def test_clear_form(self):
350350
self.app.social_container.remove_children = lambda: None # type: ignore
351351
self.app.alias_container.remove_children = lambda: None # type: ignore
352352

353-
def stub_add_social_entry():
353+
def stub_add_social_entry(value):
354354
entry = self.StubSocialEntry()
355355
entry.index = self.app.social_index
356356
self.app.social_index += 1
@@ -366,7 +366,7 @@ def stub_add_alias_entry():
366366

367367
self.app.add_social_entry = stub_add_social_entry
368368
self.app.add_alias_entry = stub_add_alias_entry
369-
self.app.add_social_entry()
369+
self.app.add_social_entry("")
370370
self.app.add_alias_entry()
371371
self.app.clear_form()
372372
self.assertEqual(len(self.app.social_entries), 0)
@@ -407,7 +407,7 @@ def test_load_file_into_form(self, mock_exists, mock_open):
407407
self.app.social_entries = []
408408
self.app.alias_entries = []
409409

410-
def stub_add_social_entry():
410+
def stub_add_social_entry(value):
411411
entry = self.StubSocialEntry()
412412
entry.index = self.app.social_index
413413
self.app.social_index += 1

0 commit comments

Comments
 (0)