Skip to content

Commit

Permalink
Merge pull request #69 from svvitale/remove-default-types-and-setvalue
Browse files Browse the repository at this point in the history
Remove types and setValueWithProperty default values
  • Loading branch information
dmuneras authored May 31, 2018
2 parents 7a3b826 + d9e45c0 commit 10f8695
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}}

Expand Down
26 changes: 18 additions & 8 deletions addon/components/place-autocomplete-field.js
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -117,11 +118,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);
}
},

Expand All @@ -138,9 +137,20 @@ 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') {
if (types.trim() === '') {
return [];
}
else {
return types.split(',');
}
}
else {
return [];
}
},
Expand All @@ -150,11 +160,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
};

Expand Down
14 changes: 13 additions & 1 deletion tests/integration/components/place-autocomplete-field-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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');
});

Expand Down
14 changes: 14 additions & 0 deletions tests/unit/components/place-autocomplete-field-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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 = {
Expand Down

0 comments on commit 10f8695

Please sign in to comment.