Skip to content

Commit c2f1ecd

Browse files
author
Tom Doan
committed
Add width option (issue 9); fix issue 10
1 parent 00e9117 commit c2f1ecd

File tree

4 files changed

+27
-17
lines changed

4 files changed

+27
-17
lines changed

README.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,17 @@ $(document).ready(function() {
5454

5555
## Options
5656

57-
Name | Type | Default | Description
58-
------------------- | -------- | ------------ | -----------
59-
`classic` | boolean | false | The default behavior is to move the selected item to the top. If you want the order of items to remain static, then set this to `true`.
60-
`customClass` | string | arrow | The class name to customize the drop-down menu style. The default `arrow` class displays a chevron-type arrow icon. Two additional helper classes are built in (add either or both to `arrow`): `triangle` converts the chevron into a solid triangle; `small` renders the arrow icon at half size.
61-
`height` | number | 50 | The drop-down menu item height. The minimum value is 8. Note that the maximum number of items displayed when the menu is opened is determined by the `size` attribute of the `<select>` element.
62-
`hoverIntent` | number | 200 | The wait period (in milliseconds) before collapsing the drop-down menu after you hover off of it. If you hover back onto the menu within the wait period, it will remain open. The minimum value is 0.
63-
`multiDelimiter` | string | ; | The separator character to use for the list of selected items in a multi-select menu.
64-
`multiVerbosity` | number | 99 | The maximum number of selected items to display in a multi-select menu before replacing it with a summary (e.g., "2/3 selected"). To display "0/3 selected" instead of "None selected", set this option to -1.
65-
`selectedMarker` | string | **&#10003;** | The icon or symbol to mark that an item is selected. HTML is accepted (e.g., `<i class="fa fa-check"></i>`).
66-
`afterLoad` | function | | Callback function to execute after the drop-down menu widget is loaded.
57+
Name | Type | Default | Description
58+
------------------- | ---------------- | ------------ | -----------
59+
`classic` | boolean | false | The default behavior is to move the selected item to the top. If you want the order of items to remain static, then set this to `true`.
60+
`customClass` | string | arrow | Class name to customize the drop-down menu style. The default `arrow` class displays a chevron-type arrow icon. Two additional helper classes are built in (add either or both to `arrow`): `triangle` converts the chevron into a solid triangle; `small` renders the arrow icon at half size.
61+
`width` | number or string | null | Drop-down menu width in pixels or percentage. Use a number for pixels or a string for percentage. By default, the width scales to fit the longest item in the drop-down menu.
62+
`height` | number | 50 | Drop-down menu height in pixels. The minimum value is 8. Note that the maximum number of items displayed when the menu is opened is determined by the `size` attribute of the `<select>` element.
63+
`hoverIntent` | number | 200 | Wait period (in milliseconds) before collapsing the drop-down menu after you hover off of it. If you hover back onto the menu within the wait period, it will remain open. The minimum value is 0.
64+
`multiDelimiter` | string | ; | Separator character to use for the list of selected items in a multi-select menu.
65+
`multiVerbosity` | number | 99 | Maximum number of selected items to display in a multi-select menu before replacing it with a summary (e.g., "2/3 selected"). To display "0/3 selected" instead of "None selected", set this option to -1.
66+
`selectedMarker` | string | **&#10003;** | Icon or symbol to mark that an item is selected. HTML is accepted (e.g., `<i class="fa fa-check"></i>`).
67+
`afterLoad` | function | | Callback function to execute after the drop-down menu widget is loaded.
6768

6869
## Methods
6970

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "pretty-dropdowns",
33
"description": "A simple, lightweight jQuery plugin to create stylized drop-down menus.",
4-
"version": "4.12.2",
4+
"version": "4.13.0",
55
"main": [
66
"dist/css/prettydropdowns.css",
77
"dist/js/jquery.prettydropdowns.js"

dist/js/jquery.prettydropdowns.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* jQuery Pretty Dropdowns Plugin v4.12.2 by T. H. Doan (https://thdoan.github.io/pretty-dropdowns/)
2+
* jQuery Pretty Dropdowns Plugin v4.13.0 by T. H. Doan (https://thdoan.github.io/pretty-dropdowns/)
33
*
44
* jQuery Pretty Dropdowns by T. H. Doan is licensed under the MIT License.
55
* Read a copy of the license in the LICENSE file or at https://choosealicense.com/licenses/mit/
@@ -12,6 +12,7 @@
1212
oOptions = $.extend({
1313
classic: false,
1414
customClass: 'arrow',
15+
width: null,
1516
height: 50,
1617
hoverIntent: 200,
1718
multiDelimiter: '; ',
@@ -22,7 +23,9 @@
2223

2324
oOptions.selectedMarker = '<span aria-hidden="true" class="checked"> ' + oOptions.selectedMarker + '</span>';
2425
// Validate options
25-
if (isNaN(oOptions.height) || oOptions.height<8) oOptions.height = 8;
26+
if (isNaN(oOptions.width) && !/^\d+%$/.test(oOptions.width)) oOptions.width = null;
27+
if (isNaN(oOptions.height)) oOptions.height = 50;
28+
else if (oOptions.height<8) oOptions.height = 8;
2629
if (isNaN(oOptions.hoverIntent) || oOptions.hoverIntent<0) oOptions.hoverIntent = 200;
2730
if (isNaN(oOptions.multiVerbosity)) oOptions.multiVerbosity = 99;
2831

@@ -137,7 +140,13 @@
137140
// Set dropdown width and event handler
138141
// NOTE: Setting width using width(), then css() because width() only can return a float,
139142
// which can result in a missing right border when there is a scrollbar.
140-
$items.width(nWidth).css('width', $items.css('width')).click(function() {
143+
$items.width(nWidth).css('width', $items.css('width'));
144+
if (oOptions.width) {
145+
$dropdown.parent().css('min-width', $items.css('width'));
146+
$dropdown.css('width', '100%');
147+
$items.css('width', '100%');
148+
}
149+
$items.click(function() {
141150
var $li = $(this),
142151
$selected = $dropdown.children('.selected');
143152
// Ignore disabled menu
@@ -230,7 +239,7 @@
230239
// Put focus on menu when user clicks on label
231240
if (sLabelId) $('#' + sLabelId).off('click', handleFocus).click(handleFocus);
232241
// Done with everything!
233-
$dropdown.parent().width(nOuterWidth||$dropdown.outerWidth(true)).removeClass('loading');
242+
$dropdown.parent().width(oOptions.width||nOuterWidth||$dropdown.outerWidth(true)).removeClass('loading');
234243
oOptions.afterLoad();
235244
},
236245

@@ -386,7 +395,7 @@
386395
++nCount;
387396
return '<li id="item' + nTimestamp + '-' + nCount + '"'
388397
+ (sGroup ? ' data-group="' + sGroup + '"' : '')
389-
+ (elOpt && elOpt.value ? ' data-value="' + elOpt.value + '"' : '')
398+
+ (elOpt && (elOpt.value||oOptions.classic) ? ' data-value="' + elOpt.value + '"' : '')
390399
+ (elOpt && elOpt.nodeName==='OPTION' ? ' role="option"' : '')
391400
+ (sTitle ? ' title="' + sTitle + '" aria-label="' + sTitle + '"' : '')
392401
+ (sClass ? ' class="' + $.trim(sClass) + '"' : '')

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pretty-dropdowns",
3-
"version": "4.12.2",
3+
"version": "4.13.0",
44
"description": "A simple, lightweight jQuery plugin to create stylized drop-down menus.",
55
"keywords": [
66
"jquery-plugin",

0 commit comments

Comments
 (0)