Skip to content

Commit 560fee0

Browse files
fix(boxai-sidebar): Arrow Key Navigation on Suggested Questions (#3865)
* feat(boxai-sidebar): Arrow Key Navigation on Suggested Questions * feat(boxai-sidebar): ArrowLeft/Right browser-native button behavior fix * feat(boxai-sidebar): UT added
1 parent ea97280 commit 560fee0

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/elements/content-sidebar/BoxAISidebar.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,20 @@ const BoxAISidebar = (props: BoxAISidebarProps) => {
9999
questionsWithoutInProgress = questionsWithoutInProgress.slice(0, -1);
100100
}
101101

102+
const handleKeyPress = React.useCallback((event: KeyboardEvent) => {
103+
if (event.key === 'ArrowLeft' || event.key === 'ArrowRight') {
104+
event.preventDefault();
105+
event.stopPropagation();
106+
}
107+
}, []);
108+
109+
React.useEffect(() => {
110+
document.addEventListener('keydown', handleKeyPress,{ capture: true });
111+
return () => {
112+
document.removeEventListener('keydown', handleKeyPress, { capture: true });
113+
};
114+
}, [handleKeyPress]);
115+
102116
const localizedQuestions = DOCUMENT_SUGGESTED_QUESTIONS.map(question => ({
103117
id: question.id,
104118
label: formatMessage(messages[question.labelId]),

src/elements/content-sidebar/__tests__/BoxAISidebar.test.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,29 @@ describe('elements/content-sidebar/BoxAISidebar', () => {
220220

221221
expect(screen.queryByTestId('content-answers-modal')).not.toBeInTheDocument();
222222
});
223+
224+
describe('BoxAISidebar handleKeyPress', () => {
225+
226+
test('should prevent default behavior and stop propagation for ArrowLeft and ArrowRight keys', async () => {
227+
await renderComponent();
228+
229+
const eventLeft = new KeyboardEvent('keydown', { key: 'ArrowLeft' });
230+
const preventDefaultSpy = jest.spyOn(eventLeft, 'preventDefault');
231+
const stopPropagationSpy = jest.spyOn(eventLeft, 'stopPropagation');
232+
233+
document.dispatchEvent(eventLeft);
234+
235+
expect(preventDefaultSpy).toHaveBeenCalled();
236+
expect(stopPropagationSpy).toHaveBeenCalled();
237+
238+
const eventRight = new KeyboardEvent('keydown', { key: 'ArrowRight' });
239+
const preventDefaultSpyRight = jest.spyOn(eventRight, 'preventDefault');
240+
const stopPropagationSpyRight = jest.spyOn(eventRight, 'stopPropagation');
241+
242+
document.dispatchEvent(eventRight);
243+
244+
expect(preventDefaultSpyRight).toHaveBeenCalled();
245+
expect(stopPropagationSpyRight).toHaveBeenCalled();
246+
});
247+
});
223248
});

0 commit comments

Comments
 (0)