Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for placeholder on type select #17

Merged
merged 3 commits into from
May 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/data/titlemaps.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
{
"key": "select2",
"type": "select",
"placeholder": "Please choose",
"titleMap": {
"a": "A",
"b": "B",
Expand All @@ -60,6 +61,7 @@
{
"key": "noenum",
"type": "select",
"placeholder": "Please choose",
"titleMap": [
{ "value":"a", "name": "A" },
{ "value":"b", "name":"B" },
Expand Down
29 changes: 28 additions & 1 deletion src/bootstrap-decorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,33 @@ function(decoratorsProvider, sfBuilderProvider, sfPathProvider) {
}
};

var selectPlaceholder = function(args) {
if (args.form.placeholder) {
var selectBox = args.fieldFrag.querySelector('select');
var option = document.createElement('option');
option.setAttribute('value', '');

/* We only want the placeholder to show when we do not have a value on the model.
* We make ngModel builder replace all so we can use $$value$$.
*/
option.setAttribute('sf-field-model', 'replaceAll');

/* https://github.com/angular/angular.js/issues/12190#issuecomment-115277040
* angular > 1.4 does a emptyOption.attr('selected', true)
* which does not like the ng-if comment.
*/
if (angular.version.major === 1 && angular.version.minor < 4) {
option.setAttribute('ng-if', '$$value$$ === undefined');
} else {
option.setAttribute('ng-show', '$$value$$ === undefined');
}

option.textContent = args.form.placeholder;

selectBox.appendChild(option);
}
};

var defaults = [sfField, ngModel, ngModelOptions, condition];
decoratorsProvider.defineDecorator('bootstrapDecorator', {
textarea: {template: base + 'textarea.html', builder: defaults},
Expand All @@ -38,7 +65,7 @@ function(decoratorsProvider, sfBuilderProvider, sfPathProvider) {
section: {template: base + 'section.html', builder: [sfField, simpleTransclusion, condition]},
conditional: {template: base + 'section.html', builder: [sfField, simpleTransclusion, condition]},
actions: {template: base + 'actions.html', builder: defaults},
select: {template: base + 'select.html', builder: defaults},
select: {template: base + 'select.html', builder: [selectPlaceholder, sfField, ngModel, ngModelOptions, condition]},
checkbox: {template: base + 'checkbox.html', builder: defaults},
checkboxes: {template: base + 'checkboxes.html', builder: [sfField, ngModelOptions, ngModel, array, condition]},
number: {template: base + 'default.html', builder: defaults},
Expand Down