Skip to content

Commit 0a40f4b

Browse files
committed
Merge pull request #17 from kornosaurus/select-placeholder
Add support for placeholder on type select
2 parents aa81f65 + 7c8e25d commit 0a40f4b

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

examples/data/titlemaps.json

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
{
5252
"key": "select2",
5353
"type": "select",
54+
"placeholder": "Please choose",
5455
"titleMap": {
5556
"a": "A",
5657
"b": "B",
@@ -60,6 +61,7 @@
6061
{
6162
"key": "noenum",
6263
"type": "select",
64+
"placeholder": "Please choose",
6365
"titleMap": [
6466
{ "value":"a", "name": "A" },
6567
{ "value":"b", "name":"B" },

src/bootstrap-decorator.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,33 @@ function(decoratorsProvider, sfBuilderProvider, sfPathProvider) {
2828
}
2929
};
3030

31+
var selectPlaceholder = function(args) {
32+
if (args.form.placeholder) {
33+
var selectBox = args.fieldFrag.querySelector('select');
34+
var option = document.createElement('option');
35+
option.setAttribute('value', '');
36+
37+
/* We only want the placeholder to show when we do not have a value on the model.
38+
* We make ngModel builder replace all so we can use $$value$$.
39+
*/
40+
option.setAttribute('sf-field-model', 'replaceAll');
41+
42+
/* https://github.com/angular/angular.js/issues/12190#issuecomment-115277040
43+
* angular > 1.4 does a emptyOption.attr('selected', true)
44+
* which does not like the ng-if comment.
45+
*/
46+
if (angular.version.major === 1 && angular.version.minor < 4) {
47+
option.setAttribute('ng-if', '$$value$$ === undefined');
48+
} else {
49+
option.setAttribute('ng-show', '$$value$$ === undefined');
50+
}
51+
52+
option.textContent = args.form.placeholder;
53+
54+
selectBox.appendChild(option);
55+
}
56+
};
57+
3158
var defaults = [sfField, ngModel, ngModelOptions, condition];
3259
decoratorsProvider.defineDecorator('bootstrapDecorator', {
3360
textarea: {template: base + 'textarea.html', builder: defaults},
@@ -38,7 +65,7 @@ function(decoratorsProvider, sfBuilderProvider, sfPathProvider) {
3865
section: {template: base + 'section.html', builder: [sfField, simpleTransclusion, condition]},
3966
conditional: {template: base + 'section.html', builder: [sfField, simpleTransclusion, condition]},
4067
actions: {template: base + 'actions.html', builder: defaults},
41-
select: {template: base + 'select.html', builder: defaults},
68+
select: {template: base + 'select.html', builder: [selectPlaceholder, sfField, ngModel, ngModelOptions, condition]},
4269
checkbox: {template: base + 'checkbox.html', builder: defaults},
4370
checkboxes: {template: base + 'checkboxes.html', builder: [sfField, ngModelOptions, ngModel, array, condition]},
4471
number: {template: base + 'default.html', builder: defaults},

0 commit comments

Comments
 (0)