Skip to content

Commit 18b5554

Browse files
authored
feat(dropdown): support custom data attributes for items
This PR allows to render custom data- attributes for each dropdown item by providing a data key object. Every key inside the data object will be rendered as data attribute (only "text" and "value" will be ignored as those are already used by the dropdown module internally)
1 parent 35a4548 commit 18b5554

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/definitions/modules/dropdown.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@
793793
}
794794
if (module.is.multiple()) {
795795
$.each(preSelected, function (index, value) {
796-
$item.filter('[data-value="' + value + '"]')
796+
$item.filter('[data-' + metadata.value + '="' + value + '"]')
797797
.addClass(className.filtered)
798798
;
799799
});
@@ -4171,6 +4171,7 @@
41714171
descriptionVertical: 'descriptionVertical', // whether description should be vertical
41724172
value: 'value', // actual dropdown value
41734173
text: 'text', // displayed text when selected
4174+
data: 'data', // custom data attributes
41744175
type: 'type', // type of dropdown element
41754176
image: 'image', // optional image path
41764177
imageClass: 'imageClass', // optional individual class for image
@@ -4315,9 +4316,21 @@
43154316
$.each(values, function (index, option) {
43164317
var
43174318
itemType = option[fields.type] || 'item',
4318-
isMenu = itemType.indexOf('menu') !== -1
4319+
isMenu = itemType.indexOf('menu') !== -1,
4320+
maybeData = '',
4321+
dataObject = option[fields.data]
43194322
;
4320-
4323+
if (dataObject) {
4324+
var dataKey,
4325+
dataKeyEscaped
4326+
;
4327+
for (dataKey in dataObject) {
4328+
dataKeyEscaped = String(dataKey).replace(/\W/g, '');
4329+
if (Object.prototype.hasOwnProperty.call(dataObject, dataKey) && ['text', 'value'].indexOf(dataKeyEscaped.toLowerCase()) === -1) {
4330+
maybeData += ' data-' + dataKeyEscaped + '="' + deQuote(String(dataObject[dataKey])) + '"';
4331+
}
4332+
}
4333+
}
43214334
if (itemType === 'item' || isMenu) {
43224335
var
43234336
maybeText = option[fields.text]
@@ -4334,7 +4347,7 @@
43344347
: '',
43354348
hasDescription = escape(option[fields.description] || '', preserveHTML) !== ''
43364349
;
4337-
html += '<div class="' + deQuote(maybeActionable + maybeDisabled + maybeDescriptionVertical + (option[fields.class] || className.item)) + '" data-value="' + deQuote(option[fields.value], true) + '"' + maybeText + '>';
4350+
html += '<div class="' + deQuote(maybeActionable + maybeDisabled + maybeDescriptionVertical + (option[fields.class] || className.item)) + '" data-value="' + deQuote(option[fields.value], true) + '"' + maybeText + maybeData + '>';
43384351
if (isMenu) {
43394352
html += '<i class="' + (itemType.indexOf('left') !== -1 ? 'left' : '') + ' dropdown icon"></i>';
43404353
}

0 commit comments

Comments
 (0)