Skip to content

Commit 2ee5d0f

Browse files
authored
Merge pull request #132 from gearsdigital/feature/simplify-options-item-builder-api
simplify options item builder api
2 parents a73e938 + 96e019c commit 2ee5d0f

File tree

4 files changed

+67
-25
lines changed

4 files changed

+67
-25
lines changed

public/index.html

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,15 +254,37 @@ <h2>Options</h2>
254254

255255
/*
256256
* Type: String or Function
257-
* Description: Define how each option should be rendered inside its <li> element.
257+
* Description: Define how each option should be rendered inside its &lt;li&gt; element.
258258
*
259259
* If it's a string, all keys wrapped in brackets will be replaced by
260260
* the respective values in itemData. Available keys are:
261-
* 'value', 'text', 'slug', 'disabled'.
261+
* 'value', 'text', 'slug', 'index'.
262262
*
263-
* If it's a function, it will be called with the following parameters:
264-
* (itemData, element, index). The function must return a string,
265-
* no keys will be replaced in this method.
263+
* If it's a function, it will be called with the following parameter:
264+
* (itemData). The function must return a string. If available all keys
265+
* will be replaced by the respective values in itemData.
266+
*
267+
* itemData&lt;Object&gt; {
268+
* className // Type: String. Description: option class names.
269+
* disabled // Type: Boolean. Description: option is disabled true/false
270+
* selected // Type: Boolean. Description: option is selected true/false
271+
* element // Type: HTMLDomElement. Description: current select element
272+
* index // Type: Number. Description: current option index
273+
* slug // Type: String. Description: option slug
274+
* text // Type: String. Description: option text
275+
* value // Type: String. Description: option value
276+
* }
277+
*
278+
* EXAMPLE:
279+
*
280+
* function(itemData) {
281+
* return '<span class="item-{index}">{text}</span>';
282+
* }
283+
*
284+
* // you're free to build and return your own strings
285+
* function(itemData) {
286+
* return itemData.text + '(' + itemData.index + ')';
287+
* }
266288
*/
267289
optionsItemBuilder: '{text}',
268290

public/jquery.selectric.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* /,'
1010
* /'
1111
*
12-
* Selectric ϟ v1.11.0 (Sep 23 2016) - http://lcdsantos.github.io/jQuery-Selectric/
12+
* Selectric ϟ v1.11.0 (Sep 24 2016) - http://lcdsantos.github.io/jQuery-Selectric/
1313
*
1414
* Copyright (c) 2016 Leonardo Santos; MIT License
1515
*
@@ -542,23 +542,33 @@
542542
/**
543543
* Generate every option markup
544544
*
545-
* @param {number} i - Index of current item
546-
* @param {object} elm - Current item
545+
* @param {number} index - Index of current item
546+
* @param {object} itemData - Current item
547547
* @return {string} HTML for the option
548548
*/
549-
getItemMarkup: function(i, elm) {
549+
getItemMarkup: function(index, itemData) {
550550
var _this = this;
551551
var itemBuilder = _this.options.optionsItemBuilder;
552+
// limit access to item data to provide a simple interface
553+
// to most relevant options.
554+
var filteredItemData = {
555+
value: itemData.value,
556+
text : itemData.text,
557+
slug : itemData.slug,
558+
index: itemData.index
559+
};
552560

553561
return _this.utils.format('<li data-index="{1}" class="{2}">{3}</li>',
554-
i,
562+
index,
555563
_this.utils.arrayToClassname([
556-
elm.className,
557-
i === _this.items.length - 1 ? 'last' : '',
558-
elm.disabled ? 'disabled' : '',
559-
elm.selected ? 'selected' : ''
564+
itemData.className,
565+
index === _this.items.length - 1 ? 'last' : '',
566+
itemData.disabled ? 'disabled' : '',
567+
itemData.selected ? 'selected' : ''
560568
]),
561-
$.isFunction(itemBuilder) ? itemBuilder(elm, elm.element, i) : _this.utils.format(itemBuilder, elm)
569+
$.isFunction(itemBuilder)
570+
? _this.utils.format(itemBuilder(itemData), itemData)
571+
: _this.utils.format(itemBuilder, filteredItemData)
562572
);
563573
},
564574

0 commit comments

Comments
 (0)