Skip to content

Add prop on component to manage the behaviour preventDefaultOnEnter #1

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 2 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
17 changes: 17 additions & 0 deletions App.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
<template>
<div class="container">
<span>Outside a Form</span>
<VueTagInput
v-model="tags"
:suggestions="suggestions"
:allow-duplicated="false"
:only-from-suggestions="false"
:quick-mode="true"
/>
<br>
<span>Inside a Form</span>
<form>
<input
type="checkbox"
v-model="preventDefaultOnEnter">Prevent default on enter
<VueTagInput
v-model="tags"
:suggestions="suggestions"
:allow-duplicated="false"
:only-from-suggestions="false"
:quick-mode="true"
:prevent-default-on-enter="preventDefaultOnEnter"
/>
</form>
</div>
</template>

Expand All @@ -22,6 +38,7 @@ export default {
tags: [],
// suggestions: ['Japan', 'Taiwan', 'Africa', 'America', 'Canada', 'Finland', 'China'],
suggestions: ['中國', '中文', '中午', '中飯'],
preventDefaultOnEnter: true,
};
},
};
Expand Down
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ An array of tag objects. Each tag need an `id` and a `text` property which is us
```js
// Recommended: Pass Object
[
{id: 1, name: 'Apple'},
{id: 2, name: 'Banana'},
{id: 1, text: 'Apple'},
{id: 2, text: 'Banana'},
...
]
// Quick: Pass String
Expand All @@ -85,8 +85,8 @@ An array of suggestions that are used as basis for showing autocomplete. Each ta
```js
// Recommended: Pass Object
[
{id: 1, name: 'Apple'},
{id: 2, name: 'Banana'},
{id: 1, text: 'Apple'},
{id: 2, text: 'Banana'},
...
]
// Quick: Pass String
Expand Down Expand Up @@ -138,7 +138,7 @@ Array of characters matching keyboard event key values. This is useful when need
Default: `false`
If set to `true`, it will be possible to add new items only from the autocomplete dropdown.

#### allowDuplicates - {Boolean}
#### allowDuplicated - {Boolean}
Default: `false`
Allows users to add duplicated tags. If it's `false`, the duplicated tag would play animation with [`errorAninmatedClass`](#errorAninmatedClass---String) to hint the user.

Expand Down Expand Up @@ -197,6 +197,10 @@ export default {
</script>
```

#### preventDefaultOnEnter - {Boolean}
Default: `false`
Prevent default when user tap ENTER to avoid the form submission.

## Events

#### add
Expand Down
10 changes: 10 additions & 0 deletions src/VueTagInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ export default {
required: false,
default: () => ({}),
},
preventDefaultOnEnter: {
type: Boolean,
required: false,
default: false,
},
},
data() {
return {
Expand Down Expand Up @@ -197,6 +202,11 @@ export default {
if (e.keyCode === KEYS.TAB || e.keyCode === KEYS.UP || e.keyCode === KEYS.DOWN) {
e.preventDefault();
}

// prevent ENTER key to keep cursor position in input
if (e.keyCode === KEYS.ENTER && this.preventDefaultOnEnter) {
e.preventDefault();
}
},
handleComposition(e) {
if (e.type === 'compositionend') {
Expand Down