项目作者: geocode-city

项目描述 :
JS Autocomplete snippet
高级语言: PureScript
项目地址: git://github.com/geocode-city/autocomplete.git
创建时间: 2021-01-28T04:45:45Z
项目社区:https://github.com/geocode-city/autocomplete

开源协议:Apache License 2.0

下载


geocode.city Autocomplete

Simple JS snippet to add a geocode.city-backed text input to an existing page.

Usage

I’m bad at Javascript (and worse at Purescript) so this is an old-school
style of snippet: include it on your page, make sure you have an element
with id geocode-city-autocomplete, and we’re done. If you have an api key,
have that element provide it via a data-api-key attribute.

For example:

  1. <body>
  2. <div id="geocode-city-autocomplete" data-api-key="my-api-key" data-suggestion-count="10">
  3. </div>
  4. <script src="https://cdn.jsdelivr.net/gh/geocode-city/autocomplete@0.1.0.1/dist/geocode_city.min.js"></script>
  5. </body>

The element would ideally be empty: the script replaces it with its
own input. All markup generated by the script has appropriately prefixed
CSS classes, so you can style it to match your own app.

Data attributes

  • data-api-key: your api key.
  • data-suggestion-count: how many suggestions to display. Defaults to 5, max is 100 (enforced by the server.)
  • data-input-name and data-input-class: control the name and input attributes of the
    textual input.

Events

You can listen to any of these events and find their data in the event.detail property.

  • citySelected: receives the city object that was selected by the user.
  • citiesFound: receives an array of city objects when a response has been received. Will not be raised if no results are found, or if there’s errors.
  • noCitiesFound: raised when the request succeeded, but no cities matched the query.
  • lookupError: raised when the response fails, due to a client or server errors. The error
    message as parsed by the request handler is included in event.detail.message.

Example of a city object:

  1. {
  2. "timezone": "Europe/Paris",
  3. "region": "Île-de-France",
  4. "population": 2138551,
  5. "name": "Paris",
  6. "longitude": 2.3488,
  7. "latitude": 48.85341,
  8. "district": "Paris",
  9. "countryCode": "FR",
  10. "country": "France"
  11. }

Markup

The component is unstyled, but CSS classes are used for each element; cf. the render function
in each of Autocomplete.Component and Autocomplete.Dropdown for the most up to date classes.

Bundling

I distribute this project through jsdelivr. See
NOTES for development commands, but for production, I just run:

  1. npm run bundle

And the dist/geocode_city.js file should be updated. Minification is offloaded
to jsdelivr for the time being.