Skip to content

Commit f2aabb7

Browse files
authored
fix(JsonViewer): handle case sensitive search (#1966)
1 parent 2b1eba3 commit f2aabb7

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

src/components/JsonViewer/JsonViewer.tsx

+25-3
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ export function JsonViewer({
9696
extraTools,
9797
collapsedInitially,
9898
}: JsonViewerProps) {
99-
const [caseSensitiveSearch] = useSetting(CASE_SENSITIVE_JSON_SEARCH, false);
99+
const [caseSensitiveSearch, setCaseSensitiveSearch] = useSetting(
100+
CASE_SENSITIVE_JSON_SEARCH,
101+
false,
102+
);
100103

101104
const [collapsedState, setCollapsedState] = React.useState<CollapsedState>(() => {
102105
if (collapsedInitially) {
@@ -155,13 +158,16 @@ export function JsonViewer({
155158
};
156159

157160
const updateState = (
158-
changedState: Partial<Pick<State, 'collapsedState' | 'filter' | 'matchIndex'>>,
161+
changedState: Partial<Pick<State, 'collapsedState' | 'filter' | 'matchIndex'>> & {
162+
caseSensitive?: boolean;
163+
},
159164
cb?: () => void,
160165
) => {
161166
const {
162167
collapsedState: newCollapsedState,
163168
matchIndex: newMatchIndex,
164169
filter: newFilter,
170+
caseSensitive: newCaseSensitive,
165171
} = changedState;
166172

167173
if (newCollapsedState !== undefined) {
@@ -173,7 +179,15 @@ export function JsonViewer({
173179
if (newFilter !== undefined) {
174180
setFilter(newFilter);
175181
}
176-
setState(calculateState(value, newCollapsedState ?? collapsedState, newFilter ?? filter));
182+
const caseSensitive = newCaseSensitive ?? caseSensitiveSearch;
183+
setState(
184+
calculateState(
185+
value,
186+
newCollapsedState ?? collapsedState,
187+
newFilter ?? filter,
188+
caseSensitive,
189+
),
190+
);
177191

178192
cb?.();
179193
};
@@ -254,6 +268,12 @@ export function JsonViewer({
254268
}
255269
};
256270

271+
const handleUpdateCaseSensitive = () => {
272+
const newCaseSensitive = !caseSensitiveSearch;
273+
setCaseSensitiveSearch(newCaseSensitive);
274+
updateState({caseSensitive: newCaseSensitive});
275+
};
276+
257277
const renderToolbar = () => {
258278
return (
259279
<Flex gap={2} wrap="nowrap" className={block('toolbar')}>
@@ -279,6 +299,8 @@ export function JsonViewer({
279299
onKeyDown={onEnterKeyDown}
280300
onNextMatch={onNextMatch}
281301
onPrevMatch={onPrevMatch}
302+
caseSensitive={caseSensitiveSearch}
303+
onUpdateCaseSensitive={handleUpdateCaseSensitive}
282304
/>
283305
)}
284306
<span className={block('extra-tools')}>{extraTools}</span>

src/components/JsonViewer/components/Filter.tsx

+16-10
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import React from 'react';
22

33
import {ActionTooltip, Button, Flex, Icon, TextInput} from '@gravity-ui/uikit';
44

5-
import {CASE_SENSITIVE_JSON_SEARCH} from '../../../utils/constants';
6-
import {useSetting} from '../../../utils/hooks';
75
import {block} from '../constants';
86
import i18n from '../i18n';
97

@@ -19,16 +17,24 @@ interface FilterProps {
1917
onKeyDown?: (event: React.KeyboardEvent<HTMLInputElement>) => void;
2018
onNextMatch?: (_event: unknown, diff?: number) => void;
2119
onPrevMatch?: (_event: unknown, diff?: number) => void;
20+
caseSensitive?: boolean;
21+
onUpdateCaseSensitive: VoidFunction;
2222
}
2323

2424
export const Filter = React.forwardRef<HTMLInputElement, FilterProps>(function Filter(
25-
{matchIndex, matchedRows, value, onUpdate, onKeyDown, onNextMatch, onPrevMatch},
25+
{
26+
matchIndex,
27+
matchedRows,
28+
value,
29+
onUpdate,
30+
onKeyDown,
31+
onNextMatch,
32+
onPrevMatch,
33+
caseSensitive,
34+
onUpdateCaseSensitive,
35+
},
2636
ref,
2737
) {
28-
const [caseSensitiveSearch, setCaseSensitiveSearch] = useSetting(
29-
CASE_SENSITIVE_JSON_SEARCH,
30-
false,
31-
);
3238
const count = matchedRows.length;
3339
const matchPosition = count ? 1 + (matchIndex % count) : 0;
3440
return (
@@ -47,15 +53,15 @@ export const Filter = React.forwardRef<HTMLInputElement, FilterProps>(function F
4753
endContent={
4854
<ActionTooltip
4955
title={
50-
caseSensitiveSearch
56+
caseSensitive
5157
? i18n('context_case-sensitive-search')
5258
: i18n('context_case-sensitive-search-disabled')
5359
}
5460
>
5561
<Button
5662
view="flat-secondary"
57-
onClick={() => setCaseSensitiveSearch(!caseSensitiveSearch)}
58-
selected={caseSensitiveSearch}
63+
onClick={onUpdateCaseSensitive}
64+
selected={caseSensitive}
5965
>
6066
<Icon data={FontCaseIcon} />
6167
</Button>

0 commit comments

Comments
 (0)