Skip to content

Commit

Permalink
feat(dropdown): add support for images/icons in api response param
Browse files Browse the repository at this point in the history
Adds support for image or icon properties to be used in JSON values for item data (either by values or most important by any API response)

Closes #872
  • Loading branch information
lubber-de authored and Sean committed Jul 15, 2019
1 parent cd890aa commit f033782
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
29 changes: 23 additions & 6 deletions src/definitions/modules/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -3952,7 +3952,11 @@ $.fn.dropdown.settings = {
name : 'name', // displayed dropdown text
value : 'value', // actual dropdown value
text : 'text', // displayed text when selected
type : 'type' // type of dropdown element
type : 'type', // type of dropdown element
image : 'image', // optional image path
imageClass : 'imageClass', // optional individual class for image
icon : 'icon', // optional icon name
iconClass : 'iconClass' // optional individual class for icon (for example to use flag instead)
},

keys : {
Expand Down Expand Up @@ -3999,6 +4003,8 @@ $.fn.dropdown.settings = {
dropdown : 'ui dropdown',
filtered : 'filtered',
hidden : 'hidden transition',
icon : 'icon',
image : 'image',
item : 'item',
label : 'ui label',
loading : 'loading',
Expand All @@ -4022,6 +4028,9 @@ $.fn.dropdown.settings = {

/* Templates */
$.fn.dropdown.settings.templates = {
deQuote: function(string) {
return String(string).replace(/"/g,"");
},
escape: function(string, preserveHTML) {
if (preserveHTML){
return string;
Expand Down Expand Up @@ -4052,7 +4061,8 @@ $.fn.dropdown.settings.templates = {
placeholder = select.placeholder || false,
values = select.values || [],
html = '',
escape = $.fn.dropdown.settings.templates.escape
escape = $.fn.dropdown.settings.templates.escape,
deQuote = $.fn.dropdown.settings.templates.deQuote
;
html += '<i class="dropdown icon"></i>';
if(placeholder) {
Expand All @@ -4063,7 +4073,7 @@ $.fn.dropdown.settings.templates = {
}
html += '<div class="'+className.menu+'">';
$.each(values, function(index, option) {
html += '<div class="'+(option.disabled ? className.disabled+' ':'')+className.item+'" data-value="' + String(option.value).replace(/"/g,"") + '">' + escape(option.name,preserveHTML) + '</div>';
html += '<div class="'+(option.disabled ? className.disabled+' ':'')+className.item+'" data-value="' + deQuote(option.value) + '">' + escape(option.name,preserveHTML) + '</div>';
});
html += '</div>';
return html;
Expand All @@ -4074,7 +4084,8 @@ $.fn.dropdown.settings.templates = {
var
values = response[fields.values] || [],
html = '',
escape = $.fn.dropdown.settings.templates.escape
escape = $.fn.dropdown.settings.templates.escape,
deQuote = $.fn.dropdown.settings.templates.deQuote
;
$.each(values, function(index, option) {
var
Expand All @@ -4086,13 +4097,19 @@ $.fn.dropdown.settings.templates = {
if( itemType === 'item' ) {
var
maybeText = (option[fields.text])
? ' data-text="' + String(option[fields.text]).replace(/"/g,"") + '"'
? ' data-text="' + deQuote(option[fields.text]) + '"'
: '',
maybeDisabled = (option[fields.disabled])
? className.disabled+' '
: ''
;
html += '<div class="'+ maybeDisabled + className.item+'" data-value="' + String(option[fields.value]).replace(/"/g,"") + '"' + maybeText + '>';
html += '<div class="'+ maybeDisabled + className.item+'" data-value="' + deQuote(option[fields.value]) + '"' + maybeText + '>';
if(option[fields.image]) {
html += '<img class="'+(option[fields.imageClass] ? deQuote(option[fields.imageClass]) : className.image)+'" src="' + deQuote(option[fields.image]) + '">';
}
if(option[fields.icon]) {
html += '<i class="'+deQuote(option[fields.icon])+' '+(option[fields.iconClass] ? deQuote(option[fields.iconClass]) : className.icon)+'"></i>';
}
html += escape(option[fields.name],preserveHTML);
html += '</div>';
} else if (itemType === 'header') {
Expand Down
10 changes: 10 additions & 0 deletions src/definitions/modules/dropdown.less
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,16 @@ select.ui.dropdown {
.ui.multiple.dropdown > .label ~ .text {
display: none;
}
.ui.multiple.dropdown > .label:not(.image) > img:not(.centered) {
margin-right: @itemElementDistance;
}
.ui.multiple.dropdown > .label:not(.image) > img.ui:not(.avatar) {
margin-bottom: @itemElementBottomDistance;
}
.ui.multiple.dropdown > .image.label img {
margin: @imageLabelImageMargin;
height: @imageLabelHeight;
}

/*-----------------
Multiple Search
Expand Down
4 changes: 4 additions & 0 deletions src/themes/default/modules/dropdown.variables
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
/* Item Sub-Element */
@itemElementFloat: none;
@itemElementDistance: @mini;
@itemElementBottomDistance: @itemElementDistance / 2;

/* Sub-Menu Dropdown Icon */
@itemDropdownIconDistance: 1em;
Expand Down Expand Up @@ -274,6 +275,9 @@
@labelHorizontalPadding: @relativeMini;
@labelPadding: @labelVerticalPadding @labelHorizontalPadding;

@imageLabelHeight: (1em + @labelVerticalPadding * 2); /* Logic adopted from label.less */
@imageLabelImageMargin: -@labelVerticalPadding @labelHorizontalPadding -@labelVerticalPadding -@labelHorizontalPadding;

/*-------------------
States
--------------------*/
Expand Down

0 comments on commit f033782

Please sign in to comment.