EccDropDown
EccDropDown lets the user choose one or several options - as native select, as searchable list or with a server-side search.
import { EccDropDown } from "@/ecc/components/forms/EccDropDown";
The component uses the basic props of all field components - see Component Basics.
EccDropDownSingle (@/ecc/components/forms/EccDropDownSingle) is the same component under its former name.
Options
options is an array of strings or objects (or a JSON string of such an array). keyAttribute and valueAttribute say which property is the key (written to the output) and which is the label. "$" (default) means the item itself.
// strings: key and label are the same
<EccDropDown fieldLabel="Priority" options={["LOW", "MEDIUM", "HIGH"]} valueOut="purchase.priority" />
// objects
const types = [
{ id: "", label: "- please choose -" },
{ id: "it-hardware", label: "IT Hardware" },
{ id: "it-software", label: "IT Software" },
{ id: "medical-devices", label: "Medical devices" },
];
<EccDropDown id="purchase-request-type" fieldLabel="Request type"
options={types} keyAttribute="id" valueAttribute="label"
value="purchase.requestType" data={draft} valueOut mandatory />
value + data select the stored key when a draft is opened; initValue or selected set a default.
Search
// local search in a long list
<EccDropDown fieldLabel="Cost center" options={costCenters} keyAttribute="nr" valueAttribute="name"
typeAhead placeholder="Search cost center" valueOut="purchase.costCenter" />
// server-side search, called 250 ms after typing stops
async function searchSuppliers(query: string): Promise<unknown[]> {
const res = await fetchSuppliers(query); // your ApiService function
return res.data;
}
<EccDropDown fieldLabel="Supplier" typeAhead typeAheadService={searchSuppliers}
keyAttribute="id" valueAttribute="name" valueOut="purchase.supplier" />
Multiple selection
<EccDropDown fieldLabel="Locations" options={locations} keyAttribute="id" valueAttribute="name"
selectionMode="multiple" initValue={["ber", "muc"]} valueOut="purchase.locations"
onMultiChange={(keys) => console.log(keys)} />
Output: an array of keys, e.g. ["ber", "muc"].
Loading state
const [items, setItems] = useState<unknown[]>([]);
const [loading, setLoading] = useState(true);
useEffect(() => { loadCostCenters().then((d) => { setItems(d); setLoading(false); }); }, []);
<EccDropDown fieldLabel="Cost center" options={items} loading={loading} keyAttribute="nr" valueAttribute="name" />
Props
Basic props (id, fieldLabel, mandatory, valueOut, hidden, allowedGroups, …), plus:
|
| - | items |
| path |
| key of an item, written to the output |
| path |
| label of an item |
| path, object | - | preselect the stored key(s) |
|
| - | preselected key (single) |
|
| - | default if nothing else selects |
|
|
| one or several |
|
|
| searchable list |
|
| - | server-side search |
|
| - | hint in the search field when nothing is selected |
|
| - | single selection changed |
|
| - | multiple selection changed |
|
|
| spinner, disabled |
|
|
| disabled |
Without typeAhead and in single mode the component renders a native select - best for short lists.
Related
super component: basic props | |
the same options, all visible at once |
On this page
- EccDropDown