SearchableSelect retains visible text to indicate that value wasn't cleared
This commit is contained in:
parent
914f2f313f
commit
b3f636a5d1
1 changed files with 23 additions and 9 deletions
|
@ -5,6 +5,7 @@ class App.SearchableSelect extends Spine.Controller
|
|||
'blur .js-input': 'onBlur'
|
||||
'focus .js-input': 'onFocus'
|
||||
'focus .js-shadow': 'onShadowFocus'
|
||||
'change .js-shadow': 'onShadowChange'
|
||||
'click .js-option': 'selectItem'
|
||||
'click .js-enter': 'navigateIn'
|
||||
'click .js-back': 'navigateOut'
|
||||
|
@ -35,15 +36,7 @@ class App.SearchableSelect extends Spine.Controller
|
|||
@render()
|
||||
|
||||
render: ->
|
||||
firstSelected = _.find @attribute.options, (option) -> option.selected
|
||||
|
||||
if firstSelected
|
||||
@attribute.valueName = firstSelected.name
|
||||
@attribute.value = firstSelected.value
|
||||
else if @attribute.unknown && @attribute.value
|
||||
@attribute.valueName = @attribute.value
|
||||
else if @hasSubmenu @attribute.options
|
||||
@attribute.valueName = @getName @attribute.value, @attribute.options
|
||||
@updateAttributeValueName()
|
||||
|
||||
@html App.view('generic/searchable_select')
|
||||
attribute: @attribute
|
||||
|
@ -68,6 +61,17 @@ class App.SearchableSelect extends Spine.Controller
|
|||
html += @renderSubmenus option.children
|
||||
html
|
||||
|
||||
updateAttributeValueName: ->
|
||||
firstSelected = _.find @attribute.options, (option) -> option.selected
|
||||
|
||||
if firstSelected
|
||||
@attribute.valueName = firstSelected.name
|
||||
@attribute.value = firstSelected.value
|
||||
else if @attribute.unknown && @attribute.value
|
||||
@attribute.valueName = @attribute.value
|
||||
else if @hasSubmenu @attribute.options
|
||||
@attribute.valueName = @getName @attribute.value, @attribute.options
|
||||
|
||||
hasSubmenu: (options) ->
|
||||
return false if !options
|
||||
for option in options
|
||||
|
@ -122,6 +126,10 @@ class App.SearchableSelect extends Spine.Controller
|
|||
$(document).off 'keydown.searchable_select'
|
||||
@isOpen = false
|
||||
|
||||
if !@input.val()
|
||||
@updateAttributeValueName()
|
||||
@input.val @attribute.valueName
|
||||
|
||||
onKeyUp: =>
|
||||
return if @input.val().trim() isnt ''
|
||||
@shadowInput.val('')
|
||||
|
@ -361,6 +369,12 @@ class App.SearchableSelect extends Spine.Controller
|
|||
onShadowFocus: ->
|
||||
@input.focus()
|
||||
|
||||
onShadowChange: ->
|
||||
value = @shadowInput.val()
|
||||
|
||||
for option in @attribute.options
|
||||
option.selected = (option.value + '') == value # makes sure option value is always a string
|
||||
|
||||
onInput: (event) =>
|
||||
@toggle() if not @isOpen
|
||||
|
||||
|
|
Loading…
Reference in a new issue