Skip to content

Commit 4fa451d

Browse files
committed
improved tag-input component - will support comma separated text pasting
1 parent c66ab88 commit 4fa451d

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

src/components/api-request.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ export default class ApiRequest extends LitElement {
282282
data-param-serialize-explode = "${paramExplode}"
283283
data-array = "true"
284284
placeholder = "add-multiple ↩"
285-
value = "${Array.isArray(exampleVal) ? exampleVal.join(',') : exampleVal}"
285+
.value = "${Array.isArray(exampleVal) ? exampleVal : exampleVal.split(',')}"
286286
>
287287
</tag-input>`
288288
: paramSchema.type === 'object'
@@ -353,7 +353,7 @@ export default class ApiRequest extends LitElement {
353353
${paramSchema.type === 'array' ? '[' : ''}
354354
<a class = "${this.allowTry === 'true' ? '' : 'inactive-link'}"
355355
data-example-type="${paramSchema.type === 'array' ? paramSchema.type : 'string'}"
356-
data-example = "${paramSchema.type === 'array' ? (v.value?.join('~|~') || '') : (v.value || '')}"
356+
data-example = "${v.value && Array.isArray(v.value) ? (v.value?.join('~|~') || '') : (v.value || '')}"
357357
@click="${(e) => {
358358
const inputEl = e.target.closest('table').querySelector(`[data-pname="${param.name}"]`);
359359
if (inputEl) {
@@ -659,7 +659,7 @@ export default class ApiRequest extends LitElement {
659659
data-example = "${Array.isArray(fieldSchema.example) ? fieldSchema.example.join('~|~') : fieldSchema.example || ''}"
660660
data-array = "true"
661661
placeholder = "add-multiple &#x21a9;"
662-
value = "${Array.isArray(fieldSchema.example) ? fieldSchema.example.join(',') : fieldSchema.example}"
662+
.value = "${Array.isArray(fieldSchema.example) ? fieldSchema.example : fieldSchema.example.split(',')}"
663663
>
664664
</tag-input>
665665
`
@@ -995,14 +995,14 @@ export default class ApiRequest extends LitElement {
995995
const paramSerializeExplode = el.dataset.paramSerializeExplode;
996996
const vals = (el.value && Array.isArray(el.value)) ? el.value : [];
997997
if (paramSerializeStyle === 'spaceDelimited') {
998-
urlQueryParam.append(el.dataset.pname, vals.join(' '));
998+
urlQueryParam.append(el.dataset.pname, vals.join(' ').replace(/^\s|\s$/g, ''));
999999
} else if (paramSerializeStyle === 'pipeDelimited') {
1000-
urlQueryParam.append(el.dataset.pname, vals.join('|'));
1000+
urlQueryParam.append(el.dataset.pname, vals.join('|').replace(/^\||\|$/g, ''));
10011001
} else {
10021002
if (paramSerializeExplode === 'true') { // eslint-disable-line no-lonely-if
10031003
vals.forEach((v) => { urlQueryParam.append(el.dataset.pname, v); });
10041004
} else {
1005-
urlQueryParam.append(el.dataset.pname, vals.join(','));
1005+
urlQueryParam.append(el.dataset.pname, vals.join(',').replace(/^,|,$/g, ''));
10061006
}
10071007
}
10081008
}

src/components/tag-input.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@ import { LitElement, html, css } from 'lit-element';
33
export default class TagInput extends LitElement {
44
/* eslint-disable indent */
55
render() {
6+
let tagItemTmpl = '';
7+
if (Array.isArray(this.value)) {
8+
tagItemTmpl = html`${this.value
9+
.filter((v) => v.trim() !== '')
10+
.map((v) => html`<span class='tag'>${v}</span>`)
11+
}`;
12+
}
613
return html`
7-
<div class='tags' tabindex="0">
8-
${Array.isArray(this.value) && this.value.length > 0
9-
? html`${this.value.map((v) => html`<span class='tag'> ${v} </span>`)}`
10-
: ''
11-
}
12-
<input type="text" class='editor' @paste="${this.afterPaste}" @keydown="${this.afterKeyDown}" placeholder="${this.placeholder || ''}">
13-
</div>
14-
`;
14+
<div class='tags' tabindex="0">
15+
${tagItemTmpl}
16+
<input type="text" class='editor' @paste="${(e) => this.afterPaste(e)}" @keydown="${this.afterKeyDown}" placeholder="${this.placeholder || ''}">
17+
</div>
18+
`;
1519
}
1620
/* eslint-enable indent */
1721

@@ -25,8 +29,7 @@ export default class TagInput extends LitElement {
2529
attributeChangedCallback(name, oldVal, newVal) {
2630
if (name === 'value') {
2731
if (newVal && oldVal !== newVal) {
28-
const tmpVal = newVal.split(',').filter((v) => v.trim() !== '');
29-
this.value = tmpVal || '';
32+
this.value = newVal.split(',').filter((v) => v.trim() !== '');
3033
}
3134
}
3235
super.attributeChangedCallback(name, oldVal, newVal);
@@ -35,7 +38,8 @@ export default class TagInput extends LitElement {
3538
afterPaste(e) {
3639
const clipboardData = e.clipboardData || window.clipboardData;
3740
const pastedData = clipboardData.getData('Text');
38-
console.log(pastedData); // eslint-disable-line no-console
41+
this.value = pastedData ? pastedData.split(',').filter((v) => v.trim() !== '') : '';
42+
e.preventDefault();
3943
}
4044

4145
afterKeyDown(e) {

0 commit comments

Comments
 (0)