[ui][typeahead] add optional minQueryLength parameter

pull/519/head
Yangshun Tay 2 years ago
parent 63b6a61ad3
commit 30c68afed6

@ -41,6 +41,7 @@ export default function CitiesTypeahead({
return ( return (
<Typeahead <Typeahead
label={label} label={label}
minQueryLength={3}
noResultsMessage="No cities found" noResultsMessage="No cities found"
nullable={true} nullable={true}
options={ options={

@ -39,6 +39,7 @@ export default function CountriesTypeahead({
return ( return (
<Typeahead <Typeahead
label="Country" label="Country"
minQueryLength={3}
noResultsMessage="No countries found" noResultsMessage="No countries found"
nullable={true} nullable={true}
options={ options={

@ -28,6 +28,8 @@ type Props = Readonly<{
errorMessage?: React.ReactNode; errorMessage?: React.ReactNode;
isLabelHidden?: boolean; isLabelHidden?: boolean;
label: string; label: string;
// Minimum query length before any results will be shown.
minQueryLength?: number;
noResultsMessage?: string; noResultsMessage?: string;
onQueryChange: ( onQueryChange: (
value: string, value: string,
@ -80,6 +82,7 @@ export default function Typeahead({
errorMessage, errorMessage,
isLabelHidden, isLabelHidden,
label, label,
minQueryLength = 0,
noResultsMessage = 'No results', noResultsMessage = 'No results',
nullable = false, nullable = false,
options, options,
@ -167,46 +170,48 @@ export default function Typeahead({
/> />
</Combobox.Button> </Combobox.Button>
</div> </div>
<Transition {query.length >= minQueryLength && (
afterLeave={() => setQuery('')} <Transition
as={Fragment} afterLeave={() => setQuery('')}
leave="transition ease-in duration-100" as={Fragment}
leaveFrom="opacity-100" leave="transition ease-in duration-100"
leaveTo="opacity-0"> leaveFrom="opacity-100"
<Combobox.Options leaveTo="opacity-0">
className={clsx( <Combobox.Options
'absolute z-10 mt-1 max-h-60 w-full overflow-auto rounded-md bg-white py-1 shadow-lg ring-1 ring-black ring-opacity-5 focus:outline-none', className={clsx(
textSizes[textSize], 'absolute z-10 mt-1 max-h-60 w-full overflow-auto rounded-md bg-white py-1 shadow-lg ring-1 ring-black ring-opacity-5 focus:outline-none',
)}> textSizes[textSize],
{options.length === 0 && query !== '' ? ( )}>
<div className="relative cursor-default select-none py-2 px-4 text-slate-700"> {options.length === 0 && query !== '' ? (
{noResultsMessage} <div className="relative cursor-default select-none py-2 px-4 text-slate-700">
</div> {noResultsMessage}
) : ( </div>
options.map((option) => ( ) : (
<Combobox.Option options.map((option) => (
key={option.id} <Combobox.Option
className={({ active }) => key={option.id}
clsx( className={({ active }) =>
'relative cursor-default select-none py-2 px-4 text-slate-500', clsx(
active && 'bg-slate-100', 'relative cursor-default select-none py-2 px-4 text-slate-500',
) active && 'bg-slate-100',
} )
value={option}> }
{({ selected }) => ( value={option}>
<span {({ selected }) => (
className={clsx( <span
'block truncate', className={clsx(
selected ? 'font-medium' : 'font-normal', 'block truncate',
)}> selected ? 'font-medium' : 'font-normal',
{option.label} )}>
</span> {option.label}
)} </span>
</Combobox.Option> )}
)) </Combobox.Option>
)} ))
</Combobox.Options> )}
</Transition> </Combobox.Options>
</Transition>
)}
</div> </div>
</Combobox> </Combobox>
{errorMessage && ( {errorMessage && (

Loading…
Cancel
Save