feat: add keyboard navigation support to JsonViewer component

This commit is contained in:
Imamuzzaki Abu Salam 2025-02-07 16:02:06 +07:00
parent 3a748022be
commit f42118f92d
No known key found for this signature in database
GPG Key ID: E83A7B4A2C91CD79
2 changed files with 51 additions and 0 deletions

View File

@ -41,6 +41,7 @@ const Json = (props: JsonProps) => {
<OptionsContext.Consumer> <OptionsContext.Consumer>
{options => ( {options => (
<PrismDiv <PrismDiv
tabIndex={0}
className={props.className} className={props.className}
// tslint:disable-next-line // tslint:disable-next-line
ref={node => setNode(node!)} ref={node => setNode(node!)}

View File

@ -2,6 +2,7 @@
import { mount, ReactWrapper } from 'enzyme'; import { mount, ReactWrapper } from 'enzyme';
import * as React from 'react'; import * as React from 'react';
import { act } from 'react';
import { JsonViewer } from '../'; import { JsonViewer } from '../';
import { withTheme } from '../testProviders'; import { withTheme } from '../testProviders';
@ -50,5 +51,54 @@ describe('Components', () => {
expect(flatDataComponent.html()).not.toContain('Expand all'); expect(flatDataComponent.html()).not.toContain('Expand all');
expect(flatDataComponent.html()).not.toContain('Collapse all'); expect(flatDataComponent.html()).not.toContain('Collapse all');
}); });
describe('Keyboard Navigation', () => {
let component: ReactWrapper;
const data = {
a: 1,
b: {
c:
// Long string to test horizontal scrolling
Array(100).fill('hello').join(''),
},
};
beforeEach(() => {
component = mount(withTheme(<JsonViewer data={data} />));
ClipboardService.copySelected = origCopySelected;
});
test('should handle arrow key navigation', () => {
const prismDiv = component.find('div[tabIndex=0]');
const divElement = prismDiv.getDOMNode();
// Mock scrollLeft before events
Object.defineProperty(divElement, 'scrollLeft', {
get: jest.fn(() => 0),
set: jest.fn(),
});
// Trigger events inside act()
act(() => {
divElement.dispatchEvent(
new KeyboardEvent('keydown', {
key: 'ArrowRight',
bubbles: true,
}),
);
});
act(() => {
divElement.dispatchEvent(
new KeyboardEvent('keydown', {
key: 'ArrowLeft',
bubbles: true,
}),
);
});
expect(divElement.scrollLeft).toBe(0);
});
});
}); });
}); });