Skip to content

Commit 0c54ce3

Browse files
committed
Use KeyboardEvent.key rather than non-standard KeyboardEvent.keyCode, fixes #81
1 parent 267beba commit 0c54ce3

File tree

4 files changed

+27
-30
lines changed

4 files changed

+27
-30
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ Boolean parameter to control whether the text-input should be automatically resi
118118

119119
#### delimiters (optional)
120120

121-
Array of integers matching keyboard event `keyCode` values. When a corresponding key is pressed, the preceding string is finalised as tag. Default: `[9, 13]` (Tab and return keys).
121+
Array of keys matching keyboard event `key` values. When a corresponding key is pressed, the preceding string is finalised as tag. Default: `['Enter', 'Tab']`.
122122

123123
#### delimiterChars (optional)
124124

lib/ReactTags.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ const Input = require('./Input')
77
const Suggestions = require('./Suggestions')
88

99
const KEYS = {
10-
ENTER: 13,
11-
TAB: 9,
12-
BACKSPACE: 8,
13-
UP_ARROW: 38,
14-
DOWN_ARROW: 40
10+
ENTER: 'Enter',
11+
TAB: 'Tab',
12+
BACKSPACE: 'Backspace',
13+
UP_ARROW: 'ArrowUp',
14+
DOWN_ARROW: 'ArrowDown'
1515
}
1616

1717
const CLASS_NAMES = {
@@ -61,7 +61,7 @@ class ReactTags extends React.Component {
6161
const { delimiters, delimiterChars } = this.props
6262

6363
// when one of the terminating keys is pressed, add current query to the tags.
64-
if (delimiters.indexOf(e.keyCode) > -1 || delimiterChars.indexOf(e.key) > -1) {
64+
if (delimiters.indexOf(e.key) > -1 || delimiterChars.indexOf(e.key) > -1) {
6565
if (query || selectedIndex > -1) {
6666
e.preventDefault()
6767
}
@@ -83,11 +83,11 @@ class ReactTags extends React.Component {
8383
}
8484

8585
// when backspace key is pressed and query is blank, delete the last tag
86-
if (e.keyCode === KEYS.BACKSPACE && query.length === 0 && this.props.allowBackspace) {
86+
if (e.key === KEYS.BACKSPACE && query.length === 0 && this.props.allowBackspace) {
8787
this.deleteTag(this.props.tags.length - 1)
8888
}
8989

90-
if (e.keyCode === KEYS.UP_ARROW) {
90+
if (e.key === KEYS.UP_ARROW) {
9191
e.preventDefault()
9292

9393
// if last item, cycle to the bottom
@@ -98,7 +98,7 @@ class ReactTags extends React.Component {
9898
}
9999
}
100100

101-
if (e.keyCode === KEYS.DOWN_ARROW) {
101+
if (e.key === KEYS.DOWN_ARROW) {
102102
e.preventDefault()
103103

104104
this.setState({ selectedIndex: (selectedIndex + 1) % this.suggestions.state.options.length })
@@ -213,7 +213,7 @@ ReactTags.propTypes = {
213213
placeholder: PropTypes.string,
214214
suggestions: PropTypes.arrayOf(PropTypes.object),
215215
autoresize: PropTypes.bool,
216-
delimiters: PropTypes.arrayOf(PropTypes.number),
216+
delimiters: PropTypes.arrayOf(PropTypes.string),
217217
delimiterChars: PropTypes.arrayOf(PropTypes.string),
218218
handleDelete: PropTypes.func.isRequired,
219219
handleAddition: PropTypes.func.isRequired,

package.json

-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@
5252
"istanbul": "^0.4.4",
5353
"jasmine": "^2.4.1",
5454
"jsdom": "^11.3.0",
55-
"keycode": "^2.1.2",
56-
"prop-types": "^15.5.0",
5755
"react": "^15.5.0",
5856
"react-dom": "^15.5.0",
5957
"sinon": "^4.0.0",

spec/ReactTags.spec.js

+16-17
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
const React = require('react')
66
const ReactDOM = require('react-dom')
77
const TestUtils = require('react-dom/test-utils')
8-
const keycode = require('keycode')
98
const sinon = require('sinon')
109
const fixture = require('../example/countries')
1110
const Subject = require('../dist-es5/ReactTags')
@@ -62,7 +61,7 @@ function type (value) {
6261

6362
function key () {
6463
Array.from(arguments).forEach((value) => {
65-
TestUtils.Simulate.keyDown($('input'), { value, keyCode: keycode(value), key: value })
64+
TestUtils.Simulate.keyDown($('input'), { value, key: value })
6665
})
6766
}
6867

@@ -155,13 +154,13 @@ describe('React Tags', () => {
155154
it('can allow new, non-suggested tags to be added', () => {
156155
createInstance({ allowNew: false })
157156

158-
type(query); key('enter')
157+
type(query); key('Enter')
159158

160159
sinon.assert.notCalled(props.handleAddition)
161160

162161
createInstance({ allowNew: true })
163162

164-
type(query); key('enter')
163+
type(query); key('Enter')
165164

166165
sinon.assert.calledOnce(props.handleAddition)
167166
sinon.assert.calledWith(props.handleAddition, { name: query })
@@ -170,7 +169,7 @@ describe('React Tags', () => {
170169
it('can add new tags when a delimiter character is entered', () => {
171170
createInstance({ allowNew: true, delimiterChars: [',', ';'] })
172171

173-
type('foo,bar;baz'); key('enter')
172+
type('foo,bar;baz'); key('Enter')
174173

175174
sinon.assert.calledThrice(props.handleAddition)
176175
})
@@ -258,22 +257,22 @@ describe('React Tags', () => {
258257
const input = $('input')
259258
const results = $$('li[role="option"]')
260259

261-
key('down')
260+
key('ArrowDown')
262261

263262
expect(input.getAttribute('aria-activedescendant')).toEqual(results[0].id)
264263
expect(results[0].className).toMatch(/is-active/)
265264

266-
key('down', 'down')
265+
key('ArrowDown', 'ArrowDown')
267266

268267
expect(input.getAttribute('aria-activedescendant')).toEqual(results[2].id)
269268
expect(results[2].className).toMatch(/is-active/)
270269

271-
key('down')
270+
key('ArrowDown')
272271

273272
expect(input.getAttribute('aria-activedescendant')).toEqual(results[0].id)
274273
expect(results[0].className).toMatch(/is-active/)
275274

276-
key('up', 'up')
275+
key('ArrowUp', 'ArrowUp')
277276

278277
expect(input.getAttribute('aria-activedescendant')).toEqual(results[1].id)
279278
expect(results[1].className).toMatch(/is-active/)
@@ -290,7 +289,7 @@ describe('React Tags', () => {
290289
expect(option.matches('[aria-disabled="true"]')).toBeTruthy()
291290
})
292291

293-
key('down', 'enter')
292+
key('ArrowDown', 'Enter')
294293

295294
sinon.assert.notCalled(props.handleAddition)
296295
})
@@ -303,23 +302,23 @@ describe('React Tags', () => {
303302
})
304303

305304
it('triggers addition for the selected suggestion when a delimiter is pressed', () => {
306-
key('enter')
305+
key('Enter')
307306

308307
sinon.assert.notCalled(props.handleAddition)
309308

310-
type(query); key('down', 'down', 'enter')
309+
type(query); key('ArrowDown', 'ArrowDown', 'Enter')
311310

312311
sinon.assert.calledOnce(props.handleAddition)
313312
sinon.assert.calledWith(props.handleAddition, { id: 196, name: 'United Kingdom' })
314313
})
315314

316315
it('triggers addition for an unselected but matching suggestion when a delimiter is pressed', () => {
317-
type('united kingdom'); key('enter')
316+
type('united kingdom'); key('Enter')
318317
sinon.assert.calledWith(props.handleAddition, { id: 196, name: 'United Kingdom' })
319318
})
320319

321320
it('clears the input when an addition is triggered', () => {
322-
type(query); key('down', 'down', 'enter')
321+
type(query); key('ArrowDown', 'ArrowDown', 'Enter')
323322

324323
const input = $('input')
325324

@@ -345,14 +344,14 @@ describe('React Tags', () => {
345344
})
346345

347346
it('deletes the last selected tag when backspace is pressed and query is empty', () => {
348-
type(''); key('backspace')
347+
type(''); key('Backspace')
349348

350349
sinon.assert.calledOnce(props.handleDelete)
351350
sinon.assert.calledWith(props.handleDelete, sinon.match(instance.props.tags.length - 1))
352351
})
353352

354353
it('does not delete the last selected tag when backspace is pressed and query is not empty', () => {
355-
type('uni'); key('backspace')
354+
type('uni'); key('Backspace')
356355
sinon.assert.notCalled(props.handleDelete)
357356
})
358357

@@ -362,7 +361,7 @@ describe('React Tags', () => {
362361
allowBackspace: false
363362
})
364363

365-
type(''); key('backspace')
364+
type(''); key('Backspace')
366365

367366
sinon.assert.notCalled(props.handleDelete)
368367
})

0 commit comments

Comments
 (0)