@dk-address/react 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +24 -2
- package/dist/address-input.d.ts +99 -0
- package/dist/address-input.js +194 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +3 -0
- package/dist/use-address-autosuggest.d.ts +18 -0
- package/dist/use-address-autosuggest.js +13 -0
- package/dist/use-autosuggest-store.d.ts +12 -0
- package/dist/use-autosuggest-store.js +56 -0
- package/package.json +70 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Dyrhoi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,25 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @dk-address/react
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Unstyled React address autocomplete for Danish addresses, built on [KDS Adressevælger](https://confluence.kds.dk/spaces/ADV/pages/234782998), the replacement for DAWA's autocomplete. It needs React 19 or newer, and no API key.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
npm i @dk-address/react
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```tsx
|
|
10
|
+
import { AddressInput } from "@dk-address/react";
|
|
11
|
+
|
|
12
|
+
export function AddressField() {
|
|
13
|
+
return (
|
|
14
|
+
<AddressInput onSelect={(selection) => console.log(selection.details)}>
|
|
15
|
+
<AddressInput.Label>Address</AddressInput.Label>
|
|
16
|
+
<AddressInput.Input />
|
|
17
|
+
<AddressInput.Popup>
|
|
18
|
+
<AddressInput.List />
|
|
19
|
+
</AddressInput.Popup>
|
|
20
|
+
</AddressInput>
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Read the [quickstart](https://dk-address.dyrhoi.com/docs/quickstart) and the [AddressInput reference](https://dk-address.dyrhoi.com/docs/reference/address-input) to style it, wire it into forms or use it from another component library.
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { AutocompleteClear, AutocompleteContent, AutocompleteEmpty, AutocompleteInput, AutocompleteItem, AutocompleteLabel, AutocompleteList, AutocompleteStatus } from "@ui-sdk/react/autocomplete";
|
|
2
|
+
import { type ComponentProps, type ReactNode } from "react";
|
|
3
|
+
import { type Suggestion } from "@dk-address/client";
|
|
4
|
+
import type { AutosuggestOptions, AutosuggestState, Selection } from "@dk-address/core";
|
|
5
|
+
export interface AddressInputProps extends AutosuggestOptions {
|
|
6
|
+
children?: ReactNode;
|
|
7
|
+
/** The name the text submits under in a form. */
|
|
8
|
+
name?: string;
|
|
9
|
+
required?: boolean;
|
|
10
|
+
disabled?: boolean;
|
|
11
|
+
readOnly?: boolean;
|
|
12
|
+
/** Highlight the first suggestion while typing, so Enter picks it. */
|
|
13
|
+
autoHighlight?: boolean;
|
|
14
|
+
/** Arrow keys wrap from the last suggestion to the first. */
|
|
15
|
+
loop?: boolean;
|
|
16
|
+
}
|
|
17
|
+
declare function AddressInputRoot({ children, name, required, disabled, readOnly, autoHighlight, loop, ...options }: AddressInputProps): import("react").JSX.Element;
|
|
18
|
+
export type AddressInputLabelProps = ComponentProps<typeof AutocompleteLabel>;
|
|
19
|
+
/** Names the input. Renders a `label`. */
|
|
20
|
+
declare function Label(props: AddressInputLabelProps): import("react").JSX.Element;
|
|
21
|
+
export type AddressInputControlProps = ComponentProps<"div">;
|
|
22
|
+
/**
|
|
23
|
+
* Optional box around the input and clear button, for styling them as one field. The popup
|
|
24
|
+
* lines up with it, and `--anchor-width` is its width.
|
|
25
|
+
* Has `data-open` while suggestions show and `data-loading` while searching.
|
|
26
|
+
*/
|
|
27
|
+
declare function Control({ ref, ...props }: AddressInputControlProps): import("react").JSX.Element;
|
|
28
|
+
export type AddressInputInputProps = ComponentProps<typeof AutocompleteInput>;
|
|
29
|
+
/** The text field. Renders an `input` with the combobox role. */
|
|
30
|
+
declare function Input({ ref, ...props }: AddressInputInputProps): import("react").JSX.Element;
|
|
31
|
+
export type AddressInputClearProps = ComponentProps<typeof AutocompleteClear>;
|
|
32
|
+
/** Empties the field and keeps focus in it. Renders a `button`. */
|
|
33
|
+
declare function Clear(props: AddressInputClearProps): import("react").JSX.Element;
|
|
34
|
+
export type AddressInputPopupProps = ComponentProps<typeof AutocompleteContent>;
|
|
35
|
+
/**
|
|
36
|
+
* The suggestions popup, positioned against `Control` when there is one, otherwise against
|
|
37
|
+
* the input. Use `side`, `align` and the offsets to place it, or `anchor` to line it up with
|
|
38
|
+
* another element; `--anchor-width` and `--available-height` help size it.
|
|
39
|
+
*/
|
|
40
|
+
declare function Popup({ anchor, ...props }: AddressInputPopupProps): import("react").JSX.Element;
|
|
41
|
+
export type AddressInputStatusProps = ComponentProps<typeof AutocompleteStatus>;
|
|
42
|
+
/** Announces loading and result counts. Children can be a function of `{ loading, count }`. */
|
|
43
|
+
declare function Status(props: AddressInputStatusProps): import("react").JSX.Element;
|
|
44
|
+
export type AddressInputEmptyProps = ComponentProps<typeof AutocompleteEmpty>;
|
|
45
|
+
/** Shows when a search finds nothing. Children can be a function of `{ empty, loading, query }`. */
|
|
46
|
+
declare function Empty(props: AddressInputEmptyProps): import("react").JSX.Element;
|
|
47
|
+
export type AddressInputListProps = Omit<ComponentProps<typeof AutocompleteList>, "children"> & {
|
|
48
|
+
/** A function of each suggestion, your own items, or nothing for one `Item` per suggestion. */
|
|
49
|
+
children?: ReactNode | ((suggestion: Suggestion, index: number) => ReactNode);
|
|
50
|
+
};
|
|
51
|
+
/** The listbox holding the suggestions. */
|
|
52
|
+
declare function List({ children, ...props }: AddressInputListProps): import("react").JSX.Element;
|
|
53
|
+
export type AddressInputItemProps = Omit<ComponentProps<typeof AutocompleteItem>, "value" | "label"> & {
|
|
54
|
+
suggestion: Suggestion;
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* One suggestion. Shows its label unless you pass children. Has `data-type` with the
|
|
58
|
+
* suggestion's type and `data-highlighted` while highlighted.
|
|
59
|
+
*/
|
|
60
|
+
declare function Item({ suggestion, children, ...props }: AddressInputItemProps): import("react").JSX.Element;
|
|
61
|
+
export interface UseAddressInputResult {
|
|
62
|
+
state: AutosuggestState;
|
|
63
|
+
select(suggestion: Suggestion): Promise<void>;
|
|
64
|
+
/** Prefill from a stored id. */
|
|
65
|
+
selectById(id: string): Promise<Selection | null>;
|
|
66
|
+
clear(): void;
|
|
67
|
+
}
|
|
68
|
+
/** Read the state and act on it from anywhere inside `<AddressInput>`. */
|
|
69
|
+
export declare function useAddressInput(): UseAddressInputResult;
|
|
70
|
+
/**
|
|
71
|
+
* Headless address autosuggest for KDS Adressevælger. Every part takes `className` and
|
|
72
|
+
* `style` (plain, or a function of the part's state) and its element's props.
|
|
73
|
+
*
|
|
74
|
+
* ```tsx
|
|
75
|
+
* <AddressInput onSelect={(selection) => save(selection.details)}>
|
|
76
|
+
* <AddressInput.Label>Address</AddressInput.Label>
|
|
77
|
+
* <AddressInput.Control>
|
|
78
|
+
* <AddressInput.Input />
|
|
79
|
+
* <AddressInput.Clear>×</AddressInput.Clear>
|
|
80
|
+
* </AddressInput.Control>
|
|
81
|
+
* <AddressInput.Popup>
|
|
82
|
+
* <AddressInput.Empty>No addresses found.</AddressInput.Empty>
|
|
83
|
+
* <AddressInput.List />
|
|
84
|
+
* </AddressInput.Popup>
|
|
85
|
+
* </AddressInput>
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
export declare const AddressInput: typeof AddressInputRoot & {
|
|
89
|
+
Label: typeof Label;
|
|
90
|
+
Control: typeof Control;
|
|
91
|
+
Input: typeof Input;
|
|
92
|
+
Clear: typeof Clear;
|
|
93
|
+
Popup: typeof Popup;
|
|
94
|
+
Status: typeof Status;
|
|
95
|
+
Empty: typeof Empty;
|
|
96
|
+
List: typeof List;
|
|
97
|
+
Item: typeof Item;
|
|
98
|
+
};
|
|
99
|
+
export {};
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { Autocomplete, AutocompleteClear, AutocompleteContent, AutocompleteEmpty, AutocompleteInput, AutocompleteItem, AutocompleteLabel, AutocompleteList, AutocompleteStatus, } from "@ui-sdk/react/autocomplete";
|
|
3
|
+
import { createContext, Fragment, use, useCallback, useMemo, useRef, useState, } from "react";
|
|
4
|
+
import { suggestionKey } from "@dk-address/client";
|
|
5
|
+
import { useAddressAutosuggest } from "./use-address-autosuggest.js";
|
|
6
|
+
const AddressInputContext = createContext(null);
|
|
7
|
+
function useAddressInputContext(part) {
|
|
8
|
+
const context = use(AddressInputContext);
|
|
9
|
+
if (!context)
|
|
10
|
+
throw new Error(`<AddressInput.${part}> must be inside <AddressInput>`);
|
|
11
|
+
return context;
|
|
12
|
+
}
|
|
13
|
+
function isListShown(state) {
|
|
14
|
+
return state.open && state.status !== "idle";
|
|
15
|
+
}
|
|
16
|
+
function AddressInputRoot({ children, name, required, disabled, readOnly, autoHighlight, loop, ...options }) {
|
|
17
|
+
const { store, state, inputRef } = useAddressAutosuggest(options);
|
|
18
|
+
const byKey = useMemo(() => new Map(state.suggestions.map((suggestion) => [suggestionKey(suggestion), suggestion])), [state.suggestions]);
|
|
19
|
+
const [control, setControl] = useState(null);
|
|
20
|
+
const context = useMemo(() => ({ store, state, inputRef, control, setControl }), [store, state, inputRef, control]);
|
|
21
|
+
const highlightedRef = useRef(null);
|
|
22
|
+
const pickHandledRef = useRef(false);
|
|
23
|
+
const pick = (value) => {
|
|
24
|
+
const suggestion = value ? byKey.get(value) : undefined;
|
|
25
|
+
if (suggestion)
|
|
26
|
+
void store.select(suggestion);
|
|
27
|
+
};
|
|
28
|
+
return (_jsx(AddressInputContext, { value: context, children: _jsx(Autocomplete, { value: state.value, open: isListShown(state), filter: false, status: state.status === "success" ? "idle" : state.status, name: name, required: required, disabled: disabled, readOnly: readOnly, autoHighlight: autoHighlight, loop: loop, onHighlight: (value) => {
|
|
29
|
+
highlightedRef.current = value;
|
|
30
|
+
}, onValueChange: (next, details) => {
|
|
31
|
+
if (details.reason === "item-press" && details.item) {
|
|
32
|
+
// The store decides the text and caret after a pick.
|
|
33
|
+
details.cancel();
|
|
34
|
+
pickHandledRef.current = true;
|
|
35
|
+
pick(details.item.value);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
if (details.reason === "clear-press") {
|
|
39
|
+
store.clear();
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (details.reason === "escape-key") {
|
|
43
|
+
// ui-sdk puts back the text last typed, which is stale once a pick rewrote it.
|
|
44
|
+
// Escape only closes the list here.
|
|
45
|
+
details.cancel();
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
store.setInput(next);
|
|
49
|
+
}, onOpenChange: (next, details) => {
|
|
50
|
+
if (details.reason === "item-press") {
|
|
51
|
+
// When a pick's label equals the current text, ui-sdk skips the text change and
|
|
52
|
+
// the pick only shows up here, so fall back to the highlighted suggestion.
|
|
53
|
+
const highlighted = highlightedRef.current;
|
|
54
|
+
queueMicrotask(() => {
|
|
55
|
+
if (!pickHandledRef.current)
|
|
56
|
+
pick(highlighted);
|
|
57
|
+
pickHandledRef.current = false;
|
|
58
|
+
});
|
|
59
|
+
// The store decides: it keeps the list open to refine, or closes it when done.
|
|
60
|
+
details.cancel();
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
store.setOpen(next);
|
|
64
|
+
}, children: children }) }));
|
|
65
|
+
}
|
|
66
|
+
/** Names the input. Renders a `label`. */
|
|
67
|
+
function Label(props) {
|
|
68
|
+
useAddressInputContext("Label");
|
|
69
|
+
return _jsx(AutocompleteLabel, { ...props });
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Optional box around the input and clear button, for styling them as one field. The popup
|
|
73
|
+
* lines up with it, and `--anchor-width` is its width.
|
|
74
|
+
* Has `data-open` while suggestions show and `data-loading` while searching.
|
|
75
|
+
*/
|
|
76
|
+
function Control({ ref, ...props }) {
|
|
77
|
+
const { state, setControl } = useAddressInputContext("Control");
|
|
78
|
+
const setRef = useCallback((node) => {
|
|
79
|
+
setControl(node);
|
|
80
|
+
if (typeof ref === "function") {
|
|
81
|
+
const cleanup = ref(node);
|
|
82
|
+
// React calls a returned cleanup instead of calling the ref with null.
|
|
83
|
+
if (typeof cleanup === "function") {
|
|
84
|
+
return () => {
|
|
85
|
+
setControl(null);
|
|
86
|
+
cleanup();
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
else if (ref) {
|
|
91
|
+
ref.current = node;
|
|
92
|
+
}
|
|
93
|
+
return undefined;
|
|
94
|
+
}, [ref, setControl]);
|
|
95
|
+
return (_jsx("div", { "data-open": isListShown(state) ? "" : undefined, "data-loading": state.status === "loading" ? "" : undefined, ...props, ref: setRef }));
|
|
96
|
+
}
|
|
97
|
+
/** The text field. Renders an `input` with the combobox role. */
|
|
98
|
+
function Input({ ref, ...props }) {
|
|
99
|
+
const { inputRef } = useAddressInputContext("Input");
|
|
100
|
+
const setRef = useCallback((node) => {
|
|
101
|
+
inputRef.current = node;
|
|
102
|
+
if (typeof ref === "function")
|
|
103
|
+
return ref(node);
|
|
104
|
+
if (ref)
|
|
105
|
+
ref.current = node;
|
|
106
|
+
return undefined;
|
|
107
|
+
}, [ref, inputRef]);
|
|
108
|
+
return _jsx(AutocompleteInput, { ...props, ref: setRef });
|
|
109
|
+
}
|
|
110
|
+
/** Empties the field and keeps focus in it. Renders a `button`. */
|
|
111
|
+
function Clear(props) {
|
|
112
|
+
useAddressInputContext("Clear");
|
|
113
|
+
return _jsx(AutocompleteClear, { ...props });
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* The suggestions popup, positioned against `Control` when there is one, otherwise against
|
|
117
|
+
* the input. Use `side`, `align` and the offsets to place it, or `anchor` to line it up with
|
|
118
|
+
* another element; `--anchor-width` and `--available-height` help size it.
|
|
119
|
+
*/
|
|
120
|
+
function Popup({ anchor, ...props }) {
|
|
121
|
+
const { control } = useAddressInputContext("Popup");
|
|
122
|
+
return (_jsx(AutocompleteContent, { ...props, anchor: anchor !== undefined ? anchor : (control ?? undefined) }));
|
|
123
|
+
}
|
|
124
|
+
/** Announces loading and result counts. Children can be a function of `{ loading, count }`. */
|
|
125
|
+
function Status(props) {
|
|
126
|
+
useAddressInputContext("Status");
|
|
127
|
+
return _jsx(AutocompleteStatus, { ...props });
|
|
128
|
+
}
|
|
129
|
+
/** Shows when a search finds nothing. Children can be a function of `{ empty, loading, query }`. */
|
|
130
|
+
function Empty(props) {
|
|
131
|
+
useAddressInputContext("Empty");
|
|
132
|
+
return _jsx(AutocompleteEmpty, { ...props });
|
|
133
|
+
}
|
|
134
|
+
/** The listbox holding the suggestions. */
|
|
135
|
+
function List({ children, ...props }) {
|
|
136
|
+
const { state } = useAddressInputContext("List");
|
|
137
|
+
const content = typeof children === "function"
|
|
138
|
+
? state.suggestions.map((suggestion, index) => (_jsx(Fragment, { children: children(suggestion, index) }, suggestionKey(suggestion))))
|
|
139
|
+
: (children ??
|
|
140
|
+
state.suggestions.map((suggestion) => (_jsx(Item, { suggestion: suggestion }, suggestionKey(suggestion)))));
|
|
141
|
+
return _jsx(AutocompleteList, { ...props, children: content });
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* One suggestion. Shows its label unless you pass children. Has `data-type` with the
|
|
145
|
+
* suggestion's type and `data-highlighted` while highlighted.
|
|
146
|
+
*/
|
|
147
|
+
function Item({ suggestion, children, ...props }) {
|
|
148
|
+
useAddressInputContext("Item");
|
|
149
|
+
return (_jsx(AutocompleteItem, { "data-type": suggestion.type, ...props, value: suggestionKey(suggestion), label: suggestion.label, children: children ?? suggestion.label }));
|
|
150
|
+
}
|
|
151
|
+
/** Read the state and act on it from anywhere inside `<AddressInput>`. */
|
|
152
|
+
export function useAddressInput() {
|
|
153
|
+
const { store, state } = useAddressInputContext("useAddressInput");
|
|
154
|
+
return { state, select: store.select, selectById: store.selectById, clear: store.clear };
|
|
155
|
+
}
|
|
156
|
+
AddressInputRoot.displayName = "AddressInput";
|
|
157
|
+
Label.displayName = "AddressInput.Label";
|
|
158
|
+
Control.displayName = "AddressInput.Control";
|
|
159
|
+
Input.displayName = "AddressInput.Input";
|
|
160
|
+
Clear.displayName = "AddressInput.Clear";
|
|
161
|
+
Popup.displayName = "AddressInput.Popup";
|
|
162
|
+
Status.displayName = "AddressInput.Status";
|
|
163
|
+
Empty.displayName = "AddressInput.Empty";
|
|
164
|
+
List.displayName = "AddressInput.List";
|
|
165
|
+
Item.displayName = "AddressInput.Item";
|
|
166
|
+
/**
|
|
167
|
+
* Headless address autosuggest for KDS Adressevælger. Every part takes `className` and
|
|
168
|
+
* `style` (plain, or a function of the part's state) and its element's props.
|
|
169
|
+
*
|
|
170
|
+
* ```tsx
|
|
171
|
+
* <AddressInput onSelect={(selection) => save(selection.details)}>
|
|
172
|
+
* <AddressInput.Label>Address</AddressInput.Label>
|
|
173
|
+
* <AddressInput.Control>
|
|
174
|
+
* <AddressInput.Input />
|
|
175
|
+
* <AddressInput.Clear>×</AddressInput.Clear>
|
|
176
|
+
* </AddressInput.Control>
|
|
177
|
+
* <AddressInput.Popup>
|
|
178
|
+
* <AddressInput.Empty>No addresses found.</AddressInput.Empty>
|
|
179
|
+
* <AddressInput.List />
|
|
180
|
+
* </AddressInput.Popup>
|
|
181
|
+
* </AddressInput>
|
|
182
|
+
* ```
|
|
183
|
+
*/
|
|
184
|
+
export const AddressInput = Object.assign(AddressInputRoot, {
|
|
185
|
+
Label,
|
|
186
|
+
Control,
|
|
187
|
+
Input,
|
|
188
|
+
Clear,
|
|
189
|
+
Popup,
|
|
190
|
+
Status,
|
|
191
|
+
Empty,
|
|
192
|
+
List,
|
|
193
|
+
Item,
|
|
194
|
+
});
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { AddressInput, useAddressInput, type AddressInputClearProps, type AddressInputControlProps, type AddressInputEmptyProps, type AddressInputInputProps, type AddressInputItemProps, type AddressInputLabelProps, type AddressInputListProps, type AddressInputPopupProps, type AddressInputProps, type AddressInputStatusProps, type UseAddressInputResult, } from "./address-input.tsx";
|
|
2
|
+
export { useAddressAutosuggest, type UseAddressAutosuggestResult, } from "./use-address-autosuggest.ts";
|
|
3
|
+
export { suggestionKey } from "@dk-address/client";
|
|
4
|
+
export type { AccessAddress, Address, SearchKind, Suggestion, SuggestionType, } from "@dk-address/client";
|
|
5
|
+
export type { AddressAutosuggest, AutosuggestOptions, AutosuggestState, Selection, } from "@dk-address/core";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type RefObject } from "react";
|
|
2
|
+
import type { AddressAutosuggest, AutosuggestOptions, AutosuggestState } from "@dk-address/core";
|
|
3
|
+
export interface UseAddressAutosuggestResult {
|
|
4
|
+
state: AutosuggestState;
|
|
5
|
+
/** Call `setInput` on typing, `select` on a pick, `setOpen` when your popup opens or closes. */
|
|
6
|
+
store: AddressAutosuggest;
|
|
7
|
+
/**
|
|
8
|
+
* Point this at the DOM input your component library renders, so the caret moves into
|
|
9
|
+
* the gap after a pick (e.g. `ref={(antd) => { inputRef.current = antd?.input ?? null }}`).
|
|
10
|
+
*/
|
|
11
|
+
inputRef: RefObject<HTMLInputElement | null>;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* The address autosuggest without any UI, for wiring into a component library's own
|
|
15
|
+
* autocomplete (antd, MUI, Mantine…). It adds no ARIA or keyboard handling; your
|
|
16
|
+
* component provides those.
|
|
17
|
+
*/
|
|
18
|
+
export declare function useAddressAutosuggest(options?: AutosuggestOptions): UseAddressAutosuggestResult;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { useRef } from "react";
|
|
2
|
+
import { useAutosuggestStore, useCaretPlacement } from "./use-autosuggest-store.js";
|
|
3
|
+
/**
|
|
4
|
+
* The address autosuggest without any UI, for wiring into a component library's own
|
|
5
|
+
* autocomplete (antd, MUI, Mantine…). It adds no ARIA or keyboard handling; your
|
|
6
|
+
* component provides those.
|
|
7
|
+
*/
|
|
8
|
+
export function useAddressAutosuggest(options = {}) {
|
|
9
|
+
const { store, state } = useAutosuggestStore(options);
|
|
10
|
+
const inputRef = useRef(null);
|
|
11
|
+
useCaretPlacement(inputRef, state.caret, state.value);
|
|
12
|
+
return { state, store, inputRef };
|
|
13
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type RefObject } from "react";
|
|
2
|
+
import { type AddressAutosuggest, type AutosuggestOptions, type AutosuggestState, type Caret } from "@dk-address/core";
|
|
3
|
+
/** Create one store for the component's lifetime, keep its options in sync and subscribe to it. */
|
|
4
|
+
export declare function useAutosuggestStore(options: AutosuggestOptions): {
|
|
5
|
+
store: AddressAutosuggest;
|
|
6
|
+
state: AutosuggestState;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Move the caret where the store asks after a pick. Only in a focused input, so prefilling
|
|
10
|
+
* with `selectById` never steals focus.
|
|
11
|
+
*/
|
|
12
|
+
export declare function useCaretPlacement(inputRef: RefObject<HTMLInputElement | null>, caret: Caret | null, value: string): void;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { useEffect, useLayoutEffect, useState, useSyncExternalStore } from "react";
|
|
2
|
+
import { createAddressAutosuggest, } from "@dk-address/core";
|
|
3
|
+
/** Create one store for the component's lifetime, keep its options in sync and subscribe to it. */
|
|
4
|
+
export function useAutosuggestStore(options) {
|
|
5
|
+
const { onSelect, client, kind, debounceMs, limit, municipalityCodes, includeProvisional, houseNumberPrefix, fetchDetails, } = options;
|
|
6
|
+
const [store] = useState(() => createAddressAutosuggest(options));
|
|
7
|
+
useLayoutEffect(() => {
|
|
8
|
+
store.setOptions({ onSelect });
|
|
9
|
+
}, [store, onSelect]);
|
|
10
|
+
const municipalityKey = municipalityCodes?.join(",") ?? "";
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
store.setOptions({
|
|
13
|
+
client,
|
|
14
|
+
kind,
|
|
15
|
+
debounceMs,
|
|
16
|
+
limit,
|
|
17
|
+
municipalityCodes: municipalityKey ? municipalityKey.split(",") : undefined,
|
|
18
|
+
includeProvisional,
|
|
19
|
+
houseNumberPrefix,
|
|
20
|
+
fetchDetails,
|
|
21
|
+
});
|
|
22
|
+
}, [
|
|
23
|
+
store,
|
|
24
|
+
client,
|
|
25
|
+
kind,
|
|
26
|
+
debounceMs,
|
|
27
|
+
limit,
|
|
28
|
+
municipalityKey,
|
|
29
|
+
includeProvisional,
|
|
30
|
+
houseNumberPrefix,
|
|
31
|
+
fetchDetails,
|
|
32
|
+
]);
|
|
33
|
+
useEffect(() => () => store.destroy(), [store]);
|
|
34
|
+
const state = useSyncExternalStore(store.subscribe, store.getState, store.getState);
|
|
35
|
+
return { store, state };
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Move the caret where the store asks after a pick. Only in a focused input, so prefilling
|
|
39
|
+
* with `selectById` never steals focus.
|
|
40
|
+
*/
|
|
41
|
+
export function useCaretPlacement(inputRef, caret, value) {
|
|
42
|
+
useLayoutEffect(() => {
|
|
43
|
+
const input = inputRef.current;
|
|
44
|
+
if (!input || !caret)
|
|
45
|
+
return;
|
|
46
|
+
const place = () => {
|
|
47
|
+
if (input.ownerDocument.activeElement !== input || input.value !== value)
|
|
48
|
+
return;
|
|
49
|
+
input.setSelectionRange(caret.position, caret.position);
|
|
50
|
+
};
|
|
51
|
+
place();
|
|
52
|
+
// Input wrappers that write the DOM value in a later effect move the caret to the end again.
|
|
53
|
+
const frame = requestAnimationFrame(place);
|
|
54
|
+
return () => cancelAnimationFrame(frame);
|
|
55
|
+
}, [inputRef, caret, value]);
|
|
56
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,72 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dk-address/react",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Unstyled React address autocomplete for Danish addresses, built on KDS Adressevælger.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"address",
|
|
7
|
+
"adressevaelger",
|
|
8
|
+
"autocomplete",
|
|
9
|
+
"danish",
|
|
10
|
+
"dar",
|
|
11
|
+
"dawa",
|
|
12
|
+
"denmark",
|
|
13
|
+
"kds",
|
|
14
|
+
"react"
|
|
15
|
+
],
|
|
16
|
+
"homepage": "https://dk-address.dyrhoi.com/docs",
|
|
17
|
+
"bugs": "https://github.com/Dyrhoi/dk-address/issues",
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/Dyrhoi/dk-address.git",
|
|
22
|
+
"directory": "packages/react"
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
27
|
+
"type": "module",
|
|
28
|
+
"sideEffects": false,
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"default": "./dist/index.js"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public"
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
40
|
+
"lint": "bun lint --deny-warnings",
|
|
41
|
+
"typecheck": "bun check",
|
|
42
|
+
"test": "vitest run"
|
|
43
|
+
},
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"@dk-address/client": "0.1.0",
|
|
46
|
+
"@dk-address/core": "0.1.0",
|
|
47
|
+
"@ui-sdk/core": "0.1.0-beta.17",
|
|
48
|
+
"@ui-sdk/react": "0.1.0-beta.17"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@repo/kds-fixtures": "0.0.0",
|
|
52
|
+
"@repo/oxlint-config": "0.0.0",
|
|
53
|
+
"@repo/typescript-config": "0.0.0",
|
|
54
|
+
"@repo/vitest-config": "0.0.0",
|
|
55
|
+
"@types/bun": "1.4.5",
|
|
56
|
+
"@types/react": "19.3.1",
|
|
57
|
+
"@types/react-dom": "19.3.0",
|
|
58
|
+
"@vitest/browser-playwright": "5.1.1",
|
|
59
|
+
"playwright": "1.65.1",
|
|
60
|
+
"react": "19.3.1",
|
|
61
|
+
"react-dom": "19.3.1",
|
|
62
|
+
"vite": "8.3.8",
|
|
63
|
+
"vitest": "5.1.1",
|
|
64
|
+
"vitest-browser-react": "2.3.0"
|
|
65
|
+
},
|
|
66
|
+
"peerDependencies": {
|
|
67
|
+
"react": ">=19",
|
|
68
|
+
"react-dom": ">=19"
|
|
69
|
+
},
|
|
70
|
+
"main": "./dist/index.js",
|
|
71
|
+
"types": "./dist/index.d.ts"
|
|
72
|
+
}
|