Skip to content

Commit

Permalink
Merge pull request #82 from AnastasiaBuniakEPAM/JS-371
Browse files Browse the repository at this point in the history
JS-386: Refactor select.js
  • Loading branch information
uharbachou1 committed Jan 22, 2016
2 parents 405b3f7 + e9a287a commit 9277cb9
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 70 deletions.
6 changes: 6 additions & 0 deletions app/code/Magento/Checkout/Block/Cart/LayoutProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ public function process($jsLayout)
'options' => $this->countryCollection->load()->toOptionArray(),
'value' => null
],
'region' => [
'visible' => true,
'formElement' => 'input',
'label' => __('State/Province'),
'value' => null
],
'region_id' => [
'visible' => true,
'formElement' => 'select',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@
<item name="region_id" xsi:type="array">
<item name="component" xsi:type="string">Magento_Ui/js/form/element/region</item>
<item name="sortOrder" xsi:type="string">112</item>
<item name="dataScope" xsi:type="string">shippingAddress.region</item>
<item name="config" xsi:type="array">
<item name="template" xsi:type="string">ui/form/field</item>
<item name="elementTmpl" xsi:type="string">ui/form/element/select</item>
<item name="customEntry" xsi:type="string">shippingAddress.region</item>
</item>
<!-- Value of region_id field is filtered by the value of county_id attribute -->
<item name="filterBy" xsi:type="array">
Expand All @@ -101,11 +101,14 @@
</item>
<!-- The following items override configuration of corresponding address attributes -->
<item name="region" xsi:type="array">
<!-- Make region attribute invisible on frontend. Corresponding input element is created by region_id field -->
<item name="visible" xsi:type="boolean">false</item>
<item name="component" xsi:type="string">Magento_Ui/js/form/element/abstract</item>
<item name="component" xsi:type="string">Magento_Ui/js/form/element/region_input</item>
<item name="sortOrder" xsi:type="string">111</item>
<item name="dataScope" xsi:type="string">shippingAddress.region</item>
<item name="dataScope" xsi:type="string">shippingAddress.region_input</item>
<item name="provider" xsi:type="string">checkoutProvider</item>
<item name="config" xsi:type="array">
<item name="template" xsi:type="string">ui/form/field</item>
<item name="elementTmpl" xsi:type="string">ui/form/element/input</item>
</item>
</item>
<item name="postcode" xsi:type="array">
<!-- post-code field has custom UI component -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,13 @@
<item name="children" xsi:type="array">
<!-- The following items override configuration of corresponding address attributes -->
<item name="region" xsi:type="array">
<!-- Make region attribute invisible on frontend. Corresponding input element is created by region_id field -->
<item name="visible" xsi:type="boolean">false</item>
<item name="component" xsi:type="string">Magento_Ui/js/form/element/region_input</item>
</item>
<item name="region_id" xsi:type="array">
<item name="component" xsi:type="string">Magento_Ui/js/form/element/region</item>
<item name="config" xsi:type="array">
<item name="template" xsi:type="string">ui/form/field</item>
<item name="elementTmpl" xsi:type="string">ui/form/element/select</item>
<item name="customEntry" xsi:type="string">shippingAddress.region</item>
</item>
<item name="validation" xsi:type="array">
<item name="required-entry" xsi:type="boolean">true</item>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,9 @@
<field name="region">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="visible" xsi:type="boolean">false</item>
<item name="dataType" xsi:type="string">text</item>
<item name="formElement" xsi:type="string">input</item>
<item name="component" xsi:type="string">Magento_Ui/js/form/element/region_input</item>
<item name="source" xsi:type="string">address</item>
</item>
</argument>
Expand All @@ -418,7 +418,6 @@
<item name="dataType" xsi:type="string">text</item>
<item name="formElement" xsi:type="string">select</item>
<item name="source" xsi:type="string">address</item>
<item name="customEntry" xsi:type="string">region</item>
<item name="validation" xsi:type="array">
<item name="required-entry" xsi:type="boolean">true</item>
</item>
Expand Down
40 changes: 40 additions & 0 deletions app/code/Magento/Ui/view/base/web/js/form/element/region_input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

define([
'underscore',
'uiRegistry',
'./abstract'
], function (_, registry, Abstract) {
'use strict';

return Abstract.extend({
defaults: {
imports: {
update: '${ $.parentName }.country_id:value'
}
},

/**
* Set visible true if no country was chosen and toggle element if region_id was changed.
*
* @param {String} value
*/
update: function (value) {
var country = registry.get(this.parentName + '.' + 'country_id'),
options = country.indexedOptions,
option;

if (!value) {
this.setVisible(true);

return;
}

option = options[value];
this.setVisible(!option['is_region_required']);
}
});
});
33 changes: 5 additions & 28 deletions app/code/Magento/Ui/view/base/web/js/form/element/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ define([
'underscore',
'mageUtils',
'uiRegistry',
'./abstract',
'uiLayout'
], function (_, utils, registry, Abstract, layout) {
'./abstract'
], function (_, utils, registry, Abstract) {
'use strict';

var inputNode = {
Expand Down Expand Up @@ -111,15 +110,10 @@ define([
/**
* Extends instance with defaults, extends config with formatted values
* and options, and invokes initialize method of AbstractElement class.
* If instance's 'customEntry' property is set to true, calls 'initInput'
*/
initialize: function () {
this._super();

if (this.customEntry) {
this.initInput();
}

if (this.filterBy) {
this.initFilter();
}
Expand Down Expand Up @@ -182,17 +176,6 @@ define([
return this;
},

/**
* Creates input from template, renders it via renderer.
*
* @returns {Object} Chainable.
*/
initInput: function () {
layout([utils.template(inputNode, this)]);

return this;
},

/**
* Matches specfied value with existing options
* or, if value is not specified, returns value of the first option.
Expand Down Expand Up @@ -246,9 +229,7 @@ define([
},

/**
* Sets 'data' to 'options' observable array, if instance has
* 'customEntry' property set to true, calls 'setHidden' method
* passing !options.length as a parameter
* Sets 'data' to 'options' observable array
*
* @param {Array} data
* @returns {Object} Chainable
Expand All @@ -260,12 +241,8 @@ define([

this.options(data);

if (this.customEntry) {
isVisible = !!data.length;

this.setVisible(isVisible);
this.toggleInput(!isVisible);
}
isVisible = !!data.length;
this.setVisible(isVisible);

return this;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,6 @@ define([
expect(model.setLinks).toHaveBeenCalled();
});
});
describe('initInput method', function () {
it('check for chainable', function () {
expect(model.initInput()).toEqual(model);
});
});
describe('getOption method', function () {
it('check existed option', function () {
model.indexedOptions = {
Expand Down Expand Up @@ -219,33 +214,6 @@ define([
it('check for chainable', function () {
expect(model.setOptions([])).toEqual(model);
});
it('check for default customEntry', function () {
var data = [{
value: 'First'
}, {
value: 'Second'
}];

spyOn(model, 'setVisible');
spyOn(model, 'toggleInput');
expect(model.setOptions(data)).toEqual(model);
expect(model.setVisible).not.toHaveBeenCalled();
expect(model.toggleInput).not.toHaveBeenCalled();
});
it('check for customEntry', function () {
var data = [{
value: 'First'
}, {
value: 'Second'
}];

model.customEntry = true;
spyOn(model, 'setVisible');
spyOn(model, 'toggleInput');
expect(model.setOptions(data)).toEqual(model);
expect(model.setVisible).toHaveBeenCalled();
expect(model.toggleInput).toHaveBeenCalled();
});
});
describe('getPreview method', function () {
it('check for default preview', function () {
Expand Down

0 comments on commit 9277cb9

Please sign in to comment.