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

disallowed colon #1931

Merged
merged 5 commits into from
Jun 30, 2021
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import { render, screen, wait } from '@testing-library/react';
import { render, screen, wait } from './util/test-utils';

import userEvent from '@testing-library/user-event';
import SearchBar from './SearchBar';

Expand Down Expand Up @@ -34,7 +35,8 @@ jest.mock('react-router-dom', () => ({

describe('<SearchBar />', () => {
it('Should open filters modal', async () => {
render(<AppTestComponent />);
render(<AppTestComponent />,
{initialState: {app: { filterBreadcrumbs: [], searchQuery: ''}}},);

userEvent.click(screen.getByRole('button', { name: /Filter/i }));

Expand Down
54 changes: 51 additions & 3 deletions verification/curator-service/ui/src/components/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ import { useDebounce } from '../hooks/useDebounce';
import FiltersModal from './FiltersModal';
import { searchQueryToURL, URLToSearchQuery } from './util/searchQuery';
import { useLocation, useHistory } from 'react-router-dom';
import { KeyboardEvent } from 'react';
import { KeyboardEvent, ChangeEvent } from 'react';
import { useSelector } from 'react-redux';
import {
selectFilterBreadcrumbs,
} from './App/redux/selectors';

const searchBarStyles = makeStyles((theme: Theme) => ({
searchRoot: {
Expand All @@ -38,6 +42,9 @@ const searchBarStyles = makeStyles((theme: Theme) => ({
activeButton: {
fontWeight: 'bold',
},
multilineColor: {
color: 'red',
},
}));

const StyledSearchTextField = withStyles((theme: Theme) => ({
Expand Down Expand Up @@ -94,6 +101,18 @@ export default function SearchBar({
const [modalAlert, setModalAlert] = useState<boolean>(false);
const guideButtonRef = React.useRef<HTMLButtonElement>(null);

const [searchError, setSearchError] = useState<boolean>(false);

const filtersBreadcrumb = useSelector(selectFilterBreadcrumbs);

useEffect(() => {
if (filtersBreadcrumb.length > 0) {
setSearchError(false)
return;
}

}, [filtersBreadcrumb]);

// Set search query debounce to 1000ms
const debouncedSearch = useDebounce(searchInput, 2000);

Expand Down Expand Up @@ -135,7 +154,9 @@ export default function SearchBar({
};

const disallowFilteringInSearchBar = (
e: KeyboardEvent<HTMLInputElement>,
e:
| ChangeEvent<HTMLInputElement | HTMLTextAreaElement>
| KeyboardEvent<HTMLInputElement>,
) => {
e.preventDefault();
setIsUserTyping(false);
Expand All @@ -147,17 +168,43 @@ export default function SearchBar({
setModalAlert(shouldTheAlertStillBeOpen);
}

function checkIfThereIsColon(
event: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>,
eventTargetValue: string,
) {
let searchStringStrippedOutColon = eventTargetValue;
if (eventTargetValue.includes(':')) {
searchStringStrippedOutColon = eventTargetValue.replace(
/:/g,
'',
);
setSearchError(true);
disallowFilteringInSearchBar(event);
} else {
setSearchError(false);
}

return searchStringStrippedOutColon;
}

return (
<>
<div className={classes.searchRoot}>
<StyledSearchTextField
error={searchError}
helperText={
searchError &&
'Incorrect entry. ":" characters have been removed. Please use filters instead.'
}
id="search-field"
data-testid="searchbar"
name="searchbar"
onKeyPress={handleKeyPress}
autoComplete="off"
onChange={(event): void => {
setSearchInput(event.target.value);
setSearchInput(
checkIfThereIsColon(event, event.target.value),
);
}}
onKeyDown={(e: KeyboardEvent<HTMLInputElement>) => {
if (!isUserTyping) {
Expand All @@ -173,6 +220,7 @@ export default function SearchBar({
fullWidth
InputProps={{
margin: 'dense',
className:clsx(searchError && classes.multilineColor),
startAdornment: (
<>
<StyledInputAdornment position="start">
Expand Down