Skip to content

Commit

Permalink
Show warning when search text is too short
Browse files Browse the repository at this point in the history
  • Loading branch information
csillag committed Jul 6, 2023
1 parent a5ce455 commit 3902a19
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 10 deletions.
1 change: 1 addition & 0 deletions .changelog/671.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Show a warning when search text string it too short
57 changes: 48 additions & 9 deletions src/app/components/Search/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -101,10 +105,19 @@ const SearchCmp: FC<SearchProps> = ({ 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)
Expand All @@ -113,7 +126,7 @@ const SearchCmp: FC<SearchProps> = ({ scope, variant, disabled, onFocusChange: o
const onFormSubmit = (e?: FormEvent) => {
e?.preventDefault()
if (value) {
navigate(RouteUtils.getSearchRoute(scope, value))
navigate(RouteUtils.getSearchRoute(scope, valueWithoutPrefix))
}
}

Expand All @@ -133,6 +146,8 @@ const SearchCmp: FC<SearchProps> = ({ scope, variant, disabled, onFocusChange: o
const searchButtonContent =
variant !== 'button' ? <SearchIcon sx={{ color: COLORS.grayMediumLight }} /> : t('search.searchBtnText')

const hasError = isTooShort

return (
<SearchForm
searchVariant={variant}
Expand All @@ -142,7 +157,8 @@ const SearchCmp: FC<SearchProps> = ({ scope, variant, disabled, onFocusChange: o
>
<TextField
value={value}
onChange={e => setValue(e.target.value)}
onChange={e => onChange(e.target.value)}
error={hasError}
onFocus={() => onFocusChange(true)}
onBlur={() => onFocusChange(false)}
InputProps={{
Expand All @@ -158,7 +174,7 @@ const SearchCmp: FC<SearchProps> = ({ scope, variant, disabled, onFocusChange: o
<HighlightOffIcon />
</IconButton>
)}
<SearchButton disabled={disabled} searchVariant={variant} type="submit">
<SearchButton disabled={disabled || hasError} searchVariant={variant} type="submit">
{searchButtonContent}
</SearchButton>
</>
Expand All @@ -178,12 +194,35 @@ const SearchCmp: FC<SearchProps> = ({ scope, variant, disabled, onFocusChange: o
helperText={
value &&
value !== valueInSearchParams && (
<SearchSuggestionsButtons
scope={scope}
onClickSuggestion={suggestion => {
setValue(suggestion)
}}
/>
<div>
{isTooShort && (
<>
<Typography
component="span"
sx={{
display: 'inline-flex',
color: COLORS.errorIndicatorBackground,
background: COLORS.linen,
fontSize: 12,
alignItems: 'center',
verticalAlign: 'middle',
mb: 3,
}}
>
<ErrorIcon />
&nbsp;
{t('search.error.tooShort')}
</Typography>
<br />
</>
)}
<SearchSuggestionsButtons
scope={scope}
onClickSuggestion={suggestion => {
setValue(suggestion)
}}
/>
</div>
)
}
/>
Expand Down
4 changes: 3 additions & 1 deletion src/app/components/Search/search-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export const searchSuggestionTerms: Record<Network, Partial<Record<Layer, LayerS
},
} satisfies SpecifiedPerEnabledLayer

export const textSearchMininumLength = 3

export const validateAndNormalize = {
blockHeight: (searchTerm: string) => {
// Remove localized digit group separators
Expand Down Expand Up @@ -85,7 +87,7 @@ export const validateAndNormalize = {
}
},
evmTokenNameFragment: (searchTerm: string) => {
if (searchTerm?.length > 2) {
if (searchTerm?.length >= textSearchMininumLength) {
return searchTerm.toLowerCase()
}
},
Expand Down
3 changes: 3 additions & 0 deletions src/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 3902a19

Please sign in to comment.