Skip to content

Commit

Permalink
consider disabled fieldset for disabled state (#2718)
Browse files Browse the repository at this point in the history
* consider disabled fieldset for disabled state

fixes #2389
  • Loading branch information
koenpunt authored Oct 27, 2016
1 parent bd47b88 commit 95c89d1
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 16 deletions.
20 changes: 11 additions & 9 deletions coffee/chosen.jquery.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,18 @@ class Chosen extends AbstractChosen
@form_field_jq.show()

search_field_disabled: ->
@is_disabled = @form_field_jq[0].disabled
if(@is_disabled)
@container.addClass 'chosen-disabled'
@search_field[0].disabled = true
@selected_item.unbind "focus.chosen", @activate_action if !@is_multiple
@is_disabled = @form_field.disabled || @form_field_jq.parents('fieldset').is(':disabled')

@container.toggleClass 'chosen-disabled', @is_disabled
@search_field[0].disabled = @is_disabled

unless @is_multiple
@selected_item.unbind 'focus.chosen', this.activate_field

if @is_disabled
this.close_field()
else
@container.removeClass 'chosen-disabled'
@search_field[0].disabled = false
@selected_item.bind "focus.chosen", @activate_action if !@is_multiple
else unless @is_multiple
@selected_item.bind 'focus.chosen', this.activate_field

container_mousedown: (evt) ->
return if @is_disabled
Expand Down
20 changes: 13 additions & 7 deletions coffee/chosen.proto.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,22 @@ class @Chosen extends AbstractChosen
@form_field.show()

search_field_disabled: ->
@is_disabled = @form_field.disabled
if(@is_disabled)
@is_disabled = @form_field.disabled || @form_field.up('fieldset')?.disabled || false

if @is_disabled
@container.addClassName 'chosen-disabled'
@search_field.disabled = true
@selected_item.stopObserving "focus", @activate_action if !@is_multiple
this.close_field()
else
@container.removeClassName 'chosen-disabled'
@search_field.disabled = false
@selected_item.observe "focus", @activate_action if !@is_multiple

@search_field.disabled = @is_disabled

unless @is_multiple
@selected_item.stopObserving 'focus', this.activate_field

if @is_disabled
this.close_field()
else unless @is_multiple
@selected_item.observe 'focus', this.activate_field

container_mousedown: (evt) ->
return if @is_disabled
Expand Down
21 changes: 21 additions & 0 deletions spec/jquery/basic.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,24 @@ describe "Basic setup", ->
select.chosen()
placeholder = div.find(".chosen-single > span")
expect(placeholder.text()).toBe("<None>")

describe "disabled fieldset", ->

it "should render as disabled", ->
tmpl = "
<fieldset disabled>
<select data-placeholder='Choose a Country...'>
<option value=''></option>
<option value='United States'>United States</option>
<option value='United Kingdom'>United Kingdom</option>
<option value='Afghanistan'>Afghanistan</option>
</select>
</fieldset>
"
div = $("<div>").html(tmpl)
select = div.find("select")
expect(select.size()).toBe(1)
select.chosen()

container = div.find(".chosen-container")
expect(container.hasClass("chosen-disabled")).toBe true
23 changes: 23 additions & 0 deletions spec/proto/basic.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,26 @@ describe "Basic setup", ->

placeholder = div.down(".chosen-single > span")
expect(placeholder.innerText).toBe("<None>")

describe "disabled fieldset", ->

it "should render as disabled", ->
tmpl = "
<fieldset disabled>
<select data-placeholder='Choose a Country...'>
<option value=''></option>
<option value='United States'>United States</option>
<option value='United Kingdom'>United Kingdom</option>
<option value='Afghanistan'>Afghanistan</option>
</select>
</fieldset>
"
div = new Element("div")
document.body.insert(div)
div.update(tmpl)
select = div.down("select")
expect(select).toBeDefined()
new Chosen(select)

container = div.down(".chosen-container")
expect(container.hasClassName("chosen-disabled")).toBe true

0 comments on commit 95c89d1

Please sign in to comment.