Skip to content
This repository was archived by the owner on Nov 20, 2021. It is now read-only.

Commit f7b494f

Browse files
author
Thomas Levy
committed
Match on key as well as keyCode
1 parent fb217f3 commit f7b494f

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

react-tagsinput.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,8 +411,9 @@
411411
var tag = this._tag();
412412
var empty = tag === '';
413413
var keyCode = e.keyCode;
414-
var add = addKeys.indexOf(keyCode) !== -1;
415-
var remove = removeKeys.indexOf(keyCode) !== -1;
414+
var key = e.key;
415+
var add = addKeys.indexOf(keyCode) !== -1 || addKeys.indexOf(key) !== -1;
416+
var remove = removeKeys.indexOf(keyCode) !== -1 || removeKeys.indexOf(key) !== -1;
416417

417418
if (add) {
418419
var added = this.accept();

src/index.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,21 @@ class TagsInput extends React.Component {
8787

8888
static propTypes = {
8989
focusedClassName: React.PropTypes.string,
90-
addKeys: React.PropTypes.array,
90+
addKeys: React.PropTypes.arrayOf(React.Proptypes.oneOfType([
91+
React.PropTypes.number,
92+
React.PropTypes.string
93+
])),
9194
addOnBlur: React.PropTypes.bool,
9295
addOnPaste: React.PropTypes.bool,
9396
currentValue: React.PropTypes.string,
9497
inputValue: React.PropTypes.string,
9598
inputProps: React.PropTypes.object,
9699
onChange: React.PropTypes.func.isRequired,
97100
onChangeInput: React.PropTypes.func,
98-
removeKeys: React.PropTypes.array,
101+
removeKeys: React.PropTypes.arrayOf(React.Proptypes.oneOfType([
102+
React.PropTypes.number,
103+
React.PropTypes.string
104+
])),
99105
renderInput: React.PropTypes.func,
100106
renderTag: React.PropTypes.func,
101107
renderLayout: React.PropTypes.func,
@@ -297,8 +303,9 @@ class TagsInput extends React.Component {
297303
const tag = this._tag()
298304
let empty = tag === ''
299305
let keyCode = e.keyCode
300-
let add = addKeys.indexOf(keyCode) !== -1
301-
let remove = removeKeys.indexOf(keyCode) !== -1
306+
let key = e.key
307+
let add = addKeys.indexOf(keyCode) !== -1 || addKeys.indexOf(key) !== -1
308+
let remove = removeKeys.indexOf(keyCode) !== -1 || removeKeys.indexOf(key) !== -1
302309

303310
if (add) {
304311
let added = this.accept()

test/index.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ function paste(comp, value) {
6868
});
6969
}
7070

71-
function keyDown(comp, code) {
72-
TestUtils.Simulate.keyDown(comp.input(), {keyCode: code});
71+
function keyDown(comp, code, key) {
72+
TestUtils.Simulate.keyDown(comp.input(), {keyCode: code, key: key});
7373
}
7474

7575
function blur(comp) {
@@ -277,6 +277,16 @@ describe("TagsInput", () => {
277277
assert.equal(comp.tag(0), tag, "it should be the tag that was added");
278278
});
279279

280+
it("should add a tag on key `,`", () => {
281+
let comp = TestUtils.renderIntoDocument(<TestComponent addKeys={[","]} />);
282+
let tag = randstring();
283+
284+
change(comp, tag);
285+
keyDown(comp, null, ",");
286+
assert.equal(comp.len(), 1, "there should be one tag");
287+
assert.equal(comp.tag(0), tag, "it should be the tag that was added");
288+
});
289+
280290
it("should add a tag on blur, if `this.props.addOnBlur` is true", () => {
281291
let comp = TestUtils.renderIntoDocument(<TestComponent addOnBlur={true} />);
282292
let tag = randstring();
@@ -318,6 +328,16 @@ describe("TagsInput", () => {
318328
assert.equal(comp.len(), 0, "there should be no tags");
319329
});
320330

331+
it("should remove a tag on key `,`", () => {
332+
let comp = TestUtils.renderIntoDocument(<TestComponent removeKeys={[","]} />);
333+
let tag = randstring();
334+
335+
add(comp, tag);
336+
assert.equal(comp.len(), 1, "there should be one tag");
337+
keyDown(comp, null, ",");
338+
assert.equal(comp.len(), 0, "there should be no tags");
339+
});
340+
321341
it("should be unlimited tags", () => {
322342
let comp = TestUtils.renderIntoDocument(<TestComponent maxTags={-1} />);
323343
let tag = randstring();

0 commit comments

Comments
 (0)