Skip to content

Commit 9d08442

Browse files
committed
chore: Restore previous value if input value is auto-submitted
1 parent 7926e66 commit 9d08442

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

js/chat/chat.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,15 +239,21 @@ class ChatInput extends LightElement {
239239
value: string,
240240
{ submit = false, focus = false }: ChatInputSetInputOptions = {}
241241
): void {
242+
// Store previous value to restore post-submit (if submitting)
243+
const oldValue = this.textarea.value;
244+
242245
this.textarea.value = value;
243246

244247
// Simulate an input event (to trigger the textarea autoresize)
245248
const inputEvent = new Event("input", { bubbles: true, cancelable: true });
246249
this.textarea.dispatchEvent(inputEvent);
247250

248251
if (submit) {
249-
this.#sendInput(focus);
250-
} else if (focus) {
252+
this.#sendInput(false);
253+
if (oldValue) this.setInputValue(oldValue);
254+
}
255+
256+
if (focus) {
251257
this.textarea.focus();
252258
}
253259
}

tests/playwright/shiny/components/chat/update_user_input/test_chat_update_user_input.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,26 @@ def test_validate_chat_update_user_input(page: Page, local_app: ShinyAppProc) ->
3131

3232
# Set the input and move focus ----
3333
set_and_focus.click()
34-
chat.expect_user_input("Input was set and focused, but not submitted.")
34+
set_msg = "Input was set and focused, but not submitted."
35+
chat.expect_user_input(set_msg)
3536
expect(chat.loc_input_button).to_be_enabled()
3637
expect(chat.loc_input).to_be_focused()
3738

3839
# Auto submit ----
40+
submit.loc.focus()
41+
submit.click()
42+
chat.expect_user_input(set_msg) # User doesn't lose what they had written
43+
chat.expect_latest_message("You said: This chat was sent on behalf of the user.")
44+
expect(chat.loc_input_button).to_be_enabled()
45+
expect(chat.loc_input).not_to_be_focused()
46+
47+
# Input remains cleared if previously clear (have to clear the input first)
48+
chat.loc_input.focus()
49+
while chat.loc_input.input_value():
50+
chat.loc_input.press("Backspace")
51+
52+
chat.expect_user_input("")
53+
3954
submit.loc.focus()
4055
submit.click()
4156
chat.expect_user_input("")

0 commit comments

Comments
 (0)