Skip to content

Commit

Permalink
You can select item with keyboards
Browse files Browse the repository at this point in the history
  • Loading branch information
soyjavi committed Jul 21, 2015
1 parent a9f09f6 commit 3a2f880
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 18 deletions.
39 changes: 33 additions & 6 deletions components/autocomplete/index.cjsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,17 @@ module.exports = React.createClass
@setState focus: false if Object.keys(suggestions).length is 0

onKeyPress: (event) ->
key_ascii = event.which
query = @refs.input.getValue().trim()
if event.which is 13 and query isnt ""

if @state.focus
children = @refs.suggestions.getDOMNode().children
for child, index in children when child.classList.contains "active"
child.classList.remove "active"
query = child.getAttribute "id"
break

if key_ascii is 13 and query isnt ""
for key, label of @state.suggestions when query.toLowerCase() is label.toLowerCase()
suggestion = {"#{key}": label}
break
Expand All @@ -62,22 +71,35 @@ module.exports = React.createClass
else if suggestion
@_addValue suggestion

else if @state.focus and key_ascii in [40, 38]
if key_ascii is 40
index = if index >= children.length - 1 then 0 else index + 1
else
index = if index is 0 then children.length - 1 else index - 1
children[index].classList.add "active"

onBlur: (event) ->
setTimeout (=> @setState focus: false, suggestions: {}), 300

onSelect: (event) ->
key = event.target.getAttribute "id"
@_addValue {"#{key}": @state.suggestions[key]}

onRefreshSelection: (event) ->
children = @refs.suggestions.getDOMNode().children
for child, index in children when child.classList.contains "active"
child.classList.remove "active"
break

onDelete: (event) ->
delete @state.values[event.target.getAttribute "id"]
@setState focus: false, values: @state.values
@props.onChange? @

# -- Render
render: ->
<div data-component-autocomplete={@props.type}
className={"focus" if @state.focus}>
className = "focus" if @state.focus
<div data-component-autocomplete={@props.type} className={className}>
{ <label>{@props.label}</label> if @props.label }
{
if @props.multiple
Expand All @@ -88,9 +110,14 @@ module.exports = React.createClass
}
</ul>
}
<Input {...@props} ref="input" value="" label="" onFocus={@onFocus}
onChange={@onChange} onKeyPress={@onKeyPress} onBlur={@onBlur}/>
<ul ref="suggestions" data-role="suggestions" onClick={@onSelect}>
<Input {...@props} ref="input" value="" label=""
onBlur={@onBlur}
onChange={@onChange}
onFocus={@onFocus}
onKeyDown={@onKeyPress}
/>
<ul ref="suggestions" data-role="suggestions"
onClick={@onSelect} onMouseEnter={@onRefreshSelection}>
{<li key={key} id={key}>{label}</li> for key, label of @state.suggestions}
</ul>
</div>
Expand Down
26 changes: 14 additions & 12 deletions components/autocomplete/style.styl
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

[data-component-autocomplete]
position : relative

// -- Children
> label
font-size : FONT_SIZE_TINY
color : THEME
> [data-role] > *
font-size : FONT_SIZE_SMALL
cursor : pointer
> [data-role="values"]
> *
Expand All @@ -20,25 +17,30 @@
background-color : THEME
border-radius : (SPACE / 8)
> [data-role="suggestions"]
z-index : 10
z-index : 2
position : absolute
width : 100%
height : 0
max-height : 50vh
overflow-x : hidden
overflow-y : scroll
max-height : 0
margin-top : -(SPACE)
background-color : WHITE
visibility : hidden
box-shadow : ZDEPTH_SHADOW_1
transition max-height ANIMATION_DURATION ANIMATION_EASE
opacity : 0
transform translateY(-(INPUT_HEIGHT / 2))
transition-property height, box-shadow, opacity, transform
transition-duration ANIMATION_DURATION
transition-timing-function ANIMATION_EASE
> *
padding : (SPACE / 2)
&:hover
&:hover, &.active
color : WHITE
background-color : THEME
background-color : FORM_ITEM_SELECT_BACKGROUND

// -- Styles
&.focus
> [data-role="suggestions"]
max-height : 25vh
visibility : visible
height : auto
opacity : 1
transform translateY(0%)
box-shadow ZDEPTH_SHADOW_1
5 changes: 5 additions & 0 deletions components/constants.styl
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@
COLOR = #222222
BACKGROUND = #f5f5f5
THEME = #cccccc
THEME = #e91e63
WHITE = #ffffff
PRIMARY = #e91e63

SECONDARY = #00bcd4
SUCCESS = #259b24
CANCEL = #e51c23
WARNING = #ff9800
FORM_COLOR = COLOR
FORM_BORDER_COLOR = lighten(FORM_COLOR, 75%)
FORM_BORDER_COLOR_FOCUS = THEME
FORM_ITEM_SELECT = THEME
FORM_ITEM_SELECT_BACKGROUND = lighten(THEME, 75%)

// -- Fonts
FONT_FAMILY = "Roboto", "Helvetica Neue", "Helvetica", "sans-serif"
Expand Down

0 comments on commit 3a2f880

Please sign in to comment.