From 16ba3461ac21c567d57979e3849f09515b9d3dd7 Mon Sep 17 00:00:00 2001 From: Scott Vitale Date: Fri, 25 May 2018 15:11:00 -0600 Subject: [PATCH 1/3] Remove the defaults for both "types" and "setValueWithProperty" options Using no default for these options provides more consistent behavior with the unmodified Google Autocomplete component. --- README.md | 2 +- addon/components/place-autocomplete-field.js | 8 +++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 6117632..51b6883 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ In order to use this addon you just have to use the component in your templates. types='(cities)' //You don't have to pass this value, default value is 'geocode' restrictions= restrictionsObjectFromController // You can pass and object with restriction options. withGeoLocate= true // You don't have to pass this value, default value is false - setValueWithProperty= 'formatted_address' // Optional, default is formatted_address + setValueWithProperty= 'formatted_address' // Optional, defaults to typical Google Autocomplete behavior preventSubmit= true // You don't have to pass this value, default value is false. Prevents the form to be submitted if the user hits ENTER }} diff --git a/addon/components/place-autocomplete-field.js b/addon/components/place-autocomplete-field.js index 58d72b9..2f9dccd 100644 --- a/addon/components/place-autocomplete-field.js +++ b/addon/components/place-autocomplete-field.js @@ -117,11 +117,9 @@ export default Component.extend({ let place = this.get('autocomplete').getPlace(); this._callCallback('placeChangedCallback', place); + // If setValueWithProperty is undefined, use Google Autocomplete default behavior if (place[this.get('setValueWithProperty')] !== undefined) { this.set('value', place[this.get('setValueWithProperty')]); - } else { - // Address not found use value - this.set('value', place.name); } }, @@ -150,11 +148,11 @@ export default Component.extend({ layout: layout, disabled: false, inputClass: 'place-autocomplete--input', - types: 'geocode', + types: undefined, restrictions: {}, tabindex: 0, withGeoLocate: false, - setValueWithProperty: 'formatted_address', + setValueWithProperty: undefined, preventSubmit: false }; From b029a51205a6116aafe437134b9e8ad848576470 Mon Sep 17 00:00:00 2001 From: Scott Vitale Date: Fri, 25 May 2018 16:05:29 -0600 Subject: [PATCH 2/3] Add array support to the types option as suggested in #48 --- addon/components/place-autocomplete-field.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/addon/components/place-autocomplete-field.js b/addon/components/place-autocomplete-field.js index 2f9dccd..d29fe1d 100644 --- a/addon/components/place-autocomplete-field.js +++ b/addon/components/place-autocomplete-field.js @@ -1,5 +1,6 @@ import layout from '../templates/components/place-autocomplete-field'; import Component from '@ember/component'; +import { isArray } from '@ember/array'; import { isEmpty, isPresent, typeOf, isEqual, isBlank } from '@ember/utils'; import { scheduleOnce, run } from "@ember/runloop"; @@ -136,9 +137,15 @@ export default Component.extend({ }, _typesToArray() { - if (this.get('types') !== '') { - return this.get('types').split(','); - } else { + let types = this.get('types'); + + if (isArray(types)) { + return types; + } + else if (typeOf(types) === 'string') { + return types.split(','); + } + else { return []; } }, From d9e45c062cf14cab592265e6a5f206923d8cd9e8 Mon Sep 17 00:00:00 2001 From: Scott Vitale Date: Thu, 31 May 2018 12:35:03 -0600 Subject: [PATCH 3/3] Fix failing tests and add new tests for cases where 'types' and 'setValueWithProperty' are not set. --- addon/components/place-autocomplete-field.js | 7 ++++++- .../components/place-autocomplete-field-test.js | 14 +++++++++++++- .../components/place-autocomplete-field-test.js | 14 ++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/addon/components/place-autocomplete-field.js b/addon/components/place-autocomplete-field.js index d29fe1d..9cc4241 100644 --- a/addon/components/place-autocomplete-field.js +++ b/addon/components/place-autocomplete-field.js @@ -143,7 +143,12 @@ export default Component.extend({ return types; } else if (typeOf(types) === 'string') { - return types.split(','); + if (types.trim() === '') { + return []; + } + else { + return types.split(','); + } } else { return []; diff --git a/tests/integration/components/place-autocomplete-field-test.js b/tests/integration/components/place-autocomplete-field-test.js index 1cfdc9a..0c5d947 100644 --- a/tests/integration/components/place-autocomplete-field-test.js +++ b/tests/integration/components/place-autocomplete-field-test.js @@ -21,7 +21,7 @@ describe('Integration | Component | Place Autocomplete Field', function() { expect(find('input').classList.contains('fake-input-class')).to.equal(true); }); - it("accepts 'value' option and updates with google autocomplete response", function() { + it("unset 'setValueWithProperty' option does not affect entered address", function() { // Mock only google places window.google.maps.__gjsload__ = function() { return true; @@ -30,6 +30,18 @@ describe('Integration | Component | Place Autocomplete Field', function() { let fakeModel = EmberObject.extend({ address: 'fake address'}).create(); this.set('fakeModel', fakeModel); this.render(hbs`{{place-autocomplete-field value=fakeModel.address}}`); + expect(this.get('fakeModel.address')).to.equal('fake address'); + }); + + it("accepts 'value' option and updates with google autocomplete response", function() { + // Mock only google places + window.google.maps.__gjsload__ = function() { + return true; + }; + window.google.maps.places.Autocomplete = GooglePlaceAutocompleteMockedObject; + let fakeModel = EmberObject.extend({ address: 'fake address'}).create(); + this.set('fakeModel', fakeModel); + this.render(hbs`{{place-autocomplete-field value=fakeModel.address setValueWithProperty='formatted_address'}}`); expect(this.get('fakeModel.address')).to.equal('Cra. 65, MedellĂ­n, Antioquia, Colombia'); }); diff --git a/tests/unit/components/place-autocomplete-field-test.js b/tests/unit/components/place-autocomplete-field-test.js index 0d47433..b29caec 100644 --- a/tests/unit/components/place-autocomplete-field-test.js +++ b/tests/unit/components/place-autocomplete-field-test.js @@ -5,6 +5,14 @@ import { setupTest } from 'ember-mocha'; describe('Integration | Component | PlaceAutocompleteField', function() { setupTest('component:place-autocomplete-field'); + it('returns empty array on undefined/null', function(){ + let component = this.subject(); + expect(component._typesToArray()).to.eql([]); + + component.set('types', null); + expect(component._typesToArray()).to.eql([]); + }); + it('converts types option to array', function(){ let component = this.subject(); component.set('types', 'geocode'); @@ -23,6 +31,12 @@ describe('Integration | Component | PlaceAutocompleteField', function() { expect(component._typesToArray()).to.eql([]); }); + it('supports array passed as types option', function() { + let component = this.subject(); + component.set('types', ['geocode', 'establishment']); + expect(component._typesToArray()).to.eql(['geocode', 'establishment']); + }); + it('get geolocate is not available', function(){ let component = this.subject(); let navigator = {