Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MPDX-7837] Adds close adornment to base search text field. #869

Merged
merged 6 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/components/common/SearchBox/SearchBox.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { ThemeProvider } from '@mui/material/styles';
import { render } from '@testing-library/react';
import { render, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import theme from '../../../theme';
import { SearchBox } from './SearchBox';
Expand Down Expand Up @@ -29,7 +29,7 @@ it('triggers onChange', async () => {
const inputText = 'name';
const placeholderText = 'placeholder';

const { getByRole } = render(
const { getByRole, getByTestId } = render(
<ThemeProvider theme={theme}>
<SearchBox
showContactSearchIcon={false}
Expand All @@ -41,12 +41,11 @@ it('triggers onChange', async () => {
);

const textbox = getByRole('textbox');

expect(textbox).toHaveValue('');

userEvent.type(textbox, inputText);
await waitFor(() => expect(onChange).toHaveBeenCalledWith(inputText));

await new Promise((resolve) => setTimeout(resolve, 300));

expect(onChange).toHaveBeenCalledWith(inputText);
userEvent.click(getByTestId('SearchInputCloseButton'));
expect(textbox).toHaveValue('');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also test that onChange gets called with '' to prove that the bug I mentioned below gets fixed.

expect(onChange).toHaveBeenCalledWith('');
});
23 changes: 22 additions & 1 deletion src/components/common/SearchBox/SearchBox.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React, { useState } from 'react';
import { mdiAccountSearch } from '@mdi/js';
import Icon from '@mdi/react';
import Close from '@mui/icons-material/Close';
import SearchIcon from '@mui/icons-material/Search';
import { InputAdornment, TextField } from '@mui/material';
import { IconButton, InputAdornment, TextField } from '@mui/material';
import { styled } from '@mui/material/styles';
import { useTranslation } from 'react-i18next';

Expand Down Expand Up @@ -41,6 +42,7 @@ export const SearchBox: React.FC<SearchBoxProps> = ({
<SearchInput
size="small"
variant="outlined"
sx={{ width: { sm: '27ch', md: '31ch' } }}
onChange={(e) => handleOnChange(e.target.value)}
placeholder={placeholder ?? t('Search')}
value={currentSearchTerm}
Expand All @@ -54,6 +56,25 @@ export const SearchBox: React.FC<SearchBoxProps> = ({
)}
</InputAdornment>
),
endAdornment: (
<InputAdornment position="end">
{currentSearchTerm && (
<IconButton
onClick={() => handleOnChange('')}
data-testid="SearchInputCloseButton"
>
<Close
titleAccess={t('Close')}
sx={{
width: 14,
height: 14,
color: 'text.primary',
}}
/>
</IconButton>
dr-bizz marked this conversation as resolved.
Show resolved Hide resolved
)}
</InputAdornment>
),
}}
/>
);
Expand Down
Loading