Fixed #252 - Separator, addOnBlur and allowDuplicate for Chips
parent
c15d8f7cee
commit
a9fe3617e4
|
@ -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;
|
||||||
|
|
|
@ -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);
|
|
||||||
this.$emit('add', {
|
|
||||||
originalEvent: event,
|
|
||||||
value: values
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.$refs.input.value = '';
|
|
||||||
event.preventDefault();
|
|
||||||
break;
|
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', {
|
||||||
|
originalEvent: event,
|
||||||
|
value: value
|
||||||
|
});
|
||||||
|
this.$refs.input.value = '';
|
||||||
|
|
||||||
|
if (preventDefault) {
|
||||||
|
event.preventDefault();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
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) {
|
||||||
|
|
|
@ -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: {
|
||||||
|
|
|
@ -42,22 +42,40 @@ import Chips from 'primevue/chips';
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
<tr>
|
<tr>
|
||||||
<td>value</td>
|
<td>addOnBlur</td>
|
||||||
<td>array</td>
|
<td>boolean</td>
|
||||||
<td>null</td>
|
<td>false</td>
|
||||||
<td>Value of the component.</td>
|
<td>Whether to add an item when the input loses focus.</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>max</td>
|
<td>allowDuplicate</td>
|
||||||
<td>number</td>
|
<td>boolean</td>
|
||||||
<td>null</td>
|
<td>true</td>
|
||||||
<td>Maximum number of entries allowed.</td>
|
<td>Whether to allow duplicate values or not.</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>ariaLabelledBy</td>
|
<td>ariaLabelledBy</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>Establishes relationships between the component and label(s) where its value should be one or more element IDs.</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>max</td>
|
||||||
|
<td>number</td>
|
||||||
|
<td>null</td>
|
||||||
|
<td>Maximum number of entries allowed.</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>value</td>
|
||||||
|
<td>array</td>
|
||||||
|
<td>null</td>
|
||||||
|
<td>Value of the component.</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>separator</td>
|
||||||
|
<td>string</td>
|
||||||
|
<td>null</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';
|
||||||
<h3>Basic</h3>
|
<h3>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>
|
||||||
|
@ -170,7 +191,8 @@ export default {
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
value1: null,
|
value1: null,
|
||||||
value2: null
|
value2: null,
|
||||||
|
value3: null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue