|
| 1 | +import * as combobox from "@zag-js/combobox" |
| 2 | +import { Portal, mergeProps, normalizeProps, useMachine } from "@zag-js/hono-jsx" |
| 3 | +import { useId, useMemo, useState } from "hono/jsx" |
| 4 | +import { createFilter } from "@zag-js/i18n-utils" |
| 5 | + |
| 6 | +interface ComboboxProps extends Omit<combobox.Props, "id" | "collection"> {} |
| 7 | + |
| 8 | +export default function Combobox(props: ComboboxProps) { |
| 9 | + const [options, setOptions] = useState(comboboxData) |
| 10 | + |
| 11 | + const filter = createFilter({ sensitivity: "base" }) |
| 12 | + |
| 13 | + const collection = useMemo( |
| 14 | + () => |
| 15 | + combobox.collection({ |
| 16 | + items: options, |
| 17 | + itemToValue: (item) => item.code, |
| 18 | + itemToString: (item) => item.label, |
| 19 | + }), |
| 20 | + [options], |
| 21 | + ) |
| 22 | + |
| 23 | + const service = useMachine(combobox.machine, { |
| 24 | + id: useId(), |
| 25 | + collection, |
| 26 | + onInputValueChange({ inputValue }) { |
| 27 | + const filtered = comboboxData.filter((item) => filter.contains(item.label, inputValue)) |
| 28 | + setOptions(filtered.length > 0 ? filtered : comboboxData) |
| 29 | + }, |
| 30 | + placeholder: "Type or select country", |
| 31 | + ...props, |
| 32 | + }) |
| 33 | + |
| 34 | + const api = combobox.connect(service, normalizeProps) |
| 35 | + |
| 36 | + const triggerProps = mergeProps(api.getTriggerProps(), { |
| 37 | + onClick() { |
| 38 | + setOptions(comboboxData) |
| 39 | + }, |
| 40 | + }) |
| 41 | + |
| 42 | + return ( |
| 43 | + <div> |
| 44 | + <div {...api.getRootProps()}> |
| 45 | + <label {...api.getLabelProps()}>Nationality</label> |
| 46 | + <div {...api.getControlProps()}> |
| 47 | + <input {...api.getInputProps()} /> |
| 48 | + <button {...triggerProps}> |
| 49 | + <CaretIcon /> |
| 50 | + </button> |
| 51 | + </div> |
| 52 | + </div> |
| 53 | + <Portal> |
| 54 | + <div {...api.getPositionerProps()}> |
| 55 | + {options.length > 0 && ( |
| 56 | + <ul {...api.getContentProps()}> |
| 57 | + {options.map((item, index) => ( |
| 58 | + <li key={`${item.code}:${index}`} {...api.getItemProps({ item })}> |
| 59 | + {item.label} |
| 60 | + </li> |
| 61 | + ))} |
| 62 | + </ul> |
| 63 | + )} |
| 64 | + </div> |
| 65 | + </Portal> |
| 66 | + </div> |
| 67 | + ) |
| 68 | +} |
| 69 | + |
| 70 | +const comboboxData = [ |
| 71 | + { label: "Zambia", code: "ZA" }, |
| 72 | + { label: "Benin", code: "BN" }, |
| 73 | + { label: "Canada", code: "CA" }, |
| 74 | + { label: "United States", code: "US" }, |
| 75 | + { label: "Japan", code: "JP" }, |
| 76 | + { label: "Nigeria", code: "NG" }, |
| 77 | + { label: "Albania", code: "AL" }, |
| 78 | + { label: "Algeria", code: "DZ" }, |
| 79 | + { label: "American Samoa", code: "AS" }, |
| 80 | + { label: "Andorra", code: "AD" }, |
| 81 | + { label: "Angola", code: "AO" }, |
| 82 | + { label: "Anguilla", code: "AI" }, |
| 83 | + { label: "Antarctica", code: "AQ" }, |
| 84 | + { label: "Australia", code: "AU" }, |
| 85 | + { label: "Austria", code: "AT" }, |
| 86 | + { label: "Azerbaijan", code: "AZ" }, |
| 87 | + { label: "Bahamas", code: "BS" }, |
| 88 | + { label: "Bahrain", code: "BH" }, |
| 89 | + { label: "Madagascar", code: "MG" }, |
| 90 | + { label: "Malawi", code: "MW" }, |
| 91 | + { label: "Malaysia", code: "MY" }, |
| 92 | + { label: "Maldives", code: "MV" }, |
| 93 | + { label: "Mali", code: "ML" }, |
| 94 | + { label: "Malta", code: "MT" }, |
| 95 | + { label: "Togo", code: "TG" }, |
| 96 | + { label: "Tokelau", code: "TK" }, |
| 97 | + { label: "Tonga", code: "TO" }, |
| 98 | + { label: "Trinidad and Tobago", code: "TT" }, |
| 99 | + { label: "Tunisia", code: "TN" }, |
| 100 | +] |
| 101 | + |
| 102 | +const CaretIcon = () => ( |
| 103 | + <svg |
| 104 | + stroke="currentColor" |
| 105 | + fill="currentColor" |
| 106 | + strokeWidth="0" |
| 107 | + viewBox="0 0 1024 1024" |
| 108 | + height="1em" |
| 109 | + width="1em" |
| 110 | + xmlns="http://www.w3.org/2000/svg" |
| 111 | + > |
| 112 | + <path d="M840.4 300H183.6c-19.7 0-30.7 20.8-18.5 35l328.4 380.8c9.4 10.9 27.5 10.9 37 0L858.9 335c12.2-14.2 1.2-35-18.5-35z"></path> |
| 113 | + </svg> |
| 114 | +) |
0 commit comments