Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/components/InputBox/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
MIN_CONVERSATION_HEIGHT,
MIN_INPUT_HEIGHT,
} from './resize.mjs'
import { shouldHandleInputAction } from './input-action.mjs'

export function InputBox({ onSubmit, enabled, postMessage, reverseResizeDir }) {
const { t } = useTranslation()
Expand Down Expand Up @@ -93,7 +94,7 @@ export function InputBox({ onSubmit, enabled, postMessage, reverseResizeDir }) {

const handleKeyDownOrClick = (e) => {
e.stopPropagation()
if (e.type === 'click' || (e.keyCode === 13 && e.shiftKey === false)) {
if (shouldHandleInputAction(e)) {
e.preventDefault()
if (enabled) {
if (!value) return
Expand Down
10 changes: 10 additions & 0 deletions src/components/InputBox/input-action.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function shouldHandleInputAction(event) {
if (event.type === 'click') return true
if (event.type !== 'keydown') return false

const isComposing = event.isComposing || event.nativeEvent?.isComposing
if (isComposing || event.keyCode === 229) return false

const isEnter = event.key === 'Enter' || event.keyCode === 13
return isEnter && !event.shiftKey
}
68 changes: 68 additions & 0 deletions tests/unit/components/input-box-action.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import assert from 'node:assert/strict'
import { test } from 'node:test'
import { shouldHandleInputAction } from '../../../src/components/InputBox/input-action.mjs'

test('input actions handle button clicks and both plain Enter forms', () => {
assert.equal(shouldHandleInputAction({ type: 'click' }), true)
assert.equal(
shouldHandleInputAction({ type: 'keydown', key: 'Enter', shiftKey: false }),
true,
)
assert.equal(
shouldHandleInputAction({ type: 'keydown', keyCode: 13, shiftKey: false }),
true,
)
})

test('input actions preserve Shift+Enter line breaks', () => {
assert.equal(
shouldHandleInputAction({ type: 'keydown', key: 'Enter', keyCode: 13, shiftKey: true }),
false,
)
})

test('input actions ignore directly exposed active IME composition', () => {
assert.equal(
shouldHandleInputAction({
type: 'keydown',
key: 'Enter',
keyCode: 13,
shiftKey: false,
isComposing: true,
}),
false,
)
})

test('input actions ignore active IME composition on synthetic native events', () => {
assert.equal(
shouldHandleInputAction({
type: 'keydown',
key: 'Enter',
keyCode: 13,
shiftKey: false,
nativeEvent: { isComposing: true },
}),
false,
)
})

test('input actions ignore the legacy IME keyCode 229 fallback', () => {
assert.equal(
shouldHandleInputAction({
type: 'keydown',
key: 'Enter',
keyCode: 229,
shiftKey: false,
isComposing: false,
}),
false,
)
})

test('input actions ignore unrelated keyboard events', () => {
assert.equal(
shouldHandleInputAction({ type: 'keydown', key: 'a', keyCode: 65, shiftKey: false }),
false,
)
})