Forms
Combobox
A select you can type into, for long lists. Matching ignores case, accents and Arabic letter forms (أ/ا, ة/ه, ى/ي). Server search: onQueryChange gets each typed query, options are the results as given, and loading shows “Searching”; the app debounces the query. valueLabel names a chosen value that is not in the results. limit shows the first matches and how many more there are. onCreate adds a last option, Add «text», when no option has that label.
import { Combobox } from "@percentsa/ui";Examples
Type to filter
Code
export function ComboboxExample() {
const t = useT();
const [bank, setBank] = useState<string | null>(null);
return (
<Field label={t("Bank", "البنك")} hint={t("Type to filter", "اكتب للتصفية")} className="w-full max-w-xs">
<Combobox value={bank} onValueChange={setBank} name="bank" options={BANKS.map(([value, en, ar]) => ({ value, label: t(en, ar) }))} />
</Field>
);
}
const CITIES = [
["Riyadh", "الرياض"],
["Jeddah", "جدة"],
["Makkah", "مكة المكرمة"],
["Madinah", "المدينة المنورة"],
["Dammam", "الدمام"],
["Khobar", "الخبر"],
["Dhahran", "الظهران"],
["Taif", "الطائف"],
["Tabuk", "تبوك"],
["Abha", "أبها"],
["Khamis Mushait", "خميس مشيط"],
["Buraidah", "بريدة"],
["Hail", "حائل"],
["Jazan", "جازان"],
["Najran", "نجران"],
["Al Ahsa", "الأحساء"],
] as const;8 results at a time; type a new city to add it
Code
export function ComboboxAsyncExample() {
const t = useT();
const [added, setAdded] = useState<string[]>([]);
const all: ComboboxOption[] = [...CITIES.map(([en, ar]) => t(en, ar)), ...added].map((label) => ({ value: label, label }));
const [results, setResults] = useState<ComboboxOption[] | null>(null);
const [loading, setLoading] = useState(false);
const [city, setCity] = useState<string | null>(null);
const timer = useRef<ReturnType<typeof setTimeout>>(undefined);
// Stands in for a server search. A real app debounces the query in its data hook.
const search = (query: string) => {
clearTimeout(timer.current);
setLoading(true);
timer.current = setTimeout(() => {
setResults(all.filter((o) => searchKey(o.label).includes(searchKey(query))));
setLoading(false);
}, 400);
};
return (
<Field label={t("City", "المدينة")} hint={t("8 results at a time; type a new city to add it", "8 نتائج في كل مرة؛ اكتب مدينة جديدة لإضافتها")} className="w-full max-w-xs">
<Combobox
value={city}
onValueChange={setCity}
options={results ?? all}
onQueryChange={search}
loading={loading}
valueLabel={city ?? undefined}
limit={8}
onCreate={(name) => {
setAdded((list) => [...list, name]);
setCity(name);
}}
name="city"
/>
</Field>
);
}
const CITY_DETAILS = [
["Riyadh", "الرياض", "Riyadh Province", "منطقة الرياض"],
["Jeddah", "جدة", "Makkah Province", "منطقة مكة المكرمة"],
["Dammam", "الدمام", "Eastern Province", "المنطقة الشرقية"],
["Khobar", "الخبر", "Eastern Province", "المنطقة الشرقية"],
["Abha", "أبها", "Asir Province", "منطقة عسير"],
["Tabuk", "تبوك", "Tabuk Province", "منطقة تبوك"],
] as const;
const useCitySuggestions = (): AutocompleteSuggestion[] => {
const t = useT();
return CITY_DETAILS.map(([en, ar, detailEn, detailAr]) => ({ value: t(en, ar), detail: t(detailEn, detailAr) }));
};