Skip to content

Commit

Permalink
Fix subform repeat counter. (#19693)
Browse files Browse the repository at this point in the history
* Fix subform repeat counter.

As normal rows should be zero indexed.

When length === count === 0, first row should be 0.

Note: PHP creates existing rows with zero index - so if min=1, you
currently get row 0 on form load then row 2, 3 etc. as you add them.

* Update unit tests

* Additional test fixes.

* Fix typo in comment.

* Reminify subform-repeatable.js
  • Loading branch information
Sophist-UK authored and Michael Babker committed May 12, 2018
1 parent 7ee41b1 commit f8e7344
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
9 changes: 4 additions & 5 deletions media/system/js/subform-repeatable-uncompressed.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
$.subformRepeatable = function(container, options){
this.$container = $(container);

// check if alredy exist
// check if already exist
if(this.$container.data("subformRepeatable")){
return self;
}
Expand Down Expand Up @@ -144,14 +144,13 @@

// fix names ind id`s for field that in $row
$.subformRepeatable.prototype.fixUniqueAttributes = function($row, count){
this.lastRowNum++;
var group = $row.attr('data-group'),// current group name
basename = $row.attr('data-base-name'), // group base name, without count
count = count || 0,
countnew = Math.max(this.lastRowNum, count + 1),
groupnew = basename + countnew; // new group name
countnew = Math.max(this.lastRowNum, count),
groupnew = basename + countnew; // new group name

this.lastRowNum = countnew;
this.lastRowNum = countnew + 1;
$row.attr('data-group', groupnew);

// Fix inputs that have a "name" attribute
Expand Down
3 changes: 1 addition & 2 deletions media/system/js/subform-repeatable.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 11 additions & 11 deletions tests/javascript/subform-repeatable/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,26 @@ define(['jquery', 'testsRoot/subform-repeatable/spec-setup', 'jasmineJquery'], f
expect($container.find('tbody').children().length).toEqual(2);
});

it('Should fix the id of the template input checkbox element to "jform_group__group2__checkbox"', function () {
it('Should fix the id of the template input checkbox element to "jform_group__group1__checkbox"', function () {
expect($container.find('#jform_group__groupX__checkbox')).not.toExist();
expect($container.find('#jform_group__group2__checkbox')).toExist();
expect($container.find('#jform_group__group1__checkbox')).toExist();
});

it('Should fix the for attribute of the checkbox label element to match the changed input id', function () {
expect($container.find('label[for="jform_group__group2__checkbox"]')).toExist();
expect($container.find('label[for="jform_group__group1__checkbox"]')).toExist();
});

it('Should fix the name of the template input checkbox element to "jform[group][group2][checkbox]"', function () {
expect($container.find('#jform_group__group2__checkbox')).toHaveAttr('name','jform[group][group2][checkbox]');
it('Should fix the name of the template input checkbox element to "jform[group][group1][checkbox]"', function () {
expect($container.find('#jform_group__group1__checkbox')).toHaveAttr('name','jform[group][group1][checkbox]');
});

it('Should fix the id of the template input radio element to "jform_group__group2__radio0"', function () {
it('Should fix the id of the template input radio element to "jform_group__group1__radio0"', function () {
expect($container.find('#jform_group__groupX__radio0')).not.toExist();
expect($container.find('#jform_group__group2__radio0')).toExist();
expect($container.find('#jform_group__group1__radio0')).toExist();
});

it('Should fix the name of the template input radio element to "jform[group][group2][radio]"', function () {
expect($container.find('#jform_group__group2__radio0')).toHaveAttr('name','jform[group][group2][radio]');
it('Should fix the name of the template input radio element to "jform[group][group1][radio]"', function () {
expect($container.find('#jform_group__group1__radio0')).toHaveAttr('name','jform[group][group1][radio]');
});

it('Should have captured the template correctly', function () {
Expand All @@ -76,8 +76,8 @@ define(['jquery', 'testsRoot/subform-repeatable/spec-setup', 'jasmineJquery'], f
expect($container.find('tbody').children().last()).toHaveAttr('data-new', 'true');
});

it('Should set data-group attribute to "group2" in the new element', function () {
expect($container.find('tbody').children().last()).toHaveAttr('data-group', 'group2');
it('Should set data-group attribute to "group1" in the new element', function () {
expect($container.find('tbody').children().last()).toHaveAttr('data-group', 'group1');
});

it('Should trigger subform-row-add event', function () {
Expand Down

0 comments on commit f8e7344

Please sign in to comment.