From 3902a194ac33ab6638bffdf0b77f244e1599f6ee Mon Sep 17 00:00:00 2001 From: Kristof Csillag Date: Tue, 4 Jul 2023 05:28:08 +0200 Subject: [PATCH] Show warning when search text is too short --- .changelog/671.feature.md | 1 + src/app/components/Search/index.tsx | 57 +++++++++++++++++++---- src/app/components/Search/search-utils.ts | 4 +- src/locales/en/translation.json | 3 ++ 4 files changed, 55 insertions(+), 10 deletions(-) create mode 100644 .changelog/671.feature.md diff --git a/.changelog/671.feature.md b/.changelog/671.feature.md new file mode 100644 index 000000000..27eb9a980 --- /dev/null +++ b/.changelog/671.feature.md @@ -0,0 +1 @@ +Show a warning when search text string it too short diff --git a/src/app/components/Search/index.tsx b/src/app/components/Search/index.tsx index 58e4011c2..765b6de0f 100644 --- a/src/app/components/Search/index.tsx +++ b/src/app/components/Search/index.tsx @@ -10,11 +10,15 @@ import { COLORS } from '../../../styles/theme/colors' import { RouteUtils } from '../../utils/route-utils' import { useScreenSize } from '../../hooks/useScreensize' import HighlightOffIcon from '@mui/icons-material/HighlightOff' +import ErrorIcon from '@mui/icons-material/Error' import IconButton from '@mui/material/IconButton' import { SearchSuggestionsButtons } from './SearchSuggestionsButtons' import { formHelperTextClasses } from '@mui/material/FormHelperText' import { outlinedInputClasses } from '@mui/material/OutlinedInput' import { SearchScope } from '../../../types/searchScope' +import { textSearchMininumLength } from './search-utils' +import Typography from '@mui/material/Typography' +import { isValidBlockHeight } from '../../utils/helpers' export type SearchVariant = 'button' | 'icon' | 'expandable' @@ -101,10 +105,19 @@ const SearchCmp: FC = ({ scope, variant, disabled, onFocusChange: o const [isFocused, setIsFocused] = useState(false) const valueInSearchParams = useSearchParams()[0].get('q') ?? '' + const valueWithoutPrefix = value.trim() + + const isTooShort = + !!value && valueWithoutPrefix.length < textSearchMininumLength && !isValidBlockHeight(valueWithoutPrefix) + useEffect(() => { setValue(valueInSearchParams) }, [valueInSearchParams]) + const onChange = (newValue: string) => { + setValue(newValue) + } + const onFocusChange = (value: boolean) => { setIsFocused(value) onFocusChangeProp?.(value) @@ -113,7 +126,7 @@ const SearchCmp: FC = ({ scope, variant, disabled, onFocusChange: o const onFormSubmit = (e?: FormEvent) => { e?.preventDefault() if (value) { - navigate(RouteUtils.getSearchRoute(scope, value)) + navigate(RouteUtils.getSearchRoute(scope, valueWithoutPrefix)) } } @@ -133,6 +146,8 @@ const SearchCmp: FC = ({ scope, variant, disabled, onFocusChange: o const searchButtonContent = variant !== 'button' ? : t('search.searchBtnText') + const hasError = isTooShort + return ( = ({ scope, variant, disabled, onFocusChange: o > setValue(e.target.value)} + onChange={e => onChange(e.target.value)} + error={hasError} onFocus={() => onFocusChange(true)} onBlur={() => onFocusChange(false)} InputProps={{ @@ -158,7 +174,7 @@ const SearchCmp: FC = ({ scope, variant, disabled, onFocusChange: o )} - + {searchButtonContent} @@ -178,12 +194,35 @@ const SearchCmp: FC = ({ scope, variant, disabled, onFocusChange: o helperText={ value && value !== valueInSearchParams && ( - { - setValue(suggestion) - }} - /> +
+ {isTooShort && ( + <> + + +   + {t('search.error.tooShort')} + +
+ + )} + { + setValue(suggestion) + }} + /> +
) } /> diff --git a/src/app/components/Search/search-utils.ts b/src/app/components/Search/search-utils.ts index a54fe68b0..ba6afd39e 100644 --- a/src/app/components/Search/search-utils.ts +++ b/src/app/components/Search/search-utils.ts @@ -44,6 +44,8 @@ export const searchSuggestionTerms: Record { // Remove localized digit group separators @@ -85,7 +87,7 @@ export const validateAndNormalize = { } }, evmTokenNameFragment: (searchTerm: string) => { - if (searchTerm?.length > 2) { + if (searchTerm?.length >= textSearchMininumLength) { return searchTerm.toLowerCase() } }, diff --git a/src/locales/en/translation.json b/src/locales/en/translation.json index c5e7ae439..4ce7a708d 100644 --- a/src/locales/en/translation.json +++ b/src/locales/en/translation.json @@ -374,6 +374,9 @@ }, "search": { "placeholder": "Address, Block, Contract, Txn Hash, Transaction ID, Token name, etc", + "error": { + "tooShort": "Please enter either at least 3 characters or a number in order to perform a search." + }, "mobilePlaceholder": "Search Address, Block, Txn, Token, etc", "noResults": { "header": "No results found",