Skip to content

Commit 7aa56a1

Browse files
committed
fix: add prop to disable keyboard shortcut
1 parent 5db6ac1 commit 7aa56a1

File tree

2 files changed

+57
-5
lines changed

2 files changed

+57
-5
lines changed

packages/module/src/DataViewTextFilter/DataViewTextFilter.test.tsx

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { render, fireEvent } from '@testing-library/react';
1+
import { render } from '@testing-library/react';
22
import '@testing-library/jest-dom';
33
import DataViewTextFilter, { DataViewTextFilterProps } from './DataViewTextFilter';
44
import DataViewToolbar from '../DataViewToolbar';
@@ -23,7 +23,7 @@ describe('DataViewTextFilter component', () => {
2323
});
2424

2525
it('should focus the search input when "/" key is pressed and filter is visible', () => {
26-
const { container } = render(<DataViewToolbar
26+
render(<DataViewToolbar
2727
filters={
2828
<DataViewTextFilter {...defaultProps} showToolbarItem={true} />
2929
}
@@ -46,7 +46,7 @@ describe('DataViewTextFilter component', () => {
4646
});
4747

4848
it('should not focus the search input when "/" key is pressed if filter is not visible', () => {
49-
const { container } = render(<DataViewToolbar
49+
render(<DataViewToolbar
5050
filters={
5151
<DataViewTextFilter {...defaultProps} showToolbarItem={false} />
5252
}
@@ -81,7 +81,6 @@ describe('DataViewTextFilter component', () => {
8181
);
8282

8383
const otherInput = container.querySelector('[data-testid="other-input"]') as HTMLInputElement;
84-
const searchInput = document.getElementById('test-filter') as HTMLInputElement;
8584

8685
// Focus the other input first
8786
otherInput.focus();
@@ -104,4 +103,50 @@ describe('DataViewTextFilter component', () => {
104103
// The search input should not be focused since we're already in an input field
105104
expect(document.activeElement).toBe(otherInput);
106105
});
106+
107+
it('should not focus the search input when enableShortcut is false', () => {
108+
render(<DataViewToolbar
109+
filters={
110+
<DataViewTextFilter {...defaultProps} showToolbarItem={true} enableShortcut={false} />
111+
}
112+
/>);
113+
114+
const input = document.getElementById('test-filter') as HTMLInputElement;
115+
expect(input).toBeInTheDocument();
116+
117+
// Simulate pressing "/" key
118+
const keyEvent = new KeyboardEvent('keydown', {
119+
key: '/',
120+
code: 'Slash',
121+
bubbles: true,
122+
cancelable: true,
123+
});
124+
window.dispatchEvent(keyEvent);
125+
126+
// The input should not be focused since the shortcut is disabled
127+
expect(document.activeElement).not.toBe(input);
128+
});
129+
130+
it('should focus the search input when enableShortcut is true (default)', () => {
131+
render(<DataViewToolbar
132+
filters={
133+
<DataViewTextFilter {...defaultProps} showToolbarItem={true} />
134+
}
135+
/>);
136+
137+
const input = document.getElementById('test-filter') as HTMLInputElement;
138+
expect(input).toBeInTheDocument();
139+
140+
// Simulate pressing "/" key
141+
const keyEvent = new KeyboardEvent('keydown', {
142+
key: '/',
143+
code: 'Slash',
144+
bubbles: true,
145+
cancelable: true,
146+
});
147+
window.dispatchEvent(keyEvent);
148+
149+
// The input should be focused since the shortcut is enabled by default
150+
expect(document.activeElement).toBe(input);
151+
});
107152
});

packages/module/src/DataViewTextFilter/DataViewTextFilter.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ export interface DataViewTextFilterProps extends SearchInputProps {
1717
trimValue?: boolean;
1818
/** Custom OUIA ID */
1919
ouiaId?: string;
20+
/** Enable keyboard shortcut (/) to focus the filter. Defaults to true. */
21+
enableShortcut?: boolean;
2022
}
2123

2224
export const DataViewTextFilter: FC<DataViewTextFilterProps> = ({
@@ -28,9 +30,14 @@ export const DataViewTextFilter: FC<DataViewTextFilterProps> = ({
2830
showToolbarItem,
2931
trimValue = true,
3032
ouiaId = 'DataViewTextFilter',
33+
enableShortcut = true,
3134
...props
3235
}: DataViewTextFilterProps) => {
3336
useEffect(() => {
37+
if (!enableShortcut) {
38+
return;
39+
}
40+
3441
const handleKeyDown = (event: KeyboardEvent) => {
3542
// Only handle "/" key when not typing in an input, textarea, or contenteditable element
3643
if (event.key === '/' && !event.ctrlKey && !event.metaKey && !event.altKey) {
@@ -55,7 +62,7 @@ export const DataViewTextFilter: FC<DataViewTextFilterProps> = ({
5562
return () => {
5663
window.removeEventListener('keydown', handleKeyDown);
5764
};
58-
}, [showToolbarItem, filterId]);
65+
}, [showToolbarItem, filterId, enableShortcut]);
5966

6067
return (
6168
<ToolbarFilter

0 commit comments

Comments
 (0)