Skip to content

added patch to recognize some delimiter characters for soft keyboards #156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion example/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
/>
<p>Output:</p>
<pre><code>{JSON.stringify(this.state.tags, null, 2)}</code></pre>
</React.Fragment>
Expand Down
55 changes: 48 additions & 7 deletions lib/ReactTags.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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
Expand Down Expand Up @@ -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) => {
Expand Down