Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 36 additions & 10 deletions app/code/Magento/Ui/view/base/web/js/form/element/region.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ define([
}
},

/**
* {@inheritdoc}
*/
initialize: function () {
var option;

this._super();

option = _.find(this.countryOptions, function (row) {
return row['is_default'] === true;
});
this.hideRegion(option);

return this;
},

/**
* Method called every time country selector's value gets changed.
* Updates all validations and requirements for certain country.
Expand All @@ -42,16 +58,9 @@ define([
return;
}

defaultPostCodeResolver.setUseDefaultPostCode(!option['is_zipcode_optional']);

if (option['is_region_visible'] === false) {
// Hide select and corresponding text input field if region must not be shown for selected country.
this.setVisible(false);
this.hideRegion(option);

if (this.customEntry) { // eslint-disable-line max-depth
this.toggleInput(false);
}
}
defaultPostCodeResolver.setUseDefaultPostCode(!option['is_zipcode_optional']);

isRegionRequired = !this.skipValidation && !!option['is_region_required'];

Expand All @@ -67,7 +76,24 @@ define([
input.validation['required-entry'] = isRegionRequired;
input.validation['validate-not-number-first'] = !this.options().length;
}.bind(this));
},

/**
* Hide select and corresponding text input field if region must not be shown for selected country.
*
* @private
* @param {Object}option
*/
hideRegion: function (option) {
if (!option || option['is_region_visible'] !== false) {
return;
}

this.setVisible(false);

if (this.customEntry) {
this.toggleInput(false);
}
}
});
});

Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,40 @@ define([
});
});

describe('initialize method', function () {
it('Hides region field when it should be hidden for default country', function () {
model.countryOptions = {
'DefaultCountryCode': {
'is_default': true,
'is_region_visible': false
},
'NonDefaultCountryCode': {
'is_region_visible': true
}
};

model.initialize();

expect(model.visible()).toEqual(false);
});

it('Shows region field when it should be visible for default country', function () {
model.countryOptions = {
'CountryCode': {
'is_default': true,
'is_region_visible': true
},
'NonDefaultCountryCode': {
'is_region_visible': false
}
};

model.initialize();

expect(model.visible()).toEqual(true);
});
});

describe('update method', function () {
it('makes field optional when there is no corresponding country', function () {
var value = 'Value';
Expand Down