2.1.5 • Published 2 years ago

@ancaio/vue-dawa v2.1.5

Weekly downloads
366
License
MIT
Repository
github
Last release
2 years ago

vue-dawa Build Status

:warning: Package no longer maintained! I no longer have the time to properly maintain this package. Feel free to copy, modify, and distribute.

This component allows you to create an address-autocomplete using Danmarks Adressers WEB API in Vue 2 applications.

It relies heavily on https://github.com/DanmarksAdresser/dawa-autocomplete2, but uses the axios library instead of fetch for making requests and it is fully "vue-ified".

Demo

Installation

If you don't already use axios, install axios together with vue-dawa:

npm install --save axios @ancaio/vue-dawa

Or just:

npm install --save @ancaio/vue-dawa

Note on IE Compatibility

In lib mode, the public path is dynamically determined based on the URL from which the main js file is loaded (to enable dynamic assets loading). However, this feature requires document.currentScript support, which is missing in IE. So it's recommended to include the current-script-polyfill in the final web page before the library is imported, if IE support is a requirement.

Note on Vue Dependency

In lib mode, Vue is externalized. This means the bundle will not bundle Vue even if your code imports Vue. If the lib is used via a bundler, it will attempt to load Vue as a dependency through the bundler; otherwise, it falls back to a global Vue variable.

Usage

Make sure to import the component:

import VueDawa from '@ancaio/vue-dawa/sfc'

Add it to your components' components:

export default {
  components: {
    VueDawa
  }
}

In your template:

<vue-dawa @select="selectItem($event, 'yourAddressObject')"
  :val="yourAddressObject.oneLineAddress"
  :showMax="5"
  placeholder="Your Placeholder"
  containerId="your-container"
  fieldId="your-field-id"
  fieldName="your-field-name">
  <label slot="label-top" for="field-2">Your label</label>
</vue-dawa>

In your component's data, you will need to provide yourAddressObject.

  data () {
    yourAddressObject: {
      oneLineAddress: ''
    }
  }

You will also need to define a method for modifying the selected address to your needs - if that is desired.

// here the event payload is of the type "adresse"
translateData (address) {
  if (!address || !address.data) {
    return
  }
  return {
    id: address.data.id,
    street: address.data.vejnavn,
    streetNumber: address.data.husnr,
    floor: address.data.etage,
    door: address.data.dør,
    zipCode: address.data.postnr,
    city: address.data.postnrnavn,
    oneLineAddress: address.oneLineAddress // formatted one-line address, OR address.forslagstekst OR address.tekst
  }
}

If you will create multiple fields in the same component, you might want to handle the select event in a manner that will allow you to update whichever address object without writing custom handlers for each. A quick example:

  // handle the select event emitted by vue-dawa
  selectItem (payload, objectName) {
   this.$set(this, objectName, this.translateData(payload))
  }

Events

EventValue
selectObject - the raw DAWA address object
listHeightUpdateddouble - current height of results ul
inputChangedString - search terms changed

Props

PropRequiredTypePurpose
placeholdernoStringDisplays a placeholder in the input field
containerIdyesStringProvides the id attribute of the component wrapper and is used in computing a unique id for the list of results used in height detection
containerClassesnoString or ObjectCustom styling
disablednoBooleanDisable input. Remember to add 'disabled' in your fieldClasses prop for the default disabled styling, or use your own.
fieldIdyesStringWhen combined with a label, improves accessibility
fieldClassesnoString or ObjectCustom styling
fieldNamenoStringCan be useful in form validation
listClassesnoString or ObjectCustom styling
listItemClassesnoString or ObjectCustom styling. Active class is handled by the component already, but feel free to play.
valnoStringPre-populates the input field with an initial value
addressIdnoStringA DAWA address id. If provided, it will perform an automatic search on created and populate the field with the found address. It will also emit the selected event.
optionsnoObject, see default options belowConfiguring the DAWA service. NOTE: The component was only intended for full addresses, but it should work with other types. See DAWA's docs for more info
showMaxno, but you might want it setNumberLimits the number of visible results
resultsListStylenoObjectProvide style for results list
listScrollBehaviornoObjectDefine the scroll behavior for the results list, to be used by Element.scrollIntoView()

Default options prop

{
  params: {},
  minLength: 2, // would leave this alone if I were you, it can lead to wonky behavior.
  retryDelay: 500,
  type: 'adresse',
  baseUrl: 'https://dawa.aws.dk',
  adgangsadresserOnly: false,
  stormodtagerpostnumre: true,
  supplerendebynavn: true,
  fuzzy: true,
}

Overflow:hidden nuissance

If vue-dawa is placed in a container (or descendant of a container) with overflow: hidden, the list of results will be cut off. There is no easy or nice fix for this, unfortunately.

What you CAN do, is leverage the listHeightUpdated event, which will pass to you the height of the list of results.

You can use this height to compute a min-height property on the container in question or child of it, and give it a transition on the height for a smoother change. It's not flawless, but it will allow you to use the component in a container with overflow: hidden.

Styling

You can import the default style:

import '@ancaio/vue-dawa/dist/vue-dawa.css'

or customize the css yourself. Remember you can add custom classes to all the elements of the componets.

For the results list, you can also provide styling options via the resultsListStyle prop. For instance, if you want to control the scroll of the list, this property's value could be:

<vue-dawa
  ...
  other
  props
  :resultsListStyle="{
    maxHeight: '200px',
    overflowY: 'auto',
    overFlowX: 'hidden'
  }"
/>

Here's a starting point for tweaking the default css.

.autocomplete-container {
  /* relative position for at de absolut positionerede forslag får korrekt placering.*/
  position: relative;
  width: 100%;
  max-width: 30em;
}

.autocomplete-container input {
  /* Både input og forslag får samme bredde som omkringliggende DIV */
  width: 100%;
  box-sizing: border-box;
  margin-bottom: 5px;
}

.dawa-autocomplete-suggestions {
  position: absolute;
  z-index: 9999;
  width: 100%;
  margin: 0.15em 0 0 0;
  padding: 0;
  text-align: left;
  border-radius: 0.3125em;
  background: #fcfcfc;
  box-shadow: 0 0.0625em 0.15625em rgba(0, 0, 0, 0.15);
  box-sizing: border-box;
}

.dawa-autocomplete-suggestions .dawa-autocomplete-suggestion {
  margin: 0;
  list-style: none;
  cursor: pointer;
  padding: 0.4em 0.6em;
  color: #333;
  border: 0.0625em solid #ddd;
  border-bottom-width: 0;
}

.dawa-autocomplete-suggestions .dawa-autocomplete-suggestion:first-child {
  border-top-left-radius: inherit;
  border-top-right-radius: inherit;
}

.dawa-autocomplete-suggestions .dawa-autocomplete-suggestion:last-child {
  border-bottom-left-radius: inherit;
  border-bottom-right-radius: inherit;
  border-bottom-width: 0.0625em;
}

.dawa-autocomplete-suggestions .dawa-autocomplete-suggestion.active,
.dawa-autocomplete-suggestions .dawa-autocomplete-suggestion:hover {
  background: #f0f0f0;
  color: #333;
}
2.1.4

2 years ago

2.1.5

2 years ago

2.1.2

3 years ago

2.1.1

3 years ago

2.1.0

3 years ago

2.0.11

4 years ago

2.0.10

4 years ago

2.0.8

5 years ago

2.0.7

5 years ago

2.0.3

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago