Skip to content

Commit 1b4126f

Browse files
authored
feat: add keyboard navigation support to JsonViewer component (#2654)
1 parent 1fa1327 commit 1b4126f

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/components/JsonViewer/JsonViewer.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const Json = (props: JsonProps) => {
4141
<OptionsContext.Consumer>
4242
{options => (
4343
<PrismDiv
44+
tabIndex={0}
4445
className={props.className}
4546
// tslint:disable-next-line
4647
ref={node => setNode(node!)}

src/components/__tests__/JsonViewer.tsx

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { mount, ReactWrapper } from 'enzyme';
44
import * as React from 'react';
5+
import { act } from 'react';
56

67
import { JsonViewer } from '../';
78
import { withTheme } from '../testProviders';
@@ -50,5 +51,54 @@ describe('Components', () => {
5051
expect(flatDataComponent.html()).not.toContain('Expand all');
5152
expect(flatDataComponent.html()).not.toContain('Collapse all');
5253
});
54+
55+
describe('Keyboard Navigation', () => {
56+
let component: ReactWrapper;
57+
const data = {
58+
a: 1,
59+
b: {
60+
c:
61+
// Long string to test horizontal scrolling
62+
Array(100).fill('hello').join(''),
63+
},
64+
};
65+
66+
beforeEach(() => {
67+
component = mount(withTheme(<JsonViewer data={data} />));
68+
ClipboardService.copySelected = origCopySelected;
69+
});
70+
71+
test('should handle arrow key navigation', () => {
72+
const prismDiv = component.find('div[tabIndex=0]');
73+
const divElement = prismDiv.getDOMNode();
74+
75+
// Mock scrollLeft before events
76+
Object.defineProperty(divElement, 'scrollLeft', {
77+
get: jest.fn(() => 0),
78+
set: jest.fn(),
79+
});
80+
81+
// Trigger events inside act()
82+
act(() => {
83+
divElement.dispatchEvent(
84+
new KeyboardEvent('keydown', {
85+
key: 'ArrowRight',
86+
bubbles: true,
87+
}),
88+
);
89+
});
90+
91+
act(() => {
92+
divElement.dispatchEvent(
93+
new KeyboardEvent('keydown', {
94+
key: 'ArrowLeft',
95+
bubbles: true,
96+
}),
97+
);
98+
});
99+
100+
expect(divElement.scrollLeft).toBe(0);
101+
});
102+
});
53103
});
54104
});

0 commit comments

Comments
 (0)