Skip to content

Commit 2cf33c9

Browse files
committed
Merge pull request #418 from Fridus/feature-select-options-trackBy
Add parameter `trackBy` on form type select.
2 parents 556203e + 09bd531 commit 2cf33c9

File tree

4 files changed

+88
-3
lines changed

4 files changed

+88
-3
lines changed

src/directives/decorators/bootstrap/select.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
sf-changed="form"
1010
class="form-control {{form.fieldHtmlClass}}"
1111
schema-validate="form"
12-
ng-options="item.value as item.name group by item.group for item in form.titleMap"
12+
ng-options="item.value as item.name group by item.group for item in form.titleMap track by item[form.trackBy]"
1313
name="{{form.key.slice(-1)[0]}}">
1414
</select>
1515
<div class="help-block" sf-message="form.description"></div>

src/services/schema-form.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ angular.module('schemaForm').provider('schemaForm',
1313
return type[0];
1414
}
1515
return type;
16-
}
16+
};
1717

1818
//Creates an default titleMap list from an enum, i.e. a list of strings.
1919
var enumToTitleMap = function(enm) {
@@ -146,6 +146,7 @@ angular.module('schemaForm').provider('schemaForm',
146146
if (!f.titleMap) {
147147
f.titleMap = enumToTitleMap(schema['enum']);
148148
}
149+
f.trackBy = 'value';
149150
options.lookup[sfPathProvider.stringify(options.path)] = f;
150151
return f;
151152
}
@@ -339,6 +340,10 @@ angular.module('schemaForm').provider('schemaForm',
339340
obj.titleMap = canonicalTitleMap(obj.titleMap);
340341
}
341342

343+
if(obj.type === 'select') {
344+
obj.trackBy = obj.trackBy || 'value';
345+
}
346+
342347
//
343348
if (obj.itemForm) {
344349
obj.items = [];

test/directives/schema-form-test.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,6 +1649,85 @@ describe('directive',function(){
16491649
});
16501650

16511651

1652+
it('should show the correct option selected', function() {
1653+
inject(function($compile,$rootScope){
1654+
var scope = $rootScope.$new();
1655+
scope.thing = { id: 2, a: 1};
1656+
1657+
scope.schema = {
1658+
"type": "object",
1659+
"properties": {
1660+
"thing": {
1661+
"title": "Thing"
1662+
}
1663+
}
1664+
};
1665+
1666+
scope.form = [{
1667+
key: "thing",
1668+
type: 'select',
1669+
titleMap: [{
1670+
name: 'abc', value: { id: 1, a: 2 }
1671+
},{
1672+
name: 'def', value: { id: 2, a: 1 }
1673+
},{
1674+
name: 'ghi', value: { id: 3, a: 3 }
1675+
}]
1676+
}];
1677+
1678+
var tmpl = angular.element('<form sf-schema="schema" sf-form="form" sf-model="thing"></form>');
1679+
1680+
$compile(tmpl)(scope);
1681+
$rootScope.$apply();
1682+
1683+
setTimeout(function() {
1684+
tmpl.children().eq(0).find('select').eq(0).val().should.be.eq(2);
1685+
}, 0);
1686+
1687+
});
1688+
});
1689+
1690+
1691+
it('should show the correct option selected with `track by`', function() {
1692+
inject(function($compile,$rootScope){
1693+
var scope = $rootScope.$new();
1694+
scope.thing = { id: 2, a: 1, b: 2 };
1695+
1696+
scope.schema = {
1697+
"type": "object",
1698+
"properties": {
1699+
"thing": {
1700+
"title": "Thing"
1701+
}
1702+
}
1703+
};
1704+
1705+
scope.form = [{
1706+
key: "thing",
1707+
type: 'select',
1708+
titleMap: [{
1709+
id: 1, name: 'abc', value: { id: 1 }
1710+
},{
1711+
id: 2, name: 'def', value: { id: 2 }
1712+
},{
1713+
id: 3, name: 'ghi', value: { id: 3 }
1714+
}],
1715+
trackBy: 'id'
1716+
}];
1717+
1718+
var tmpl = angular.element('<form sf-schema="schema" sf-form="form" sf-model="thing"></form>');
1719+
1720+
$compile(tmpl)(scope);
1721+
$rootScope.$apply();
1722+
1723+
setTimeout(function() {
1724+
tmpl.children().eq(0).find('select').eq(0).val().should.be.eq(2);
1725+
}, 0);
1726+
1727+
});
1728+
});
1729+
1730+
16521731
it('should update array form on model array ref change',function(){
16531732

16541733
inject(function($compile,$rootScope){

test/services/schema-form-test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ describe('schemaForm', function() {
9191
"name": "NaN",
9292
"value": "NaN"
9393
}
94-
]
94+
],
95+
"trackBy": "value"
9596
},
9697
{
9798
"title": "Are you over 18 years old?",

0 commit comments

Comments
 (0)