File tree Expand file tree Collapse file tree 2 files changed +39
-0
lines changed
Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Original file line number Diff line number Diff 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 ] ) ,
Original file line number Diff line number Diff 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} ) ;
You can’t perform that action at this time.
0 commit comments