Skip to content

Allow moving the input field to add tags at different position #113

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
72 changes: 64 additions & 8 deletions src/components/InputTag.vue
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ export default {
data() {
return {
newTag: "",
innerTags: [...this.value],
innerTags: [...this.value].map(v => {
return { id: this.getUniqueId(), text: v };
}),
isInputActive: false
};
},
Expand All @@ -78,11 +80,21 @@ export default {

watch: {
value() {
this.innerTags = [...this.value];
this.innerTags = [...this.value].map(v => {
return { id: this.getUniqueId(), text: v };
});
}
},

methods: {
getUniqueId() {
return (
Math.random()
.toString(36)
.substring(2) + Date.now().toString(36)
);
},

focusNewTag() {
if (this.readOnly || !this.$el.querySelector(".new-tag")) {
return;
Expand Down Expand Up @@ -122,9 +134,14 @@ export default {
if (
tag &&
isValid &&
(this.allowDuplicates || this.innerTags.indexOf(tag) === -1)
(this.allowDuplicates ||
this.innerTags.map(el => el.text).indexOf(tag) === -1)
) {
this.innerTags.push(tag);
const position = [].indexOf.call(
this.$el.childNodes,
this.$refs.inputtag
);
this.innerTags.splice(position, 0, { id: this.getUniqueId(), text: tag });
this.newTag = "";
this.tagChange();

Expand Down Expand Up @@ -171,9 +188,44 @@ export default {
this.tagChange();
},

moveInputHome() {
const inputtag = this.$refs.inputtag;
if (inputtag.previousElementSibling) {
this.$el.prepend(inputtag);
this.focusNewTag();
}
},

moveInputLeft() {
const inputtag = this.$refs.inputtag;
if (inputtag.previousElementSibling && inputtag.selectionStart == 0) {
this.$el.insertBefore(inputtag, inputtag.previousElementSibling);
this.focusNewTag();
}
},

moveInputRight() {
const inputtag = this.$refs.inputtag;
if (
inputtag.nextElementSibling &&
inputtag.selectionEnd == inputtag.textLength
) {
this.$el.insertBefore(inputtag.nextElementSibling, inputtag);
this.focusNewTag();
}
},

moveInputEnd() {
const inputtag = this.$refs.inputtag;
if (inputtag.nextElementSibling) {
this.$el.appendChild(inputtag);
this.focusNewTag();
}
},

tagChange() {
this.$emit("update:tags", this.innerTags);
this.$emit("input", this.innerTags);
this.$emit("update:tags", this.innerTags.map(el => el.text));
this.$emit("input", this.innerTags.map(el => el.text));
}
}
};
Expand All @@ -188,8 +240,8 @@ export default {
}"
class="vue-input-tag-wrapper"
>
<span v-for="(tag, index) in innerTags" :key="index" class="input-tag">
<span>{{ tag }}</span>
<span v-for="(tag, index) in innerTags" :key="tag.id" class="input-tag">
<span>{{ tag.text }}</span>
<a v-if="!readOnly" @click.prevent.stop="remove(index)" class="remove">
<slot name="remove-icon" />
</a>
Expand All @@ -201,6 +253,10 @@ export default {
type = "text"
v-model = "newTag"
v-on:keydown.delete.stop = "removeLastTag"
v-on:keydown.home = "moveInputHome"
v-on:keydown.left = "moveInputLeft"
v-on:keydown.right = "moveInputRight"
v-on:keydown.end = "moveInputEnd"
v-on:keydown = "addNew"
v-on:blur = "handleInputBlur"
v-on:focus = "handleInputFocus"
Expand Down
45 changes: 32 additions & 13 deletions tests/unit/InputTag.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ describe("InputTag.vue", () => {
});

it("should have a 'tag 1'", () => {
expect(wrapper.vm.innerTags[0]).toEqual("tag 1");
expect(wrapper.vm.innerTags[0].text).toEqual("tag 1");
});

it("should have a 'tag 2'", () => {
expect(wrapper.vm.innerTags[1]).toEqual("tag 2");
expect(wrapper.vm.innerTags[1].text).toEqual("tag 2");
});

it("should reset the new tag", () => {
Expand Down Expand Up @@ -70,11 +70,11 @@ describe("InputTag.vue", () => {
});

it("should have a 'tag 1'", () => {
expect(wrapper.vm.innerTags[0]).toEqual("tag 1");
expect(wrapper.vm.innerTags[0].text).toEqual("tag 1");
});

it("should have a 'tag 3'", () => {
expect(wrapper.vm.innerTags[1]).toEqual("tag 3");
expect(wrapper.vm.innerTags[1].text).toEqual("tag 3");
});
});

Expand All @@ -93,11 +93,11 @@ describe("InputTag.vue", () => {
});

it("should have a 'tag 1'", () => {
expect(wrapper.vm.innerTags[0]).toEqual("tag 1");
expect(wrapper.vm.innerTags[0].text).toEqual("tag 1");
});

it("should have a 'tag 2'", () => {
expect(wrapper.vm.innerTags[1]).toEqual("tag 2");
expect(wrapper.vm.innerTags[1].text).toEqual("tag 2");
});
});

Expand All @@ -117,7 +117,7 @@ describe("InputTag.vue", () => {
});

it("should have a 'tag 1'", () => {
expect(wrapper.vm.innerTags[2]).toEqual("tag 1");
expect(wrapper.vm.innerTags[2].text).toEqual("tag 1");
});
});

Expand Down Expand Up @@ -186,7 +186,7 @@ describe("InputTag.vue", () => {
});

it("should have a tag 'foo'", () => {
expect(wrapper.vm.innerTags[0]).toEqual("foo");
expect(wrapper.vm.innerTags[0].text).toEqual("foo");
});
});

Expand All @@ -208,7 +208,7 @@ describe("InputTag.vue", () => {
});

it("should have a tag '123'", () => {
expect(wrapper.vm.innerTags[0]).toEqual("123");
expect(wrapper.vm.innerTags[0].text).toEqual("123");
});
});

Expand All @@ -230,7 +230,7 @@ describe("InputTag.vue", () => {
});

it("should have a tag 'mati@tucci.me'", () => {
expect(wrapper.vm.innerTags[0]).toEqual("mati@tucci.me");
expect(wrapper.vm.innerTags[0].text).toEqual("mati@tucci.me");
});
});

Expand All @@ -252,7 +252,7 @@ describe("InputTag.vue", () => {
});

it("should have a tag 'https://tucci.me'", () => {
expect(wrapper.vm.innerTags[0]).toEqual("https://tucci.me");
expect(wrapper.vm.innerTags[0].text).toEqual("https://tucci.me");
});
});

Expand All @@ -274,7 +274,7 @@ describe("InputTag.vue", () => {
});

it("should have a tag '2002-04-03'", () => {
expect(wrapper.vm.innerTags[0]).toEqual("2002-04-03");
expect(wrapper.vm.innerTags[0].text).toEqual("2002-04-03");
});
});

Expand All @@ -288,7 +288,7 @@ describe("InputTag.vue", () => {
});

it("should have an uppercase tag", () => {
expect(wrapper.vm.innerTags[0]).toEqual("NEW TAG");
expect(wrapper.vm.innerTags[0].text).toEqual("NEW TAG");
});
});
});
Expand Down Expand Up @@ -335,4 +335,23 @@ describe("InputTag.vue", () => {
);
});
});

describe("move input box", () => {
beforeEach(async () => {
await addTag(wrapper, "tag B");
await addTag(wrapper, "tag D");
await addTag(wrapper, "tag F");
});

// FIXME events in "it" don't work
(false ? it : it.skip)("should add a tag before the last one", () => {
const input = wrapper.find("input.new-tag");
input.trigger("focus");
input.trigger("keydown.left");
input.trigger("keydown", {key: 'E'});
input.trigger("keydown.enter");
expect(wrapper.findAll(".input-tag").length).toEqual(4);
expect(wrapper.vm.innerTags.length).toEqual(4);
});
});
});