Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the placeholder attribute instead of faking it with value. #2904

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions coffee/chosen.jquery.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,18 @@ class Chosen extends AbstractChosen
if @form_field_label.length > 0
@form_field_label.on 'click.chosen', this.label_click_handler

set_search_field_placeholder: ->
if @is_multiple and this.choices_count() < 1
@search_field.attr('placeholder', @default_text)
else
@search_field.attr('placeholder', '')

show_search_field_default: ->
@search_field.val('')
do @set_search_field_placeholder
if @is_multiple and this.choices_count() < 1 and not @active_field
@search_field.val(@default_text)
@search_field.addClass "default"
else
@search_field.val("")
@search_field.removeClass "default"

search_results_mouseup: (evt) ->
Expand Down Expand Up @@ -329,6 +335,7 @@ class Chosen extends AbstractChosen

link.parents('li').first().remove()

do @set_search_field_placeholder
this.search_field_scale()

results_reset: ->
Expand Down Expand Up @@ -494,7 +501,7 @@ class Chosen extends AbstractChosen
style_block[style] = @search_field.css(style)

div = $('<div />').css(style_block)
div.text this.get_search_field_value()
div.text @get_search_field_value() || @search_field.attr('placeholder')
$('body').append div

width = div.width() + 25
Expand Down
13 changes: 10 additions & 3 deletions coffee/chosen.proto.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,18 @@ class @Chosen extends AbstractChosen
if @form_field_label?
@form_field_label.observe "click", this.label_click_handler

set_search_field_placeholder: ->
if @is_multiple and this.choices_count() < 1
@search_field.placeholder = @default_text
else
@search_field.placeholder = ''

show_search_field_default: ->
@search_field.value = ""
do @set_search_field_placeholder
if @is_multiple and this.choices_count() < 1 and not @active_field
@search_field.value = @default_text
@search_field.addClassName "default"
else
@search_field.value = ""
@search_field.removeClassName "default"

search_results_mouseup: (evt) ->
Expand Down Expand Up @@ -321,6 +327,7 @@ class @Chosen extends AbstractChosen

link.up('li').remove()

do @set_search_field_placeholder
this.search_field_scale()

results_reset: ->
Expand Down Expand Up @@ -491,7 +498,7 @@ class @Chosen extends AbstractChosen
for style in styles
style_block[style] = @search_field.getStyle(style)

div = new Element('div').update(this.escape_html(this.get_search_field_value()))
div = new Element('div').update(this.escape_html(this.get_search_field_value() || @search_field.placeholder))
# CSP without 'unsafe-inline' doesn't allow setting the style attribute directly
div.setStyle(style_block)
document.body.appendChild(div)
Expand Down
2 changes: 1 addition & 1 deletion coffee/lib/abstract-chosen.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ class AbstractChosen
"""
<ul class="chosen-choices">
<li class="search-field">
<input class="chosen-search-input" type="text" autocomplete="off" value="#{@default_text}" />
<input class="chosen-search-input" type="text" autocomplete="off" placeholder="#{@default_text}" />
</li>
</ul>
<div class="chosen-drop">
Expand Down
4 changes: 0 additions & 4 deletions sass/chosen.scss
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ $chosen-sprite-retina: url('[email protected]') !default;
border: 0 !important;
background: transparent !important;
box-shadow: none;
color: #999;
font-size: 100%;
font-family: sans-serif;
line-height: normal;
Expand Down Expand Up @@ -318,9 +317,6 @@ $chosen-sprite-retina: url('[email protected]') !default;
.chosen-choices {
border: 1px solid #5897fb;
box-shadow: 0 0 5px rgba(#000,.3);
li.search-field input[type="text"] {
color: #222 !important;
}
}
}
/* @end */
Expand Down