Skip to content

Commit b3222cd

Browse files
yelahin-serhiytsviklinskyi
authored andcommitted
MC-32271: Order of State/Province drop down is not consistent in admin UI
1 parent 70c5c2f commit b3222cd

File tree

2 files changed

+49
-14
lines changed

2 files changed

+49
-14
lines changed

app/code/Magento/Checkout/view/frontend/web/js/region-updater.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,10 @@ define([
157157
regionInput = $(this.options.regionInputId),
158158
postcode = $(this.options.postcodeId),
159159
label = regionList.parent().siblings('label'),
160-
container = regionList.parents('div.field');
160+
container = regionList.parents('div.field'),
161+
regionsEntries,
162+
regionId,
163+
regionData;
161164

162165
this._clearError();
163166
this._checkRegionRequired(country);
@@ -168,8 +171,14 @@ define([
168171
// Populate state/province dropdown list if available or use input box
169172
if (this.options.regionJson[country]) {
170173
this._removeSelectOptions(regionList);
171-
$.each(this.options.regionJson[country], $.proxy(function (key, value) {
172-
this._renderSelectOption(regionList, key, value);
174+
regionsEntries = Object.entries(this.options.regionJson[country]);
175+
regionsEntries.sort(function (a, b) {
176+
return a[1].name > b[1].name ? 1 : -1;
177+
});
178+
$.each(regionsEntries, $.proxy(function (key, value) {
179+
regionId = value[0];
180+
regionData = value[1];
181+
this._renderSelectOption(regionList, regionId, regionData);
173182
}, this));
174183

175184
if (this.currentRegionOption) {

lib/web/mage/adminhtml/form.js

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ define([
133133
this.config = regions.config;
134134
delete regions.config;
135135
this.regions = regions;
136+
this.sortedRegions = this.getSortedRegions();
136137
this.disableAction = typeof disableAction === 'undefined' ? 'hide' : disableAction;
137138
this.clearRegionValueOnDisable = typeof clearRegionValueOnDisable === 'undefined' ?
138139
false : clearRegionValueOnDisable;
@@ -246,11 +247,13 @@ define([
246247
* Update.
247248
*/
248249
update: function () {
249-
var option, region, def, regionId;
250+
var option, selectElement, def, regionId, region;
250251

251-
if (this.regions[this.countryEl.value]) {
252+
selectElement = this.regionSelectEl;
253+
254+
if (this.sortedRegions[this.countryEl.value]) {
252255
if (this.lastCountryId != this.countryEl.value) { //eslint-disable-line eqeqeq
253-
def = this.regionSelectEl.getAttribute('defaultValue');
256+
def = selectElement.getAttribute('defaultValue');
254257

255258
if (this.regionTextEl) {
256259
if (!def) {
@@ -259,26 +262,27 @@ define([
259262
this.regionTextEl.value = '';
260263
}
261264

262-
this.regionSelectEl.options.length = 1;
265+
selectElement.options.length = 1;
263266

264-
for (regionId in this.regions[this.countryEl.value]) { //eslint-disable-line guard-for-in
265-
region = this.regions[this.countryEl.value][regionId];
267+
this.sortedRegions[this.countryEl.value].forEach(function (item) {
268+
regionId = item[0];
269+
region = item[1];
266270

267271
option = document.createElement('OPTION');
268272
option.value = regionId;
269273
option.text = region.name.stripTags();
270274
option.title = region.name;
271275

272-
if (this.regionSelectEl.options.add) {
273-
this.regionSelectEl.options.add(option);
276+
if (selectElement.options.add) {
277+
selectElement.options.add(option);
274278
} else {
275-
this.regionSelectEl.appendChild(option);
279+
selectElement.appendChild(option);
276280
}
277281

278282
if (regionId == def || region.name.toLowerCase() == def || region.code.toLowerCase() == def) { //eslint-disable-line
279-
this.regionSelectEl.value = regionId;
283+
selectElement.value = regionId;
280284
}
281-
}
285+
});
282286
}
283287

284288
if (this.disableAction == 'hide') { //eslint-disable-line eqeqeq
@@ -340,6 +344,28 @@ define([
340344
display ? marks[0].show() : marks[0].hide();
341345
}
342346
}
347+
},
348+
349+
/**
350+
* Sort regions from JSON by name
351+
*
352+
* @returns {*[]}
353+
*/
354+
getSortedRegions: function () {
355+
var country, regionsEntries, regionsByCountry;
356+
357+
regionsByCountry = [];
358+
359+
for (country in this.regions) { //eslint-disable-line guard-for-in
360+
regionsEntries = Object.entries(this.regions[country]);
361+
regionsEntries.sort(function (a, b) {
362+
return a[1].name > b[1].name ? 1 : -1;
363+
});
364+
365+
regionsByCountry[country] = regionsEntries;
366+
}
367+
368+
return regionsByCountry;
343369
}
344370
};
345371

0 commit comments

Comments
 (0)