diff --git a/example/main.js b/example/main.js index 2681e84..0cfe757 100644 --- a/example/main.js +++ b/example/main.js @@ -37,7 +37,10 @@ class App extends React.Component { tags={this.state.tags} suggestions={this.state.suggestions} handleDelete={this.handleDelete.bind(this)} - handleAddition={this.handleAddition.bind(this)} /> + handleAddition={this.handleAddition.bind(this)} + delimiterChars={['188','190','32','55','220','191']} + allowNew={true} + />

Output:

{JSON.stringify(this.state.tags, null, 2)}
diff --git a/lib/ReactTags.js b/lib/ReactTags.js index e1a7feb..61d0e6a 100644 --- a/lib/ReactTags.js +++ b/lib/ReactTags.js @@ -13,6 +13,17 @@ const KEYS = { UP_ARROW: 38, DOWN_ARROW: 40 } +const keyStrokes = { + ',': 188, + '.': 190, + ' ': 32, + '/': 55, + '\\': 220, + '?': 191 +} + +let oldData; +let code const CLASS_NAMES = { root: 'react-tags', @@ -47,7 +58,7 @@ class ReactTags extends React.Component { onBlur: this.handleBlur.bind(this), onFocus: this.handleFocus.bind(this), onInput: this.handleInput.bind(this), - onKeyDown: this.handleKeyDown.bind(this) + onKeyUp: this.handleKeyDown.bind(this) } } @@ -71,13 +82,42 @@ class ReactTags extends React.Component { const { query, selectedIndex } = this.state const { delimiters, delimiterChars } = this.props + // console.log(e.key); + // console.log(e.keyCode); + + let data = e.target.value + let d; + if (data !== oldData) { + oldData = data + d = data.length > 0 ? data.substr(data.length -1, 1) : false + // console.log(d); + code = keyStrokes[d] !== undefined ? String(keyStrokes[d]) : -1 + // console.log(code); + } + + + // when one of the terminating keys is pressed, add current query to the tags. - if (delimiters.indexOf(e.keyCode) > -1 || delimiterChars.indexOf(e.key) > -1) { - if (query || selectedIndex > -1) { - e.preventDefault() - } + if (delimiters.indexOf(e.keyCode) > -1 || delimiterChars.indexOf(e.key) > -1 || delimiterChars.indexOf(code) > -1) { + // console.log(delimiterChars); + // console.log(delimiterChars.indexOf(code)); + // console.log(d); + + let newQuery = query.split(d)[0] + // console.log(newQuery); + this.setState({ query : newQuery }, () => { + + // console.log(query); + + if (query || selectedIndex > -1) { + e.preventDefault() + } + + this.handleDelimiter() + return null + }) - this.handleDelimiter() + } // when backspace key is pressed and query is blank, delete the last tag @@ -105,7 +145,8 @@ class ReactTags extends React.Component { handleDelimiter () { const { query, selectedIndex } = this.state - + // console.log(query); + if (query.length >= this.props.minQueryLength) { // Check if the user typed in an existing suggestion. const match = this.suggestions.state.options.findIndex((suggestion) => {