1
1
import React from 'react'
2
2
import { bindActionCreators } from 'redux'
3
3
import { connect } from 'react-redux'
4
+ import Autosuggest from 'react-autosuggest'
4
5
5
- import { navigateTo , updateAutoComplete } from '../actions'
6
- import { getAutoCompleteSuggestions } from '../selectors'
6
+ import { navigateTo , updateAutoSuggest , setEmptyItemValue } from '../actions'
7
+ import { getEmptyItemState , getAutoSuggestSuggestions } from '../selectors'
7
8
8
9
let EmptyItem = React . createClass ( {
9
10
10
11
render ( ) {
11
12
const submitForm = event => {
12
13
event . preventDefault ( )
13
- let value = this . refs [ 'urlInput' ] . value . trim ( )
14
- this . refs [ 'urlInput' ] . value = ''
15
- if ( value ) {
16
- this . props . submitForm ( value )
14
+ let inputValue = this . props . inputValue . trim ( )
15
+ if ( inputValue ) {
16
+ this . props . submitForm ( inputValue )
17
17
}
18
18
}
19
+
20
+ const inputProps = {
21
+ value : this . props . inputValue ,
22
+ type : 'text' ,
23
+ placeholder : '.....' ,
24
+ className : 'emptyItemInput' ,
25
+ onFocus : ( ) => this . props . focus ( ) ,
26
+ onBlur : ( ) => this . props . blur ( ) ,
27
+ onChange : ( e , { newValue} ) => this . props . changed ( newValue ) ,
28
+ }
29
+ const suggestions = this . props . suggestions
30
+ const renderSuggestion = suggestion => (
31
+ < span dangerouslySetInnerHTML = { { __html : suggestion } } />
32
+ )
33
+ const getSuggestionValue = s => s
34
+ const onSuggestionSelected = ( event , { suggestionValue} ) => {
35
+ event . preventDefault ( )
36
+ this . props . submitForm ( suggestionValue )
37
+ }
19
38
return (
20
39
< div className = 'emptyItem' >
21
- < form className = 'emptyItemForm' onSubmit = { submitForm } >
22
- < input
23
- ref = 'urlInput'
24
- type = 'text'
25
- placeholder = '.....'
26
- onFocus = { ( ) => this . props . focus ( ) }
27
- onBlur = { ( ) => this . props . blur ( ) }
28
- onChange = { ( e ) => this . props . changed ( e . target . value ) }
29
- > </ input >
40
+ < form ref = 'form' className = 'emptyItemForm' onSubmit = { submitForm } >
41
+ < Autosuggest
42
+ suggestions = { suggestions }
43
+ onSuggestionsUpdateRequested = { this . props . updateAutoSuggest }
44
+ onSuggestionSelected = { onSuggestionSelected }
45
+ getSuggestionValue = { getSuggestionValue }
46
+ renderSuggestion = { renderSuggestion }
47
+ inputProps = { inputProps }
48
+ / >
30
49
</ form >
31
- < ul className = 'autoCompleteSuggestionList' >
32
- { this . props . suggestions . map ( suggestion =>
33
- < li
34
- className = 'autoCompleteSuggestion'
35
- key = { suggestion }
36
- dangerouslySetInnerHTML = { { __html : suggestion } }
37
- >
38
- </ li >
39
- ) }
40
- </ ul >
41
50
</ div >
42
51
)
43
52
} ,
@@ -51,7 +60,7 @@ let EmptyItem = React.createClass({
51
60
52
61
updateBrowserFocus ( ) {
53
62
// Make browser state reflect application state.
54
- let el = this . refs [ 'urlInput' ]
63
+ let el = this . refs [ 'form' ] . getElementsByClassName ( 'emptyItemInput' ) [ 0 ]
55
64
if ( this . props . focussed && document . activeElement !== el ) {
56
65
el . focus ( )
57
66
}
@@ -64,17 +73,28 @@ let EmptyItem = React.createClass({
64
73
65
74
66
75
function mapStateToProps ( state , { canvasItemId} ) {
67
- let suggestions = getAutoCompleteSuggestions ( state , canvasItemId )
76
+ let itemState = getEmptyItemState ( state , canvasItemId )
77
+ let inputValue = itemState !== undefined ? itemState . inputValue : ''
78
+ let suggestions = itemState !== undefined
79
+ ? getAutoSuggestSuggestions ( state , itemState . inputValueForSuggestions )
80
+ : [ ]
68
81
return {
69
82
suggestions,
83
+ inputValue
70
84
}
71
85
}
72
86
73
87
function mapDispatchToProps ( dispatch , { canvasItemId} ) {
74
- return bindActionCreators ( {
75
- changed : value => updateAutoComplete ( { itemId : canvasItemId , value} ) ,
76
- submitForm : userInput => navigateTo ( { userInput, itemId : canvasItemId } )
77
- } , dispatch )
88
+ return {
89
+ submitForm : userInput => {
90
+ dispatch ( setEmptyItemValue ( { inputValue : '' , itemId : canvasItemId } ) )
91
+ dispatch ( navigateTo ( { userInput, itemId : canvasItemId } ) )
92
+ } ,
93
+ ...bindActionCreators ( {
94
+ changed : inputValue => setEmptyItemValue ( { inputValue, itemId : canvasItemId } ) ,
95
+ updateAutoSuggest : ( ) => updateAutoSuggest ( { itemId : canvasItemId } )
96
+ } , dispatch )
97
+ }
78
98
}
79
99
80
100
export default connect ( mapStateToProps , mapDispatchToProps ) ( EmptyItem )
0 commit comments