Skip to content

Commit 1b36d3a

Browse files
committed
FE: Fix tests, icon styles
1 parent 57ede6c commit 1b36d3a

File tree

6 files changed

+42
-21
lines changed

6 files changed

+42
-21
lines changed

frontend/src/components/Topics/List/ListPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const ListPage: React.FC = () => {
6565
<ControlPanelWrapper hasInput>
6666
<Search
6767
placeholder="Search by Topic Name"
68-
actions={<Fts resourceName="topics" />}
68+
extraActions={<Fts resourceName="topics" />}
6969
/>
7070
<label>
7171
<Switch

frontend/src/components/common/Fts/Fts.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22
import FtsIcon from 'components/common/Icons/FtsIcon';
33
import styled from 'styled-components';
44

5-
import useFts from './useFts';
5+
import useFts, { FtsAvailableResource } from './useFts';
66

77
export const IconWrapper = styled.span.attrs<{ active: boolean }>(() => ({
88
role: 'button',
@@ -11,15 +11,14 @@ export const IconWrapper = styled.span.attrs<{ active: boolean }>(() => ({
1111
display: inline-block;
1212
&:hover {
1313
cursor: pointer;
14-
color: ${({ theme }) => theme.icons.ftsIcon.active};
1514
}
1615
color: ${(props) =>
1716
props.active
1817
? props.theme.icons.ftsIcon.active
1918
: props.theme.icons.ftsIcon.normal};
2019
`;
2120

22-
const Fts = ({ resourceName }: { resourceName: string }) => {
21+
const Fts = ({ resourceName }: { resourceName: FtsAvailableResource }) => {
2322
const { handleSwitch, isFtsEnabled } = useFts(resourceName);
2423

2524
return (

frontend/src/components/common/Fts/useFts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import { useSearchParams } from 'react-router-dom';
33

4-
type FtsAvailableResource = 'topics';
4+
export type FtsAvailableResource = 'topics';
55

66
const storageName = 'kafbat-ui_fts';
77

frontend/src/components/common/Search/Search.tsx

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import React, { ComponentRef, ReactNode, useEffect, useRef } from 'react';
1+
import React, {
2+
ComponentRef,
3+
ReactNode,
4+
useEffect,
5+
useRef,
6+
useState,
7+
} from 'react';
28
import { useDebouncedCallback } from 'use-debounce';
39
import Input from 'components/common/Input/Input';
410
import { useSearchParams } from 'react-router-dom';
@@ -19,10 +25,11 @@ const Search: React.FC<SearchProps> = ({
1925
disabled = false,
2026
value,
2127
onChange,
22-
actions,
28+
extraActions,
2329
}) => {
2430
const [searchParams, setSearchParams] = useSearchParams();
2531
const ref = useRef<ComponentRef<'input'>>(null);
32+
const [showIcon, setShowIcon] = useState(!!value || !!searchParams.get('q'));
2633

2734
useEffect(() => {
2835
if (ref.current !== null && value) {
@@ -31,6 +38,7 @@ const Search: React.FC<SearchProps> = ({
3138
}, [value]);
3239

3340
const handleChange = useDebouncedCallback((e) => {
41+
setShowIcon(!!e.target.value);
3442
if (ref.current != null) {
3543
ref.current.value = e.target.value;
3644
}
@@ -52,16 +60,16 @@ const Search: React.FC<SearchProps> = ({
5260
searchParams.set('q', '');
5361
setSearchParams(searchParams);
5462
}
63+
5564
if (ref.current != null) {
5665
ref.current.value = '';
5766
}
5867
if (onChange) {
5968
onChange('');
6069
}
70+
setShowIcon(false);
6171
};
6272

63-
const showClearIcon = searchParams.get('q');
64-
6573
return (
6674
<Input
6775
type="text"
@@ -74,13 +82,16 @@ const Search: React.FC<SearchProps> = ({
7482
search
7583
actions={
7684
<S.Actions>
77-
{showClearIcon && (
78-
<S.IconButtonWrapper onClick={clearSearchValue}>
85+
{showIcon && (
86+
<S.IconButtonWrapper
87+
onClick={clearSearchValue}
88+
data-testid="search-clear-button"
89+
>
7990
<CloseCircleIcon />
8091
</S.IconButtonWrapper>
8192
)}
8293

83-
{actions}
94+
{extraActions}
8495
</S.Actions>
8596
}
8697
/>

frontend/src/components/common/Search/__tests__/Search.spec.tsx

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Search from 'components/common/Search/Search';
22
import React from 'react';
33
import { render } from 'lib/testHelpers';
44
import userEvent from '@testing-library/user-event';
5-
import { screen } from '@testing-library/react';
5+
import { screen, fireEvent } from '@testing-library/react';
66
import { useSearchParams } from 'react-router-dom';
77

88
jest.mock('use-debounce', () => ({
@@ -42,23 +42,34 @@ describe('Search', () => {
4242
expect(screen.queryByPlaceholderText('Search')).toBeInTheDocument();
4343
});
4444

45-
it('Clear button is visible', () => {
45+
it('Clear button is not visible by default', async () => {
4646
render(<Search placeholder={placeholder} />);
4747

48-
const clearButton = screen.getByRole('button');
48+
const clearButton = screen.queryByTestId('search-clear-button');
49+
expect(clearButton).not.toBeInTheDocument();
50+
});
51+
52+
it('Clear button is visible if value passed', async () => {
53+
render(<Search placeholder={placeholder} value="text" />);
54+
55+
const clearButton = screen.queryByTestId('search-clear-button');
4956
expect(clearButton).toBeInTheDocument();
5057
});
5158

5259
it('Clear button should clear text from input', async () => {
5360
render(<Search placeholder={placeholder} />);
5461

5562
const searchField = screen.getAllByRole('textbox')[0];
56-
await userEvent.type(searchField, 'some text');
57-
expect(searchField).toHaveValue('some text');
63+
fireEvent.change(searchField, { target: { value: 'hello' } });
64+
expect(searchField).toHaveValue('hello');
5865

59-
const clearButton = screen.getByRole('button');
60-
await userEvent.click(clearButton);
66+
let clearButton = screen.queryByTestId('search-clear-button');
67+
expect(clearButton).toBeInTheDocument();
68+
await userEvent.click(clearButton!);
6169

6270
expect(searchField).toHaveValue('');
71+
72+
clearButton = screen.queryByTestId('search-clear-button');
73+
expect(clearButton).not.toBeInTheDocument();
6374
});
6475
});

frontend/src/theme/theme.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,8 +1544,8 @@ export const darkTheme: ThemeType = {
15441544
},
15451545
menuIcon: Colors.brand[0],
15461546
ftsIcon: {
1547-
normal: Colors.neutral[20],
1548-
active: Colors.brand[70],
1547+
normal: Colors.neutral[50],
1548+
active: Colors.brand[10],
15491549
},
15501550
},
15511551
textArea: {

0 commit comments

Comments
 (0)