Skip to content

Commit 6f9028e

Browse files
committed
Merge remote-tracking branch 'origin/imported-magento-magento2-30763' into 2.4-develop-pr103
2 parents 60301e2 + 375a49f commit 6f9028e

File tree

2 files changed

+70
-10
lines changed
  • app/code/Magento/Ui/view/base/web/js/form/element
  • dev/tests/js/jasmine/tests/app/code/Magento/Ui/base/js/form/element

2 files changed

+70
-10
lines changed

app/code/Magento/Ui/view/base/web/js/form/element/region.js

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@ define([
2323
}
2424
},
2525

26+
/**
27+
* {@inheritdoc}
28+
*/
29+
initialize: function () {
30+
var option;
31+
32+
this._super();
33+
34+
option = _.find(this.countryOptions, function (row) {
35+
return row['is_default'] === true;
36+
});
37+
this.hideRegion(option);
38+
39+
return this;
40+
},
41+
2642
/**
2743
* Method called every time country selector's value gets changed.
2844
* Updates all validations and requirements for certain country.
@@ -42,16 +58,9 @@ define([
4258
return;
4359
}
4460

45-
defaultPostCodeResolver.setUseDefaultPostCode(!option['is_zipcode_optional']);
46-
47-
if (option['is_region_visible'] === false) {
48-
// Hide select and corresponding text input field if region must not be shown for selected country.
49-
this.setVisible(false);
61+
this.hideRegion(option);
5062

51-
if (this.customEntry) { // eslint-disable-line max-depth
52-
this.toggleInput(false);
53-
}
54-
}
63+
defaultPostCodeResolver.setUseDefaultPostCode(!option['is_zipcode_optional']);
5564

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

@@ -67,7 +76,24 @@ define([
6776
input.validation['required-entry'] = isRegionRequired;
6877
input.validation['validate-not-number-first'] = !this.options().length;
6978
}.bind(this));
79+
},
80+
81+
/**
82+
* Hide select and corresponding text input field if region must not be shown for selected country.
83+
*
84+
* @private
85+
* @param {Object}option
86+
*/
87+
hideRegion: function (option) {
88+
if (!option || option['is_region_visible'] !== false) {
89+
return;
90+
}
91+
92+
this.setVisible(false);
93+
94+
if (this.customEntry) {
95+
this.toggleInput(false);
96+
}
7097
}
7198
});
7299
});
73-

dev/tests/js/jasmine/tests/app/code/Magento/Ui/base/js/form/element/region.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,40 @@ define([
4747
});
4848
});
4949

50+
describe('initialize method', function () {
51+
it('Hides region field when it should be hidden for default country', function () {
52+
model.countryOptions = {
53+
'DefaultCountryCode': {
54+
'is_default': true,
55+
'is_region_visible': false
56+
},
57+
'NonDefaultCountryCode': {
58+
'is_region_visible': true
59+
}
60+
};
61+
62+
model.initialize();
63+
64+
expect(model.visible()).toEqual(false);
65+
});
66+
67+
it('Shows region field when it should be visible for default country', function () {
68+
model.countryOptions = {
69+
'CountryCode': {
70+
'is_default': true,
71+
'is_region_visible': true
72+
},
73+
'NonDefaultCountryCode': {
74+
'is_region_visible': false
75+
}
76+
};
77+
78+
model.initialize();
79+
80+
expect(model.visible()).toEqual(true);
81+
});
82+
});
83+
5084
describe('update method', function () {
5185
it('makes field optional when there is no corresponding country', function () {
5286
var value = 'Value';

0 commit comments

Comments
 (0)