Fixed #252 - Separator, addOnBlur and allowDuplicate for Chips

pull/310/head
cagataycivici 2020-04-07 12:22:46 +03:00
parent c15d8f7cee
commit a9fe3617e4
4 changed files with 113 additions and 27 deletions

View File

@ -4,6 +4,9 @@ export declare class Chips extends Vue {
value?: any[]; value?: any[];
max?: number; max?: number;
ariaLabelledBy?: string; ariaLabelledBy?: string;
addOnBlur?: boolean;
allowDuplicate?: boolean;
separator?: string;
$emit(eventName: 'focus', event: Event): this; $emit(eventName: 'focus', event: Event): this;
$emit(eventName: 'blur', event: Event): this; $emit(eventName: 'blur', event: Event): this;
$emit(eventName: 'input', value: any[]): this; $emit(eventName: 'input', value: any[]): this;

View File

@ -8,7 +8,8 @@
</slot> </slot>
</li> </li>
<li class="p-chips-input-token"> <li class="p-chips-input-token">
<input ref="input" type="text" class="p-inputtext p-component" @focus="onFocus($event)" @blur="onBlur($event)" @keydown="onKeyDown($event)" :disabled="$attrs.disabled || maxedOut" :aria-labelledby="ariaLabelledBy"> <input ref="input" type="text" class="p-inputtext p-component" @focus="onFocus($event)" @blur="onBlur($event)"
@keydown="onKeyDown($event)" @paste="onPaste($event)" :disabled="$attrs.disabled || maxedOut" :aria-labelledby="ariaLabelledBy">
</li> </li>
</ul> </ul>
</div> </div>
@ -17,9 +18,30 @@
<script> <script>
export default { export default {
props: { props: {
value: Array, value: {
max: Number, type: Array,
ariaLabelledBy: null default: null
},
max: {
type: Number,
default: null
},
separator: {
type: String,
default: null
},
ariaLabelledBy: {
type: String,
default: null
},
addOnBlur: {
type: Boolean,
default: null
},
allowDuplicate: {
type: Boolean,
default: true
}
}, },
data() { data() {
return { return {
@ -36,6 +58,9 @@ export default {
}, },
onBlur(event) { onBlur(event) {
this.focused = false; this.focused = false;
if (this.addOnBlur) {
this.addItem(event, event.target.value, false);
}
this.$emit('blur', event); this.$emit('blur', event);
}, },
onKeyDown(event) { onKeyDown(event) {
@ -51,19 +76,51 @@ export default {
//enter //enter
case 13: case 13:
if (inputValue && inputValue.trim().length && (!this.max || this.max > this.value.length)) { if (inputValue && inputValue.trim().length && !this.maxedOut) {
let values = this.value ? [...this.value]: []; this.addItem(event, inputValue, true);
values.push(inputValue); }
this.$emit('input', values); break;
default:
if (this.separator) {
if (this.separator === ',' && event.which === 188) {
this.addItem(event, inputValue, true);
}
}
break;
}
},
onPaste(event) {
if (this.separator) {
let pastedData = (event.clipboardData || window['clipboardData']).getData('Text');
if (pastedData) {
let value = this.value || [];
let pastedValues = pastedData.split(this.separator);
pastedValues = pastedValues.filter(val => (this.allowDuplicate || value.indexOf(val) === -1));
value = [...value, ...pastedValues];
this.updateModel(event, value, true);
}
}
},
updateModel(event, value, preventDefault) {
this.$emit('input', value);
this.$emit('add', { this.$emit('add', {
originalEvent: event, originalEvent: event,
value: values value: value
}); });
}
this.$refs.input.value = ''; this.$refs.input.value = '';
if (preventDefault) {
event.preventDefault(); event.preventDefault();
break; }
},
addItem(event, item, preventDefault) {
if (item && item.trim().length) {
let value = this.value ? [...this.value]: [];
if (this.allowDuplicate || value.indexOf(item) === -1) {
value.push(item);
this.updateModel(event, value, preventDefault);
}
} }
}, },
removeItem(event, index) { removeItem(event, index) {

View File

@ -11,8 +11,11 @@
<h3 class="first">Basic</h3> <h3 class="first">Basic</h3>
<Chips v-model="value1" /> <Chips v-model="value1" />
<h3>Comma Separator</h3>
<Chips v-model="value2" separator="," />
<h3>Template</h3> <h3>Template</h3>
<Chips v-model="value2"> <Chips v-model="value3">
<template #chip="slotProps"> <template #chip="slotProps">
<div> <div>
<span>{{slotProps.value}} - (active) </span> <span>{{slotProps.value}} - (active) </span>
@ -33,7 +36,8 @@ export default {
data() { data() {
return { return {
value1: null, value1: null,
value2: null value2: null,
value3: null
} }
}, },
components: { components: {

View File

@ -42,10 +42,22 @@ import Chips from 'primevue/chips';
</thead> </thead>
<tbody> <tbody>
<tr> <tr>
<td>value</td> <td>addOnBlur</td>
<td>array</td> <td>boolean</td>
<td>false</td>
<td>Whether to add an item when the input loses focus.</td>
</tr>
<tr>
<td>allowDuplicate</td>
<td>boolean</td>
<td>true</td>
<td>Whether to allow duplicate values or not.</td>
</tr>
<tr>
<td>ariaLabelledBy</td>
<td>string</td>
<td>null</td> <td>null</td>
<td>Value of the component.</td> <td>Establishes relationships between the component and label(s) where its value should be one or more element IDs.</td>
</tr> </tr>
<tr> <tr>
<td>max</td> <td>max</td>
@ -54,10 +66,16 @@ import Chips from 'primevue/chips';
<td>Maximum number of entries allowed.</td> <td>Maximum number of entries allowed.</td>
</tr> </tr>
<tr> <tr>
<td>ariaLabelledBy</td> <td>value</td>
<td>array</td>
<td>null</td>
<td>Value of the component.</td>
</tr>
<tr>
<td>separator</td>
<td>string</td> <td>string</td>
<td>null</td> <td>null</td>
<td>Establishes relationships between the component and label(s) where its value should be one or more element IDs.</td> <td>Separator char to add an item when pressed in addition to the enter key. Currently only possible value is ","</td>
</tr> </tr>
</tbody> </tbody>
</table> </table>
@ -153,8 +171,11 @@ import Chips from 'primevue/chips';
&lt;h3&gt;Basic&lt;/h3&gt; &lt;h3&gt;Basic&lt;/h3&gt;
&lt;Chips v-model="value1" /&gt; &lt;Chips v-model="value1" /&gt;
&lt;h3&gt;Comma Separator&lt;/h3&gt;
&lt;Chips v-model="value2" separator="," /&gt;
&lt;h3&gt;Template&lt;/h3&gt; &lt;h3&gt;Template&lt;/h3&gt;
&lt;Chips v-model="value2"&gt; &lt;Chips v-model="value3"&gt;
&lt;template #chip="slotProps"&gt; &lt;template #chip="slotProps"&gt;
&lt;div&gt; &lt;div&gt;
&lt;span&gt;{{slotProps.value}} - (active) &lt;/span&gt; &lt;span&gt;{{slotProps.value}} - (active) &lt;/span&gt;
@ -170,7 +191,8 @@ export default {
data() { data() {
return { return {
value1: null, value1: null,
value2: null value2: null,
value3: null
} }
} }
} }