Ant Design
An Ant Design 6 AutoComplete driven by the KDS address store, ready to use as a Form.Item control.
Both fields are real Ant Design Form.Items: the first is required, the second starts filled in from initialValues. Press Save to see the submitted address ids.
Install the block
-
Install the dependencies:
npm i antd@6 @ant-design/icons@6 @dk-address/react -
Copy
address-auto-complete.tsx(the Code tab) into your app. -
Recommended: Ant Design's Select, and so
AutoComplete, rendersautocomplete="new-password"by default, which makes password managers offer to generate a password for the address field. Turn it off app-wide with<ConfigProvider select={{ autoComplete: "off" }}>, as the demo does.
Use it in a form
import { Form } from "antd";
import { AddressAutoComplete } from "./address-auto-complete";
<Form initialValues={{ addressId: savedId }} onFinish={(values) => save(values.addressId)}>
<Form.Item
label="Address"
name="addressId"
rules={[{ required: true, message: "Pick an address from the list" }]}
>
<AddressAutoComplete />
</Form.Item>
</Form>;- The value is the address id. Picking a final address sets it; editing or clearing the text sets it to
undefined, sorequiredmeans "an address was picked", not "something was typed". - Prefill and reset go through the form. An id from
initialValuesorsetFieldsValuefills in the text; resetting clears it. - Details come as the second argument of
onChange(id, details): floor, door, municipality, postal code and the position. - Props: every core option (
kind,municipalityCodes,limit,includeProvisional,debounceMs,client) plusplaceholder,disabled,status,className,styleandid.Form.Itemsetsidandstatus, so its label and error state work like any Ant Design control.
Change your copy
The block is useAddressAutosuggest() connected to Ant Design's AutoComplete. Keep these connections when you edit it, or the caret and the popup stop working:
- Typing and picking stay apart: typing goes through
showSearch.onSearchand picks throughonSelect. Ant Design callsonChangewith the picked option's key, so the block doesn't use it. - Filtering stays off:
showSearch.filterOptionisfalse, since KDS already filtered the results. - The store controls
open: Ant Design asks to close the popup after every pick. The block ignores that request, so the list stays open while the address is refined. - The real
<input>goes toinputRef: that's how the caret moves into the gap after a pick.
To change how the popup looks, edit popupRender, which adds the gap hint above the options, and the option labels, which show an icon and a hint for each type.