@@ -7,6 +7,7 @@ var Token = require('./token');
77var KeyEvent = require ( '../keyevent' ) ;
88var Typeahead = require ( '../typeahead' ) ;
99var classNames = require ( 'classnames' ) ;
10+ var update = require ( 'react-addons-update' ) ;
1011
1112function _arraysAreDifferent ( array1 , array2 ) {
1213 if ( array1 . length != array2 . length ) {
@@ -56,7 +57,8 @@ var TypeaheadTokenizer = React.createClass({
5657 return {
5758 // We need to copy this to avoid incorrect sharing
5859 // of state across instances (e.g., via getDefaultProps())
59- selected : this . props . defaultSelected . slice ( 0 )
60+ selected : this . props . defaultSelected . slice ( 0 ) ,
61+ value : this . props . defaultValue
6062 } ;
6163 } ,
6264
@@ -84,7 +86,9 @@ var TypeaheadTokenizer = React.createClass({
8486 componentWillReceiveProps : function ( nextProps ) {
8587 // if we get new defaultProps, update selected
8688 if ( _arraysAreDifferent ( this . props . defaultSelected , nextProps . defaultSelected ) ) {
87- this . setState ( { selected : nextProps . defaultSelected . slice ( 0 ) } )
89+ this . setState ( {
90+ selected : nextProps . defaultSelected . slice ( 0 )
91+ } ) ;
8892 }
8993 } ,
9094
@@ -140,8 +144,7 @@ var TypeaheadTokenizer = React.createClass({
140144 var entry = this . refs . typeahead . refs . entry ;
141145 if ( entry . selectionStart == entry . selectionEnd &&
142146 entry . selectionStart == 0 ) {
143- this . _removeTokenForValue (
144- this . state . selected [ this . state . selected . length - 1 ] ) ;
147+ this . _removeTokenForValue ( this . state . selected [ this . state . selected . length - 1 ] ) ;
145148 event . preventDefault ( ) ;
146149 }
147150 } ,
@@ -152,22 +155,30 @@ var TypeaheadTokenizer = React.createClass({
152155 return ;
153156 }
154157
155- this . state . selected . splice ( index , 1 ) ;
156- this . setState ( { selected : this . state . selected } ) ;
158+ this . setState ( {
159+ selected : update ( this . state . selected , { $splice : [ [ index , 1 ] ] } )
160+ } ) ;
157161 this . props . onTokenRemove ( value ) ;
158162 return ;
159163 } ,
160164
161165 _addTokenForValue : function ( value ) {
162- if ( this . state . selected . indexOf ( value ) != - 1 ) {
166+ if ( this . state . selected . indexOf ( value ) !== - 1 ) {
163167 return ;
164168 }
165- this . state . selected . push ( value ) ;
166- this . setState ( { selected : this . state . selected } ) ;
167- this . refs . typeahead . setEntryText ( "" ) ;
169+ this . setState ( {
170+ selected : update ( this . state . selected , { $push : [ value ] } ) ,
171+ value : ""
172+ } ) ;
168173 this . props . onTokenAdd ( value ) ;
169174 } ,
170175
176+ _onChange : function ( event ) {
177+ this . setState ( {
178+ value : event . target . value
179+ } ) ;
180+ } ,
181+
171182 render : function ( ) {
172183 var classes = { } ;
173184 classes [ this . props . customClasses . typeahead ] = ! ! this . props . customClasses . typeahead ;
@@ -188,14 +199,16 @@ var TypeaheadTokenizer = React.createClass({
188199 options = { this . _getOptionsForTypeahead ( ) }
189200 defaultValue = { this . props . defaultValue }
190201 maxVisible = { this . props . maxVisible }
202+ onChange = { this . _onChange }
191203 onOptionSelected = { this . _addTokenForValue }
192204 onKeyDown = { this . _onKeyDown }
193205 onKeyUp = { this . props . onKeyUp }
194206 onFocus = { this . props . onFocus }
195207 onBlur = { this . props . onBlur }
196208 displayOption = { this . props . displayOption }
197209 defaultClassNames = { this . props . defaultClassNames }
198- filterOption = { this . props . filterOption } />
210+ filterOption = { this . props . filterOption }
211+ value = { this . state . value } />
199212 </ div >
200213 ) ;
201214 }
0 commit comments