Skip to content

Commit

Permalink
Fire 'input' event before 'change' (proto)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcandre committed Sep 25, 2016
1 parent fdce548 commit 93f8cd8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions coffee/chosen.proto.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,7 @@ class @Chosen extends AbstractChosen
@search_field.setStyle({'width': w + 'px'})

trigger_form_field_change: ->
Chosen.trigger(@form_field, 'input')
Chosen.trigger(@form_field, 'change')

@trigger: (element, eventType) ->
Expand Down
32 changes: 32 additions & 0 deletions spec/proto/events.spec.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
describe "Events", ->
it "chosen should fire the right events", ->
tmpl = "
<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>
"

div = new Element("div")
document.body.insert(div)

div.update(tmpl)
select = div.down("select")
expect(select).toBeDefined()
new Chosen(select)

event_sequence = []
document.addEventListener 'input', -> event_sequence.push 'input'
document.addEventListener 'change', -> event_sequence.push 'change'

container = div.down(".chosen-container")
Chosen.trigger(container, "mousedown") # open the drop
expect(container.hasClassName("chosen-container-active")).toBe true

#select an item
Chosen.trigger(container.select(".active-result").last(), 'mouseup')

expect(event_sequence).toEqual ['input', 'change']
div.remove()

0 comments on commit 93f8cd8

Please sign in to comment.