Skip to content
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

Fixes #94 - Changes to support Android Chrome #97

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
tuning of typing vs paste inconsistency
  • Loading branch information
ajmas committed Sep 27, 2017
commit 76696cf2e021a911d07caaa599bb7aaa468ce666
19 changes: 13 additions & 6 deletions lib/ReactTags.js
Original file line number Diff line number Diff line change
@@ -86,7 +86,10 @@ class ReactTags extends React.Component {
if (query.match(regex)) {
// split the string based on the delimiterChars as a regex, being sure
// to escape chars, to prevent them being treated as special characters
const tags = query.split(regex)
// also remove any pure white-space entries
const tags = query.split(regex).filter((tag) => {
return tag.trim().length !== 0
})

// handle the case where the last character was not a delimiter, to
// avoid matching text a user was not ready to lookup
@@ -95,17 +98,21 @@ class ReactTags extends React.Component {
--maxTagIdx
}

// deal with case where we don't allow new tags
// for now just stop processing
// deal with case where we don't allow new tags, for now just stop processing
if (!this.props.allowNew) {
let lastTag = tags[tags.length - 2]
let lastTag = tags[maxTagIdx - 1]

const match = this.props.suggestions.findIndex((suggestion) => {
return suggestion.name.toLowerCase() === lastTag.toLowerCase()
return suggestion.name.toLowerCase() === lastTag.trim().toLowerCase()
})

if (match < 0) {
this.setState({ query: query.substring(0, query.length - 1) })
let toOffset = query.length - 1
// deal with difference between typing and pasting
if (delimiterChars.indexOf(query.charAt(toOffset)) < 0) {
toOffset++
}
this.setState({ query: query.substring(0, toOffset) })
return
}
}
48 changes: 48 additions & 0 deletions spec/ReactTags.spec.js
Original file line number Diff line number Diff line change
@@ -194,6 +194,54 @@ describe('React Tags', () => {
sinon.assert.calledOnce(props.handleAddition)
sinon.assert.calledWith(props.handleAddition, [{ name: 'Algeria' }, { name: 'Guinea Bissau' }])
})

it('does not process final tag on paste, if unrecognised tag', () => {
createInstance({ delimiterChars: [','], allowNew: false, suggestions: fixture })

paste('Thailand,Indonesia')

expect($('input').value).toEqual('Indonesia')
})

it('does not process final tag on paste, if unrecognised tag (white-space test)', () => {
createInstance({ delimiterChars: [','], allowNew: false, suggestions: fixture })

paste('Thailand, Algeria, Indonesia')

expect($('input').value).toEqual('Indonesia')
})

it('does not process final text on paste, if final text is not delimiter terminated', () => {
createInstance({ delimiterChars: [','], allowNew: false, suggestions: fixture })

paste('Thailand,Algeria')

expect($('input').value).toEqual('Algeria')
})

it('checks the trailing delimiter is removed on paste, when tag unrecognised', () => {
createInstance({ delimiterChars: [','], allowNew: false, suggestions: fixture })

paste('United Arab Emirates, Mars,')

expect($('input').value).toEqual('United Arab Emirates, Mars')
})

it('checks the trailing delimiter is removed on typing, when tag unrecognised', () => {
createInstance({ delimiterChars: [','], allowNew: false, suggestions: fixture })

type('Mars,')

expect($('input').value).toEqual('Mars')
})

it('checks last character not removed on paste, if not a delimiter', () => {
createInstance({ delimiterChars: [','], allowNew: false, suggestions: fixture })

paste('xxx, Thailand')

expect($('input').value).toEqual('xxx, Thailand')
})
})

describe('suggestions', () => {