Skip to content

Commit 08d44ad

Browse files
authored
Merge pull request #13 from JJJ/issue/2726
Issue/2726
2 parents dc88363 + 5b4af18 commit 08d44ad

File tree

3 files changed

+42
-37
lines changed

3 files changed

+42
-37
lines changed

coffee/chosen.jquery.coffee

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ class Chosen extends AbstractChosen
164164
this.activate_field()
165165

166166
container_mouseup: (evt) ->
167-
this.results_reset(evt) if evt.target.nodeName is "ABBR" and not @is_disabled
167+
if not @is_disabled and @allow_single_deselect and $(evt.target).hasClass('search-choice-close')
168+
this.results_reset(evt)
168169

169170
search_results_mousewheel: (evt) ->
170171
delta = evt.originalEvent.deltaY or -evt.originalEvent.wheelDelta or evt.originalEvent.detail if evt.originalEvent
@@ -337,7 +338,7 @@ class Chosen extends AbstractChosen
337338
if item.disabled
338339
choice.addClass 'search-choice-disabled'
339340
else
340-
close_link = $('<a />', { class: 'search-choice-close', 'data-option-array-index': item.array_index })
341+
close_link = $('<button />', { type: 'button', tabindex: -1, class: 'search-choice-close', 'data-option-array-index': item.array_index })
341342
close_link.on 'click.chosen', (evt) => this.choice_destroy_link_click(evt)
342343
choice.append close_link
343344
if @inherit_option_classes && item.classes
@@ -375,7 +376,7 @@ class Chosen extends AbstractChosen
375376

376377
results_reset_cleanup: ->
377378
@current_selectedIndex = @form_field.selectedIndex
378-
@selected_item.find("abbr").remove()
379+
@selected_item.find('.search-choice-close').remove()
379380

380381
result_select: (evt) ->
381382
if @result_highlight
@@ -452,8 +453,9 @@ class Chosen extends AbstractChosen
452453

453454
single_deselect_control_build: ->
454455
return unless @allow_single_deselect
455-
@selected_item.find("span").first().after "<abbr class=\"search-choice-close\"></abbr>" unless @selected_item.find("abbr").length
456-
@selected_item.addClass("chosen-single-with-deselect")
456+
unless @selected_item.find('.search-choice-close').length
457+
@selected_item.find('span').first().after '<button type="button" tabindex="-1" class="search-choice-close"></button>'
458+
@selected_item.addClass('chosen-single-with-deselect')
457459

458460
get_search_field_value: ->
459461
@search_field.val()
@@ -499,7 +501,7 @@ class Chosen extends AbstractChosen
499501

500502
keydown_backstroke: ->
501503
if @pending_backstroke
502-
this.choice_destroy @pending_backstroke.find("a").first()
504+
this.choice_destroy @pending_backstroke.find('.search-choice-close').first()
503505
this.clear_backstroke()
504506
else
505507
next_available_destroy = @search_container.siblings("li.search-choice").last()

coffee/chosen.proto.coffee

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class @Chosen extends AbstractChosen
111111

112112
if @is_multiple
113113
@search_choices.stopObserving()
114-
@container.select(".search-choice-close").each (choice) ->
114+
@container.select('.search-choice-close').each (choice) ->
115115
choice.stopObserving()
116116
else
117117
@selected_item.stopObserving()
@@ -348,7 +348,7 @@ class @Chosen extends AbstractChosen
348348
if item.disabled
349349
choice.addClassName 'search-choice-disabled'
350350
else
351-
close_link = new Element('a', { href: '#', class: 'search-choice-close', rel: item.array_index })
351+
close_link = new Element('button', { type: 'button', tabindex: -1, class: 'search-choice-close', rel: item.array_index })
352352
close_link.observe "click", (evt) => this.choice_destroy_link_click(evt)
353353
choice.insert close_link
354354
if @inherit_option_classes && item.classes
@@ -386,7 +386,7 @@ class @Chosen extends AbstractChosen
386386

387387
results_reset_cleanup: ->
388388
@current_selectedIndex = @form_field.selectedIndex
389-
deselect_trigger = @selected_item.down("abbr")
389+
deselect_trigger = @selected_item.down('.search-choice-close')
390390
deselect_trigger.remove() if(deselect_trigger)
391391

392392
result_select: (evt) ->
@@ -462,8 +462,9 @@ class @Chosen extends AbstractChosen
462462

463463
single_deselect_control_build: ->
464464
return unless @allow_single_deselect
465-
@selected_item.down("span").insert { after: "<abbr class=\"search-choice-close\"></abbr>" } unless @selected_item.down("abbr")
466-
@selected_item.addClassName("chosen-single-with-deselect")
465+
unless @selected_item.down('.search-choice-close')
466+
@selected_item.down('span').insert { after: '<button type="button" tabindex="-1" class="search-choice-close"></button>' }
467+
@selected_item.addClassName('chosen-single-with-deselect')
467468

468469
get_search_field_value: ->
469470
@search_field.value
@@ -515,7 +516,7 @@ class @Chosen extends AbstractChosen
515516

516517
keydown_backstroke: ->
517518
if @pending_backstroke
518-
this.choice_destroy @pending_backstroke.down("a")
519+
this.choice_destroy @pending_backstroke.down(".search-choice-close")
519520
this.clear_backstroke()
520521
else
521522
next_available_destroy = @search_container.siblings().last()

sass/chosen.scss

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ $chosen-sprite-retina: url('[email protected]') !default;
3131
cursor: pointer;
3232
}
3333

34-
.search-choice, .chosen-single{
34+
.search-choice,
35+
.chosen-single{
3536
.group-name{
3637
margin-right: 4px;
3738
overflow: hidden;
@@ -46,6 +47,21 @@ $chosen-sprite-retina: url('[email protected]') !default;
4647
}
4748
}
4849
}
50+
.search-choice-close {
51+
border: none;
52+
cursor: pointer;
53+
background: none;
54+
display: block;
55+
width: 12px;
56+
height: 12px;
57+
background: $chosen-sprite -42px 1px no-repeat;
58+
font-size: 1px;
59+
60+
&:hover,
61+
&:focus {
62+
background-position: -42px -10px;
63+
}
64+
}
4965
}
5066
/* @end */
5167

@@ -81,21 +97,10 @@ $chosen-sprite-retina: url('[email protected]') !default;
8197
.chosen-single-with-deselect span {
8298
margin-right: 38px;
8399
}
84-
.chosen-single abbr {
100+
.chosen-single .search-choice-close {
85101
position: absolute;
86102
top: 6px;
87103
right: 26px;
88-
display: block;
89-
width: 12px;
90-
height: 12px;
91-
background: $chosen-sprite -42px 1px no-repeat;
92-
font-size: 1px;
93-
&:hover {
94-
background-position: -42px -10px;
95-
}
96-
}
97-
&.chosen-disabled .chosen-single abbr:hover {
98-
background-position: -42px -10px;
99104
}
100105
.chosen-single div {
101106
position: absolute;
@@ -257,14 +262,6 @@ $chosen-sprite-retina: url('[email protected]') !default;
257262
position: absolute;
258263
top: 4px;
259264
right: 3px;
260-
display: block;
261-
width: 12px;
262-
height: 12px;
263-
background: $chosen-sprite -42px 1px no-repeat;
264-
font-size: 1px;
265-
&:hover {
266-
background-position: -42px -10px;
267-
}
268265
}
269266
}
270267
&.search-choice-disabled {
@@ -331,8 +328,14 @@ $chosen-sprite-retina: url('[email protected]') !default;
331328
.chosen-single {
332329
cursor: default;
333330
}
334-
.chosen-choices .search-choice .search-choice-close {
331+
.search-choice-close {
335332
cursor: default;
333+
&:hover,
334+
&:focus {
335+
outline: none;
336+
background-position: -42px 1px;
337+
cursor: default;
338+
}
336339
}
337340
}
338341
/* @end */
@@ -356,7 +359,7 @@ $chosen-sprite-retina: url('[email protected]') !default;
356359
right: auto;
357360
left: 3px;
358361
}
359-
.chosen-single abbr {
362+
.chosen-single .search-choice-close {
360363
right: auto;
361364
left: 26px;
362365
}
@@ -407,10 +410,9 @@ $chosen-sprite-retina: url('[email protected]') !default;
407410
/* @group Retina compatibility */
408411
@media only screen and (-webkit-min-device-pixel-ratio: 1.5), only screen and (min-resolution: 144dpi), only screen and (min-resolution: 1.5dppx) {
409412
.chosen-rtl .chosen-search input[type="text"],
410-
.chosen-container-single .chosen-single abbr,
411413
.chosen-container-single .chosen-single div b,
412414
.chosen-container-single .chosen-search input[type="text"],
413-
.chosen-container-multi .chosen-choices .search-choice .search-choice-close,
415+
.chosen-container .search-choice-close,
414416
.chosen-container .chosen-results-scroll-down span,
415417
.chosen-container .chosen-results-scroll-up span {
416418
background-image: $chosen-sprite-retina !important;

0 commit comments

Comments
 (0)