Skip to content

Commit 26009b5

Browse files
committed
Merge branch 'enum-maintain-order-select-options-2' of https://github.com/EagerIO/angular-schema-form into EagerIO-enum-maintain-order-select-options-2
2 parents 3bd67ff + a363e67 commit 26009b5

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

src/services/schema-form.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,18 @@ angular.module('schemaForm').provider('schemaForm',
1717

1818
// Takes a titleMap in either object or list format and returns one in
1919
// in the list format.
20-
var canonicalTitleMap = function(titleMap) {
20+
var canonicalTitleMap = function(titleMap, originalEnum) {
2121
if (!angular.isArray(titleMap)) {
2222
var canonical = [];
23-
angular.forEach(titleMap, function(name, value) {
24-
canonical.push({name: name, value: value});
25-
});
23+
if (originalEnum) {
24+
angular.forEach(originalEnum, function(value, index) {
25+
canonical.push({name: titleMap[value], value: value});
26+
});
27+
} else {
28+
angular.forEach(titleMap, function(name, value) {
29+
canonical.push({name: name, value: value});
30+
});
31+
}
2632
return canonical;
2733
}
2834
return titleMap;
@@ -63,7 +69,7 @@ angular.module('schemaForm').provider('schemaForm',
6369

6470
//Non standard attributes
6571
if (schema.validationMessage) { f.validationMessage = schema.validationMessage; }
66-
if (schema.enumNames) { f.titleMap = canonicalTitleMap(schema.enumNames); }
72+
if (schema.enumNames) { f.titleMap = canonicalTitleMap(schema.enumNames, schema.enum); }
6773
f.schema = schema;
6874

6975
// Ng model options doesn't play nice with undefined, might be defined

test/schema-form-test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,6 +1571,46 @@ describe('Schema form',function(){
15711571
});
15721572
});
15731573

1574+
it('should sort select options by enum',function(){
1575+
1576+
inject(function($compile,$rootScope){
1577+
var scope = $rootScope.$new();
1578+
scope.person = {};
1579+
1580+
scope.schema = {
1581+
"type": "object",
1582+
"properties": {
1583+
"thing": {
1584+
"type": "string",
1585+
"title": "Thing",
1586+
"enum": [
1587+
// Intentionally non-alphabetical
1588+
// https://github.com/Textalk/angular-schema-form/issues/82
1589+
// https://github.com/Textalk/angular-schema-form/issues/83
1590+
"b",
1591+
"a"
1592+
],
1593+
"enumNames": {
1594+
// Intentionally not the same order as the `enum`
1595+
"a": "The A",
1596+
"b": "The B"
1597+
}
1598+
}
1599+
}
1600+
};
1601+
1602+
scope.form = ["*"];
1603+
1604+
var tmpl = angular.element('<form sf-schema="schema" sf-form="form" sf-model="person"></form>');
1605+
1606+
$compile(tmpl)(scope);
1607+
$rootScope.$apply();
1608+
1609+
tmpl.children().eq(0).find('select').eq(0).find('option').eq(0).text().trim().should.be.eq('');
1610+
tmpl.children().eq(0).find('select').eq(0).find('option').eq(1).text().trim().should.be.eq('The B');
1611+
tmpl.children().eq(0).find('select').eq(0).find('option').eq(2).text().trim().should.be.eq('The A');
1612+
});
1613+
});
15741614

15751615
});
15761616

0 commit comments

Comments
 (0)