Skip to content

Commit c803e26

Browse files
chore: Simplify keyboard accessibility (#136)
1 parent 8bce86b commit c803e26

File tree

3 files changed

+22
-36
lines changed

3 files changed

+22
-36
lines changed

src/bundles/AiDrawer/AiDrawer.stories.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ const meta: Meta<typeof AiDrawer> = {
4848
</>
4949
)
5050
},
51+
argTypes: {
52+
payload: {
53+
// JSON controls break form submission via keyboard. See
54+
// https://github.com/storybookjs/storybook/issues/31881
55+
control: false,
56+
},
57+
},
5158
}
5259

5360
export default meta

src/components/AiChat/AiChat.tsx

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -396,10 +396,9 @@ const AiChatDisplay: FC<AiChatDisplayProps> = ({
396396
e.preventDefault()
397397
if (isLoading && stoppable) {
398398
stop()
399-
} else {
400-
scrollToBottom()
401-
handleSubmit(e)
402399
}
400+
scrollToBottom()
401+
handleSubmit(e)
403402
}}
404403
>
405404
<Input
@@ -419,24 +418,13 @@ const AiChatDisplay: FC<AiChatDisplayProps> = ({
419418
isLoading ? (
420419
<AdornmentButton
421420
aria-label="Stop"
422-
onClick={stop}
421+
type="submit"
423422
disabled={!stoppable}
424423
>
425424
<StyledStopButton />
426425
</AdornmentButton>
427426
) : (
428-
<AdornmentButton
429-
aria-label="Send"
430-
type="submit"
431-
onClick={(e) => {
432-
if (input.trim() === "") {
433-
e.preventDefault()
434-
return
435-
}
436-
scrollToBottom()
437-
handleSubmit(e)
438-
}}
439-
>
427+
<AdornmentButton type="submit" aria-label="Send">
440428
<StyledSendButton />
441429
</AdornmentButton>
442430
)

src/components/AiChat/EntryScreen.tsx

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { AdornmentButton, Input } from "../Input/Input"
66
import TimLogo from "./TimLogo"
77
import { useState } from "react"
88

9-
const Container = styled.div(({ theme }) => ({
9+
const Container = styled.form(({ theme }) => ({
1010
display: "flex",
1111
flexDirection: "column",
1212
alignItems: "center",
@@ -116,16 +116,15 @@ const EntryScreen = ({
116116
setPrompt(e.target.value)
117117
}
118118

119-
const onPromptKeyDown: React.KeyboardEventHandler<HTMLInputElement> = (e) => {
120-
if (e.key === "Enter" && prompt) {
121-
onPromptSubmit(prompt)
122-
} else {
123-
setPrompt(prompt)
124-
}
125-
}
126-
127119
return (
128-
<Container className={className} data-testid="ai-chat-entry-screen">
120+
<Container
121+
className={className}
122+
data-testid="ai-chat-entry-screen"
123+
onSubmit={(e) => {
124+
e.preventDefault()
125+
onPromptSubmit(prompt)
126+
}}
127+
>
129128
<TimLogoBox>
130129
<RiSparkling2Line />
131130
<StyledTimLogo />
@@ -134,16 +133,13 @@ const EntryScreen = ({
134133
<StyledInput
135134
fullWidth
136135
size="chat"
136+
name="prompt"
137137
onChange={onPromptChange}
138-
onKeyDown={onPromptKeyDown}
139138
inputProps={{
140139
"aria-label": "Ask a question",
141140
}}
142141
endAdornment={
143-
<AdornmentButton
144-
aria-label="Send"
145-
onClick={() => onPromptSubmit(prompt)}
146-
>
142+
<AdornmentButton type="submit" aria-label="Send">
147143
<SendIcon />
148144
</AdornmentButton>
149145
}
@@ -155,11 +151,6 @@ const EntryScreen = ({
155151
key={index}
156152
onClick={() => onPromptSubmit(content)}
157153
tabIndex={0}
158-
onKeyDown={(e) => {
159-
if (e.key === "Enter") {
160-
onPromptSubmit(content)
161-
}
162-
}}
163154
>
164155
<Typography variant="body2">{content}</Typography>
165156
</Starter>

0 commit comments

Comments
 (0)