AutoComplete.vue 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <template>
  2. <span>
  3. <slot>
  4. <input>
  5. </slot>
  6. </span>
  7. </template>
  8. <script>
  9. import commonMixin from '../base/mixins/common.js'
  10. import bindEvents from '../base/bindEvent.js'
  11. export default {
  12. name: 'bm-autocomplete',
  13. mixins: [commonMixin('autoComplete')],
  14. emits: ['update:modelValue', 'searchcomplete'],
  15. props: {
  16. types: {
  17. type: String
  18. },
  19. location: {
  20. type: String
  21. },
  22. sugStyle: {
  23. type: Object,
  24. default() {
  25. return {}
  26. }
  27. },
  28. modelValue: {}
  29. },
  30. watch: {
  31. types() {
  32. this.reload()
  33. },
  34. location() {
  35. this.reload()
  36. }
  37. },
  38. methods: {
  39. load() {
  40. const { BMap, map, $el, types, location, sugStyle } = this
  41. const input = $el.querySelector('input')
  42. if (!input) {
  43. return
  44. }
  45. this.originInstance = new BMap.Autocomplete({
  46. input,
  47. types,
  48. location: location || map,
  49. onSearchComplete: e => {
  50. const $sugs = document.querySelectorAll('.tangram-suggestion-main')
  51. for (const $sug of $sugs) {
  52. for (const name in sugStyle) {
  53. $sug.style[name] = sugStyle[name].toString()
  54. }
  55. }
  56. this.$emit('searchcomplete', e)
  57. }
  58. })
  59. // Support v-model
  60. this.originInstance.addEventListener('onconfirm', e => {
  61. const val = e.item.value
  62. this.$emit('update:modelValue', val.province + val.city + val.district + val.street + val.business)
  63. })
  64. bindEvents.call(this, this.originInstance)
  65. }
  66. }
  67. }
  68. </script>