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 5, 2023
1 parent 0929a78 commit f5cd6c1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 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
25 changes: 18 additions & 7 deletions src/app/components/Search/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ 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'

export type SearchVariant = 'button' | 'icon' | 'expandable'

Expand Down Expand Up @@ -101,20 +102,27 @@ 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

useEffect(() => {
setValue(valueInSearchParams)
}, [valueInSearchParams])

const onChange = (newValue: string) => {
setValue(newValue)
}

const onFocusChange = (value: boolean) => {
setIsFocused(value)
onFocusChangeProp?.(value)
}

const onFormSubmit = (e?: FormEvent) => {
e?.preventDefault()
if (value) {
navigate(RouteUtils.getSearchRoute(scope, value))
}
if (!value || isTooShort) return
navigate(RouteUtils.getSearchRoute(scope, valueWithoutPrefix))
}

const onClearValue = () => {
Expand Down Expand Up @@ -142,7 +150,7 @@ const SearchCmp: FC<SearchProps> = ({ scope, variant, disabled, onFocusChange: o
>
<TextField
value={value}
onChange={e => setValue(e.target.value)}
onChange={e => onChange(e.target.value)}
onFocus={() => onFocusChange(true)}
onBlur={() => onFocusChange(false)}
InputProps={{
Expand All @@ -158,7 +166,7 @@ const SearchCmp: FC<SearchProps> = ({ scope, variant, disabled, onFocusChange: o
<HighlightOffIcon />
</IconButton>
)}
<SearchButton disabled={disabled} searchVariant={variant} type="submit">
<SearchButton disabled={disabled || isTooShort} searchVariant={variant} type="submit">
{searchButtonContent}
</SearchButton>
</>
Expand All @@ -177,14 +185,17 @@ const SearchCmp: FC<SearchProps> = ({ scope, variant, disabled, onFocusChange: o
}}
helperText={
value &&
value !== valueInSearchParams && (
value !== valueInSearchParams &&
(isTooShort ? (
t('search.error.tooShort')
) : (
<SearchSuggestionsButtons
scope={scope}
onClickSuggestion={suggestion => {
setValue(suggestion)
}}
/>
)
))
}
/>
</SearchForm>
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 @@ -369,6 +369,9 @@
},
"search": {
"placeholder": "Address, Block, Contract, Txn Hash, Transaction ID, Token name, etc",
"error": {
"tooShort": "Please enter at least 3 characters for searching"
},
"mobilePlaceholder": "Search Address, Block, Txn, Token, etc",
"noResults": {
"header": "No results found",
Expand Down

0 comments on commit f5cd6c1

Please sign in to comment.