Skip to content

Commit 089a05d

Browse files
authored
UX niceities: enter to submit (#55)
avoid disabling the question input so that it retains focus between messages
1 parent 9e37ee6 commit 089a05d

File tree

3 files changed

+33
-22
lines changed

3 files changed

+33
-22
lines changed

chat/components/chat.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ def action_bar() -> rx.Component:
7373
),
7474
placeholder="Type something...",
7575
id="question",
76-
disabled=State.processing,
7776
flex="1",
7877
),
7978
rx.button(
@@ -86,7 +85,8 @@ def action_bar() -> rx.Component:
8685
margin="0 auto",
8786
align_items="center",
8887
),
89-
on_submit=[State.process_question, rx.set_value("question", "")],
88+
reset_on_submit=True,
89+
on_submit=State.process_question,
9090
),
9191
rx.text(
9292
"ReflexGPT may return factually incorrect or misleading responses. Use discretion.",

chat/components/navbar.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,25 +64,25 @@ def modal(trigger) -> rx.Component:
6464
return rx.dialog.root(
6565
rx.dialog.trigger(trigger),
6666
rx.dialog.content(
67-
rx.hstack(
68-
rx.input(
69-
placeholder="Chat name",
70-
on_blur=State.set_new_chat_name,
71-
flex="1",
72-
min_width="20ch",
73-
),
74-
rx.dialog.close(
75-
rx.button(
76-
"Create chat",
77-
on_click=State.create_chat,
67+
rx.form(
68+
rx.hstack(
69+
rx.input(
70+
placeholder="Chat name",
71+
name="new_chat_name",
72+
flex="1",
73+
min_width="20ch",
7874
),
75+
rx.button("Create chat"),
76+
spacing="2",
77+
wrap="wrap",
78+
width="100%",
7979
),
80-
spacing="2",
81-
wrap="wrap",
82-
width="100%",
80+
on_submit=State.create_chat,
8381
),
8482
background_color=rx.color("mauve", 1),
8583
),
84+
open=State.is_modal_open,
85+
on_open_change=State.set_is_modal_open,
8686
)
8787

8888

chat/state.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,26 @@ class State(rx.State):
3030
# Whether we are processing the question.
3131
processing: bool = False
3232

33-
# The name of the new chat.
34-
new_chat_name: str = ""
33+
# Whether the new chat modal is open.
34+
is_modal_open: bool = False
3535

3636
@rx.event
37-
def create_chat(self):
37+
def create_chat(self, form_data: dict[str, Any]):
3838
"""Create a new chat."""
3939
# Add the new chat to the list of chats.
40-
self.current_chat = self.new_chat_name
41-
self._chats[self.new_chat_name] = []
40+
new_chat_name = form_data["new_chat_name"]
41+
self.current_chat = new_chat_name
42+
self._chats[new_chat_name] = []
43+
self.is_modal_open = False
44+
45+
@rx.event
46+
def set_is_modal_open(self, is_open: bool):
47+
"""Set the new chat modal open state.
48+
49+
Args:
50+
is_open: Whether the modal is open.
51+
"""
52+
self.is_modal_open = is_open
4253

4354
@rx.var
4455
def selected_chat(self) -> list[QA]:
@@ -97,7 +108,7 @@ async def process_question(self, form_data: dict[str, Any]):
97108
question = form_data["question"]
98109

99110
# Check if the question is empty
100-
if question == "":
111+
if not question:
101112
return
102113

103114
async for value in self.openai_process_question(question):

0 commit comments

Comments
 (0)