@pro6pp/infer-react 0.0.2-beta.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/README.md +63 -0
- package/dist/index.d.mts +24 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +50 -0
- package/dist/index.mjs +25 -0
- package/package.json +40 -0
package/README.md
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# @pro6pp/infer-react
|
|
2
|
+
|
|
3
|
+
React Hook for the [Pro6PP Infer API](https://www.pro6pp.com/developer/infer/nl/parameters).
|
|
4
|
+
A headless library to build custom address autocomplete components in React.
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
npm install @pro6pp/infer-react
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
This library handles all the logic (state, API calls, debouncing) but doesn't render anything. You are responsible for rendering the input and the dropdown list using your own styling.
|
|
15
|
+
|
|
16
|
+
```tsx
|
|
17
|
+
import React from 'react';
|
|
18
|
+
import { useInfer } from '@pro6pp/infer-react';
|
|
19
|
+
|
|
20
|
+
const AddressForm = () => {
|
|
21
|
+
const { state, inputProps, selectItem } = useInfer({
|
|
22
|
+
authKey: 'YOUR_AUTH_KEY',
|
|
23
|
+
country: 'NL',
|
|
24
|
+
limit: 5,
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
return (
|
|
28
|
+
<div className="address-autocomplete">
|
|
29
|
+
<label>Address</label>
|
|
30
|
+
|
|
31
|
+
<input {...inputProps} placeholder="Type a Dutch address..." className="my-input-class" />
|
|
32
|
+
|
|
33
|
+
{/* render the dropdown */}
|
|
34
|
+
{(state.suggestions.length > 0 || state.cities.length > 0) && (
|
|
35
|
+
<ul className="my-dropdown-class">
|
|
36
|
+
{/* render cities, if any */}
|
|
37
|
+
{state.cities.map((city, i) => (
|
|
38
|
+
<li key={`city-${i}`} onClick={() => selectItem(city)}>
|
|
39
|
+
<strong>{city.label}</strong> (City)
|
|
40
|
+
</li>
|
|
41
|
+
))}
|
|
42
|
+
|
|
43
|
+
{/* render streets, if any */}
|
|
44
|
+
{state.streets.map((street, i) => (
|
|
45
|
+
<li key={`street-${i}`} onClick={() => selectItem(street)}>
|
|
46
|
+
<strong>{street.label}</strong> (Street)
|
|
47
|
+
</li>
|
|
48
|
+
))}
|
|
49
|
+
|
|
50
|
+
{/* render general suggestions */}
|
|
51
|
+
{state.suggestions.map((item, i) => (
|
|
52
|
+
<li key={`sugg-${i}`} onClick={() => selectItem(item)}>
|
|
53
|
+
{item.label}
|
|
54
|
+
</li>
|
|
55
|
+
))}
|
|
56
|
+
</ul>
|
|
57
|
+
)}
|
|
58
|
+
|
|
59
|
+
{state.isValid && <p>Valid address selected</p>}
|
|
60
|
+
</div>
|
|
61
|
+
);
|
|
62
|
+
};
|
|
63
|
+
```
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { InferConfig, InferState, InferCore, InferResult } from '@pro6pp/infer-core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Hook for the Pro6PP Infer API.
|
|
5
|
+
* @param config - The API configuration.
|
|
6
|
+
* @returns An object containing the current state, input helpers, and selection handler.
|
|
7
|
+
* @example
|
|
8
|
+
* const { state, inputProps, selectItem } = useInfer({
|
|
9
|
+
* authKey: 'YOUR_KEY',
|
|
10
|
+
* country: 'NL'
|
|
11
|
+
* });
|
|
12
|
+
*/
|
|
13
|
+
declare function useInfer(config: InferConfig): {
|
|
14
|
+
state: InferState;
|
|
15
|
+
core: InferCore;
|
|
16
|
+
inputProps: {
|
|
17
|
+
value: string;
|
|
18
|
+
onChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
|
|
19
|
+
onKeyDown: (e: React.KeyboardEvent<HTMLInputElement>) => void;
|
|
20
|
+
};
|
|
21
|
+
selectItem: (item: InferResult | string) => void;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export { useInfer };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { InferConfig, InferState, InferCore, InferResult } from '@pro6pp/infer-core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Hook for the Pro6PP Infer API.
|
|
5
|
+
* @param config - The API configuration.
|
|
6
|
+
* @returns An object containing the current state, input helpers, and selection handler.
|
|
7
|
+
* @example
|
|
8
|
+
* const { state, inputProps, selectItem } = useInfer({
|
|
9
|
+
* authKey: 'YOUR_KEY',
|
|
10
|
+
* country: 'NL'
|
|
11
|
+
* });
|
|
12
|
+
*/
|
|
13
|
+
declare function useInfer(config: InferConfig): {
|
|
14
|
+
state: InferState;
|
|
15
|
+
core: InferCore;
|
|
16
|
+
inputProps: {
|
|
17
|
+
value: string;
|
|
18
|
+
onChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
|
|
19
|
+
onKeyDown: (e: React.KeyboardEvent<HTMLInputElement>) => void;
|
|
20
|
+
};
|
|
21
|
+
selectItem: (item: InferResult | string) => void;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export { useInfer };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
useInfer: () => useInfer
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
var import_react = require("react");
|
|
27
|
+
var import_infer_core = require("@pro6pp/infer-core");
|
|
28
|
+
function useInfer(config) {
|
|
29
|
+
const [state, setState] = (0, import_react.useState)(import_infer_core.INITIAL_STATE);
|
|
30
|
+
const core = (0, import_react.useMemo)(() => {
|
|
31
|
+
return new import_infer_core.InferCore({
|
|
32
|
+
...config,
|
|
33
|
+
onStateChange: (newState) => setState({ ...newState })
|
|
34
|
+
});
|
|
35
|
+
}, [config.country, config.authKey, config.limit]);
|
|
36
|
+
return {
|
|
37
|
+
state,
|
|
38
|
+
core,
|
|
39
|
+
inputProps: {
|
|
40
|
+
value: state.query,
|
|
41
|
+
onChange: (e) => core.handleInput(e.target.value),
|
|
42
|
+
onKeyDown: (e) => core.handleKeyDown(e)
|
|
43
|
+
},
|
|
44
|
+
selectItem: (item) => core.selectItem(item)
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
48
|
+
0 && (module.exports = {
|
|
49
|
+
useInfer
|
|
50
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { useState, useMemo } from "react";
|
|
3
|
+
import { InferCore, INITIAL_STATE } from "@pro6pp/infer-core";
|
|
4
|
+
function useInfer(config) {
|
|
5
|
+
const [state, setState] = useState(INITIAL_STATE);
|
|
6
|
+
const core = useMemo(() => {
|
|
7
|
+
return new InferCore({
|
|
8
|
+
...config,
|
|
9
|
+
onStateChange: (newState) => setState({ ...newState })
|
|
10
|
+
});
|
|
11
|
+
}, [config.country, config.authKey, config.limit]);
|
|
12
|
+
return {
|
|
13
|
+
state,
|
|
14
|
+
core,
|
|
15
|
+
inputProps: {
|
|
16
|
+
value: state.query,
|
|
17
|
+
onChange: (e) => core.handleInput(e.target.value),
|
|
18
|
+
onKeyDown: (e) => core.handleKeyDown(e)
|
|
19
|
+
},
|
|
20
|
+
selectItem: (item) => core.selectItem(item)
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export {
|
|
24
|
+
useInfer
|
|
25
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pro6pp/infer-react",
|
|
3
|
+
"version": "0.0.2-beta.0",
|
|
4
|
+
"main": "./dist/index.js",
|
|
5
|
+
"module": "./dist/index.mjs",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.mjs",
|
|
11
|
+
"require": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"README.md"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "tsup src/index.ts --format cjs,esm --dts --external react",
|
|
20
|
+
"dev": "tsup src/index.ts --format cjs,esm --dts --watch",
|
|
21
|
+
"type-check": "tsc --noEmit"
|
|
22
|
+
},
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"react": ">=16"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"@pro6pp/infer-core": "0.0.2-beta.0"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@types/react": "^19.2.7"
|
|
31
|
+
},
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public"
|
|
34
|
+
},
|
|
35
|
+
"license": "MIT",
|
|
36
|
+
"repository": {
|
|
37
|
+
"type": "git",
|
|
38
|
+
"url": "https://gitlab.d-centralize.nl/dc/pro6pp/pro6pp-infer-sdk"
|
|
39
|
+
}
|
|
40
|
+
}
|