Помимо основного метода createSuggestions виджет экспортирует вспомогательные функции getBoundedValue и getLocation.

getBoundedValue

Возвращает строковое значение подсказки в указанных границах bounds для подсказок по адресам:

interface Bounds {
    from_bound: { value: string };
    to_bound: { value: string };
}

interface Options {
    bounds: Bounds;
    suggestion: Suggestion;
    type: "address";
}

Пример:

type getBoundedValue = (options: Options) => string;

import { getBoundedValue, createSuggestions } from "@dadata/suggestions";

const bounds = {
    from_bound: { value: "region" },
    to_bound: { value: "city" },
};

const suggestions = createSuggestions(input, {
    type: "address",
    formatSelected(suggestion) {
        // вернет значение подсказки от региона до города
        // в соответствии с переданными границами bounds
        return getBoundedValue(bounds, suggestion, "address");
    },
});

getLocation

Возвращает текущую геолокацию по IP:

interface API_OPTIONS {
    url?: string;
    headers?: Record<string, string> | (() => Record<string, string>);
    token?: string;
    serviceUrl?: string;
    partner?: string;
    timeout?: number;
}

type getLocation = (options?: API_OPTIONS) => Promise<Suggestion | null>;

Пример:

import { getLocation } from "@dadata/suggestions";

const token = /* */;

getLocation({ token }).then((location) => {
    if (location) console.log(location.data.city);
})


  • Нет меток