|
| 1 | +--- |
| 2 | +showTabs: true |
| 3 | +--- |
| 4 | + |
| 5 | +## Description |
| 6 | + |
| 7 | +`Connectors` are an opt-in way to extend the functionality of a form. They can be used to add features like API calls for autofill, validation, and more. |
| 8 | + |
| 9 | +Available connectors: |
| 10 | + |
| 11 | +- [Bring](/uilib/extensions/forms/Connectors/Bring/) |
| 12 | + |
| 13 | +## Import |
| 14 | + |
| 15 | +```ts |
| 16 | +import { Connectors } from '@dnb/eufemia/extensions/forms' |
| 17 | +``` |
| 18 | + |
| 19 | +## How to create your own connector |
| 20 | + |
| 21 | +Connectors are created by returning a function that takes the `generalConfig` and optionally a `handlerConfig` as an argument. |
| 22 | + |
| 23 | +Here is an example of how to create a connector that can be used as an field `onChangeValidator` or `onBlurValidator`: |
| 24 | + |
| 25 | +```ts |
| 26 | +export function validator(generalConfig: GeneralConfig) { |
| 27 | + // - The handler to be used as the validator |
| 28 | + return async function validatorHandler(value) { |
| 29 | + try { |
| 30 | + const { data, status } = await fetchData(value, { |
| 31 | + generalConfig, |
| 32 | + parameters: {}, |
| 33 | + }) |
| 34 | + |
| 35 | + const onMatch = () => { |
| 36 | + return new FormError('PostalCodeAndCity.invalidCode') |
| 37 | + } |
| 38 | + |
| 39 | + const { matcher } = responseResolver(data, handlerConfig) |
| 40 | + const match = matcher(value) |
| 41 | + |
| 42 | + if (status !== 400 && !match) { |
| 43 | + return onMatch() |
| 44 | + } |
| 45 | + } catch (error) { |
| 46 | + return error |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +Here is the `GeneralConfig` type simplified: |
| 53 | + |
| 54 | +```ts |
| 55 | +type GeneralConfig = { |
| 56 | + fetchConfig?: { |
| 57 | + url: string | ((value: string) => string | Promise<string>) |
| 58 | + headers?: HeadersInit |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
| 62 | +
|
| 63 | +The `responseResolver` is used to take care of the response from the API and return the `matcher` and `payload` to be used by the connector. |
| 64 | +
|
| 65 | +```ts |
| 66 | +const responseResolver: ResponseResolver< |
| 67 | + PostalCodeResolverData, |
| 68 | + PostalCodeResolverPayload |
| 69 | +> = (data, handlerConfig) => { |
| 70 | + // - Here we align the data from the API with the expected data structure |
| 71 | + const { postal_code, city } = data?.postal_codes?.[0] || {} |
| 72 | + |
| 73 | + return { |
| 74 | + /** |
| 75 | + * The matcher to be used to determine if the connector, |
| 76 | + * such as an validator for `onChangeValidator` or `onBlurValidator`, |
| 77 | + * should validate the field value. |
| 78 | + */ |
| 79 | + matcher: (value) => value === postal_code, |
| 80 | + |
| 81 | + /** |
| 82 | + * The payload to be returned and used by the connector. |
| 83 | + */ |
| 84 | + payload: { city }, |
| 85 | + } |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +You can extend a response resolver to support a custom resolver, given via the `handlerConfig` argument. |
| 90 | + |
| 91 | +```ts |
| 92 | +const responseResolver = (data, handlerConfig) => { |
| 93 | + const resolver = handlerConfig?.responseResolver |
| 94 | + if (typeof resolver === 'function') { |
| 95 | + return resolver(data) |
| 96 | + } |
| 97 | + |
| 98 | + // ... the rest of the response resolver. |
| 99 | +} |
| 100 | +``` |
0 commit comments