Skip to content

Commit b125f54

Browse files
committed
Update to support optgroup tags
- options can now be nested inside optgroup tags - removed js adding .hover class to li elements -- css :hover pseudoclass is much cleaner
1 parent f9fbeff commit b125f54

File tree

4 files changed

+55
-225
lines changed

4 files changed

+55
-225
lines changed

fancySelect.coffee

+19-24
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ $.fn.fancySelect = (opts = {}) ->
135135

136136
# Handle item selection, and
137137
# Add class selected to selected item
138-
options.on 'click', 'li', (e) ->
138+
options.on 'click', 'li.option', (e) ->
139139
sel.val($(this).data('value'))
140140

141141
sel.trigger('blur').trigger('focus') unless isiOS
@@ -144,37 +144,32 @@ $.fn.fancySelect = (opts = {}) ->
144144
$(e.currentTarget).addClass 'selected'
145145
return sel.val($(this).data('value')).trigger('change').trigger('blur').trigger('focus')
146146

147-
# handle mouse selection
148-
options.on 'mouseenter', 'li', ->
149-
nowHovered = $(this)
150-
hovered = options.find('.hover')
151-
hovered.removeClass 'hover'
152-
153-
nowHovered.addClass 'hover'
154-
155-
options.on 'mouseleave', 'li', ->
156-
options.find('.hover').removeClass('hover')
157-
158147
copyOptionsToList = ->
159148
# update our trigger to reflect the select (it really already should, this is just a safety)
160149
updateTriggerText()
161150

162151
return if isiOS && !settings.forceiOS
163152

164-
# snag current options before we add a default one
165-
selOpts = sel.find 'option'
166-
167-
# generate list of options for the fancySelect
168-
169-
sel.find('option').each (i, opt) ->
153+
sel.children().each (i, opt) ->
170154
opt = $(opt)
155+
if opt.is('option')
156+
createLiFromOption opt, options
157+
else if opt.is('optgroup')
158+
optgroup = opt
159+
options.append "<li class='optgroup'><span class='optgroup-label'>#{optgroup.prop 'label'}</span><ul id=optgroup_#{i}>"
160+
ul = $("#optgroup_#{i}")
161+
optgroup.children().each (i, nested_opt) ->
162+
nested_opt = $(nested_opt)
163+
createLiFromOption nested_opt, ul
164+
165+
createLiFromOption = (opt, parent) ->
166+
if !opt.prop('disabled') && (opt.val() || settings.includeBlank)
167+
# Is there a select option on page load?
168+
if opt.prop('selected')
169+
parent.append "<li data-value='#{opt.val()}' class='option selected'>#{opt.text()}</li>"
170+
else
171+
parent.append "<li data-value='#{opt.val()}' class='option'>#{opt.text()}</li>"
171172

172-
if !opt.prop('disabled') && (opt.val() || settings.includeBlank)
173-
# Is there a select option on page load?
174-
if opt.prop('selected')
175-
options.append "<li data-value=\"#{opt.val()}\" class=\"selected\">#{opt.text()}</li>"
176-
else
177-
options.append "<li data-value=\"#{opt.val()}\">#{opt.text()}</li>"
178173

179174
# for updating the list of options after initialization
180175
sel.on 'update', ->

fancySelect.css

+23-1
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,33 @@ div.fancy-select ul.options li {
136136
-o-transition: all 150ms ease-out;
137137
}
138138

139+
div.fancy-select ul.options li.option {
140+
padding: 8px 12px;
141+
}
142+
143+
div.fancy-select ul.options span.optgroup-label {
144+
display: block;
145+
padding: 8px 12px;
146+
font-style: italic;
147+
color: #323d42;
148+
}
149+
150+
div.fancy-select ul.options li.optgroup {
151+
padding: 0;
152+
}
153+
div.fancy-select ul.options li.optgroup ul {
154+
padding: 0;
155+
}
156+
div.fancy-select ul.options li.optgroup li {
157+
padding: 8px 12px 8px 24px;
158+
}
159+
139160
div.fancy-select ul.options li.selected {
140161
background: rgba(43,134,134,0.3);
141162
color: rgba(255,255,255,0.75);
142163
}
143164

144-
div.fancy-select ul.options li.hover {
165+
div.fancy-select ul.options li.option:hover {
145166
color: #fff;
167+
background: rgba(43,134,134,0.2);
146168
}

fancySelect.js

+2-193
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.html

+11-7
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
<script src="fancySelect.js"></script>
4848
<script>
4949
$(document).ready(function() {
50-
$('#basic-usage-demo').fancySelect();
50+
$('#basic-usage-demo').fancySelect({includeBlank:true});
5151

5252
// Boilerplate
5353
var repoName = 'fancyselect'
@@ -141,12 +141,16 @@ <h1>FancySelect</h1>
141141
<div id="main">
142142
<section id="basic-usage">
143143
<select id="basic-usage-demo">
144-
<option value="">Pick Something…</option>
145-
<option>Lorem Ipsum</option>
146-
<option>Dolor Sit</option>
147-
<option>Vehicula Ornare</option>
148-
<option>Foo</option>
149-
<option>Bar</option>
144+
<option value>Pick Something…</option>
145+
<optgroup label="group 1">
146+
<option>Lorem Ipsum</option>
147+
<option>Dolor Sit</option>
148+
<option>Vehicula Ornare</option>
149+
</optgroup>
150+
<optgroup label="group 2">
151+
<option>Foo</option>
152+
<option>Bar</option>
153+
</optgroup>
150154
<option>Baz</option>
151155
<option>Qux</option>
152156
<option>Zoobie</option>

0 commit comments

Comments
 (0)