Skip to content

Commit e2f4245

Browse files
authored
Merge pull request #11 from JJJ/issue/2903
Issue/2903
2 parents a524f7a + 7ca7624 commit e2f4245

File tree

7 files changed

+97
-17
lines changed

7 files changed

+97
-17
lines changed

coffee/lib/abstract-chosen.coffee

+2
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,8 @@ class AbstractChosen
315315
return false if @is_multiple and (not @display_selected_options and option.selected)
316316
return false if not @display_disabled_options and option.disabled
317317
return false if option.empty
318+
return false if option.hidden
319+
return false if option.group_array_index? and @results_data[option.group_array_index].hidden
318320

319321
return true
320322

coffee/lib/select-parser.coffee

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ class SelectParser
1818
label: group.label
1919
title: group.title if group.title
2020
children: 0
21-
disabled: group.disabled,
21+
disabled: group.disabled
22+
hidden: group.hidden
2223
classes: group.className
2324
this.add_option( option, group_position, group.disabled ) for option in group.childNodes
2425

@@ -36,6 +37,7 @@ class SelectParser
3637
title: option.title if option.title
3738
selected: option.selected
3839
disabled: if group_disabled is true then group_disabled else option.disabled
40+
hidden: option.hidden
3941
group_array_index: group_position
4042
group_label: if group_position? then @parsed[group_position].label else null
4143
classes: option.className

public/index.html

+8-7
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ <h2><a name="standard-select" class="anchor" href="#standard-select">Standard Se
3030
<div>
3131
<em>Turns This</em>
3232
<select data-placeholder="Choose a Country..." class="select" tabindex="1">
33-
<option value=""></option>
33+
<option value="" selected disabled hidden>Choose a Country...</option>
3434
<option value="United States">United States</option>
3535
<option value="United Kingdom">United Kingdom</option>
3636
<option value="Afghanistan">Afghanistan</option>
@@ -287,7 +287,7 @@ <h2><a name="standard-select" class="anchor" href="#standard-select">Standard Se
287287
<div>
288288
<em>Into This</em>
289289
<select data-placeholder="Choose a Country..." class="chosen-select" tabindex="2">
290-
<option value=""></option>
290+
<option value="" selected disabled hidden>Choose a Country...</option>
291291
<option value="United States">United States</option>
292292
<option value="United Kingdom">United Kingdom</option>
293293
<option value="Afghanistan">Afghanistan</option>
@@ -1173,16 +1173,16 @@ <h2><a name="optgroup-support" class="anchor" href="#optgroup-support">&lt;optgr
11731173
</div>
11741174
</div>
11751175

1176-
<h2><a name="selected-and-disabled-support" class="anchor" href="#selected-and-disabled-support">Selected and Disabled Support</a></h2>
1176+
<h2><a name="selected-and-disabled-support" class="anchor" href="#selected-and-disabled-support">Selected, Disabled and Hidden Support</a></h2>
11771177
<div class="side-by-side clearfix">
1178-
<p>Chosen automatically highlights selected options and removes disabled options.</p>
1178+
<p>Chosen automatically highlights selected options and removes disabled and / or hidden options.</p>
11791179
<div>
11801180
<em>Single Select</em>
11811181
<select data-placeholder="Your Favorite Type of Bear" class="chosen-select" tabindex="7">
11821182
<option value=""></option>
11831183
<option>American Black Bear</option>
11841184
<option>Asiatic Black Bear</option>
1185-
<option>Brown Bear</option>
1185+
<option hidden>Brown Bear</option>
11861186
<option>Giant Panda</option>
11871187
<option selected>Sloth Bear</option>
11881188
<option disabled>Sun Bear</option>
@@ -1196,7 +1196,7 @@ <h2><a name="selected-and-disabled-support" class="anchor" href="#selected-and-d
11961196
<option value=""></option>
11971197
<option>American Black Bear</option>
11981198
<option>Asiatic Black Bear</option>
1199-
<option>Brown Bear</option>
1199+
<option hidden>Brown Bear</option>
12001200
<option>Giant Panda</option>
12011201
<option selected>Sloth Bear</option>
12021202
<option disabled>Sun Bear</option>
@@ -1231,7 +1231,8 @@ <h2><a name="default-text-support" class="anchor" href="#default-text-support">D
12311231
<div class="side-by-side clearfix">
12321232
<p>Chosen automatically sets the default field text ("Choose a country...") by reading the select element's data-placeholder value. If no data-placeholder value is present, it will default to "Select an Option" or "Select Some Options" depending on whether the select is single or multiple. You can change these elements in the plugin js file as you see fit.</p>
12331233
<pre><code class="language-markup">&lt;select <strong>data-placeholder="Choose a country..."</strong> multiple class="chosen-select"&gt;</code></pre>
1234-
<p><strong>Note:</strong> on single selects, the first element is assumed to be selected by the browser. To take advantage of the default text support, you will need to include a blank option as the first element of your select list.</p>
1234+
<p><strong>Note:</strong> on single selects, the first element is assumed to be selected by the browser. To take advantage of the default text support, you will need to include a blank option as the first element of your select list - or include an element with the same label as the placeholder and set the <code class="language-javascript">hidden</code>, <code class="language-javascript">selected</code> and <code class="language-javascript">disabled</code> attributes on it for a more graceful degradation on devices Chosen does not support:</p>
1235+
<pre><code class="language-markup">&lt;option value="" selected disabled hidden&gt;Choose a country...&lt;/option&gt;</code></pre>
12351236
</div>
12361237

12371238
<h2><a name="no-results-text-support" class="anchor" href="#no-results-text-support">No Results Text Support</a></h2>

public/index.proto.html

+8-7
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ <h2><a name="standard-select" class="anchor" href="#standard-select">Standard Se
3131
<div>
3232
<em>Turns This</em>
3333
<select data-placeholder="Choose a Country..." class="select" tabindex="1">
34-
<option value=""></option>
34+
<option value="" selected disabled hidden>Choose a Country...</option>
3535
<option value="United States">United States</option>
3636
<option value="United Kingdom">United Kingdom</option>
3737
<option value="Afghanistan">Afghanistan</option>
@@ -288,7 +288,7 @@ <h2><a name="standard-select" class="anchor" href="#standard-select">Standard Se
288288
<div>
289289
<em>Into This</em>
290290
<select data-placeholder="Choose a Country..." class="chosen-select" tabindex="2">
291-
<option value=""></option>
291+
<option value="" selected disabled hidden>Choose a Country...</option>
292292
<option value="United States">United States</option>
293293
<option value="United Kingdom">United Kingdom</option>
294294
<option value="Afghanistan">Afghanistan</option>
@@ -1174,16 +1174,16 @@ <h2><a name="optgroup-support" class="anchor" href="#optgroup-support">&lt;optgr
11741174
</div>
11751175
</div>
11761176

1177-
<h2><a name="selected-and-disabled-support" class="anchor" href="#selected-and-disabled-support">Selected and Disabled Support</a></h2>
1177+
<h2><a name="selected-and-disabled-support" class="anchor" href="#selected-and-disabled-support">Selected, Disabled and Hidden Support</a></h2>
11781178
<div class="side-by-side clearfix">
1179-
<p>Chosen automatically highlights selected options and removes disabled options.</p>
1179+
<p>Chosen automatically highlights selected options and removes disabled and / or hidden options.</p>
11801180
<div>
11811181
<em>Single Select</em>
11821182
<select data-placeholder="Your Favorite Type of Bear" class="chosen-select" tabindex="7">
11831183
<option value=""></option>
11841184
<option>American Black Bear</option>
11851185
<option>Asiatic Black Bear</option>
1186-
<option>Brown Bear</option>
1186+
<option hidden>Brown Bear</option>
11871187
<option>Giant Panda</option>
11881188
<option selected>Sloth Bear</option>
11891189
<option disabled>Sun Bear</option>
@@ -1197,7 +1197,7 @@ <h2><a name="selected-and-disabled-support" class="anchor" href="#selected-and-d
11971197
<option value=""></option>
11981198
<option>American Black Bear</option>
11991199
<option>Asiatic Black Bear</option>
1200-
<option>Brown Bear</option>
1200+
<option hidden>Brown Bear</option>
12011201
<option>Giant Panda</option>
12021202
<option selected>Sloth Bear</option>
12031203
<option disabled>Sun Bear</option>
@@ -1232,7 +1232,8 @@ <h2><a name="default-text-support" class="anchor" href="#default-text-support">D
12321232
<div class="side-by-side clearfix">
12331233
<p>Chosen automatically sets the default field text ("Choose a country...") by reading the select element's data-placeholder value. If no data-placeholder value is present, it will default to "Select an Option" or "Select Some Options" depending on whether the select is single or multiple. You can change these elements in the plugin js file as you see fit.</p>
12341234
<pre><code class="language-markup">&lt;select <strong>data-placeholder="Choose a country..."</strong> multiple class="chosen-select"&gt;</code></pre>
1235-
<p><strong>Note:</strong> on single selects, the first element is assumed to be selected by the browser. To take advantage of the default text support, you will need to include a blank option as the first element of your select list.</p>
1235+
<p><strong>Note:</strong> on single selects, the first element is assumed to be selected by the browser. To take advantage of the default text support, you will need to include a blank option as the first element of your select list - or include an element with the same label as the placeholder and set the <code class="language-javascript">hidden</code>, <code class="language-javascript">selected</code> and <code class="language-javascript">disabled</code> attributes on it for a more graceful degradation on devices Chosen does not support:</p>
1236+
<pre><code class="language-markup">&lt;option value="" selected disabled hidden&gt;Choose a country...&lt;/option&gt;</code></pre>
12361237
</div>
12371238

12381239
<h2><a name="no-results-text-support" class="anchor" href="#no-results-text-support">No Results Text Support</a></h2>

public/options.html

+3-2
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ <h3>Example:</h3>
167167
&lt;option value="1"&gt;Option 1&lt;/option&gt;
168168
&lt;option value="2" selected&gt;Option 2&lt;/option&gt;
169169
&lt;option value="3" disabled&gt;Option 3&lt;/option&gt;
170+
&lt;option value="4" hidden&gt;Option 4&lt;/option&gt;
170171
&lt;/select&gt;</code>
171172
</pre>
172173

@@ -186,8 +187,8 @@ <h3>Example:</h3>
186187
<td>The attribute <code class="language-html">multiple</code> on your select box dictates whether Chosen will render a multiple or single select.</td>
187188
</tr>
188189
<tr>
189-
<td>selected, disabled</td>
190-
<td>Chosen automatically highlights selected options and disables disabled options.</td>
190+
<td>selected, disabled, hidden</td>
191+
<td>Chosen automatically highlights selected options, hides hidden options and disables disabled options.</td>
191192
</tr>
192193
</table>
193194

spec/jquery/basic.spec.coffee

+34
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,37 @@ describe "Basic setup", ->
8585

8686
container = div.find(".chosen-container")
8787
expect(container.hasClass("chosen-disabled")).toBe true
88+
89+
it "it should not render hidden options", ->
90+
tmpl = "
91+
<select data-placeholder='Choose a Country...'>
92+
<option value='' hidden>Choose a Country</option>
93+
<option value='United States'>United States</option>
94+
</select>
95+
"
96+
div = $("<div>").html(tmpl)
97+
select = div.find("select")
98+
select.chosen()
99+
container = div.find(".chosen-container")
100+
container.trigger("mousedown") # open the drop
101+
expect(container.find(".active-result").length).toBe 1
102+
103+
104+
it "it should not render hidden optgroups", ->
105+
tmpl = "
106+
<select>
107+
<optgroup label='Not shown' hidden>
108+
<option value='Item1'>Item1</option>
109+
</optgroup>
110+
<optgroup label='Shown'>
111+
<option value='Item2'>Item2</option>
112+
</optgroup>
113+
</select>
114+
"
115+
div = $("<div>").html(tmpl)
116+
select = div.find("select")
117+
select.chosen()
118+
container = div.find(".chosen-container")
119+
container.trigger("mousedown") # open the drop
120+
expect(container.find(".group-result").length).toBe 1
121+
expect(container.find(".active-result").length).toBe 1

spec/proto/basic.spec.coffee

+39
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,42 @@ describe "Basic setup", ->
9999

100100
container = div.down(".chosen-container")
101101
expect(container.hasClassName("chosen-disabled")).toBe true
102+
103+
it "it should not render hidden options", ->
104+
tmpl = "
105+
<select data-placeholder='Choose a Country...'>
106+
<option value='' hidden>Choose a Country</option>
107+
<option value='United States'>United States</option>
108+
</select>
109+
"
110+
div = new Element("div")
111+
document.body.insert(div)
112+
div.update(tmpl)
113+
select = div.down("select")
114+
expect(select).toBeDefined()
115+
new Chosen(select)
116+
container = div.down(".chosen-container")
117+
simulant.fire(container, "mousedown") # open the drop
118+
expect(container.select(".active-result").length).toBe 1
119+
120+
it "it should not render hidden optgroups", ->
121+
tmpl = "
122+
<select>
123+
<optgroup label='Not shown' hidden>
124+
<option value='Item1'>Item1</option>
125+
</optgroup>
126+
<optgroup label='Shown'>
127+
<option value='Item2'>Item2</option>
128+
</optgroup>
129+
</select>
130+
"
131+
div = new Element("div")
132+
document.body.insert(div)
133+
div.update(tmpl)
134+
select = div.down("select")
135+
expect(select).toBeDefined()
136+
new Chosen(select)
137+
container = div.down(".chosen-container")
138+
simulant.fire(container, "mousedown") # open the drop
139+
expect(container.select(".group-result").length).toBe 1
140+
expect(container.select(".active-result").length).toBe 1

0 commit comments

Comments
 (0)