diff --git a/projects/legacy/components/input-tag/input-tag.component.ts b/projects/legacy/components/input-tag/input-tag.component.ts index 820f2ad9fbcd..bdf65cb5a6f1 100644 --- a/projects/legacy/components/input-tag/input-tag.component.ts +++ b/projects/legacy/components/input-tag/input-tag.component.ts @@ -421,16 +421,17 @@ export class TuiInputTagComponent const array = value.split(this.separator); const tags = array .map((item) => this.clippedValue(item.trim())) - .filter((item, index, {length}) => item.length > 0 && index !== length - 1); + .filter((item) => item.length > 0); const validated = tags.filter((tag) => !this.disabledItemHandler(tag)); const invalid = tags.filter((tag) => this.disabledItemHandler(tag)); if (array.length > 1) { const search = invalid.length ? invalid.join(tuiIsString(this.separator) ? this.separator : ',') - : (array[array.length - 1]?.trim() ?? ''); + : ''; this.updateSearch(this.clippedValue(search)); + this.value = this.filterValue([...this.value, ...validated]); } else { this.updateSearch(this.clippedValue(value)); diff --git a/projects/legacy/components/input-tag/test/input-tag.component.spec.ts b/projects/legacy/components/input-tag/test/input-tag.component.spec.ts index 626e847909e4..696506202114 100644 --- a/projects/legacy/components/input-tag/test/input-tag.component.spec.ts +++ b/projects/legacy/components/input-tag/test/input-tag.component.spec.ts @@ -129,27 +129,25 @@ describe('InputTag', () => { it('adds tags separated by commas', () => { expect(component.value[1]).toBe('1234'); expect(component.value[2]).toBe('567'); - expect(component.value.length).toBe(3); - }); - - it('leaves the value after the last comma in the input field', () => { - expect(inputPO.value).toBe('89'); + expect(component.value[3]).toBe('89'); + expect(component.value.length).toBe(4); }); it("doesn't create empty tags", () => { inputPO.sendText(' , ,,,'); - expect(component.value.length).toBe(3); + expect(component.value.length).toBe(4); }); it('when exiting the field adds input as a tag', async () => { + inputPO.sendText('0'); focusStealer.focus(); fixture.detectChanges(); await fixture.whenStable(); - expect(component.value.length).toBe(4); - expect(component.value[3]).toBe('89'); + expect(component.value.length).toBe(5); + expect(component.value[4]).toBe('0'); }); it('does not add empty tags when leaving the field', () => { @@ -157,7 +155,7 @@ describe('InputTag', () => { focusStealer.focus(); fixture.detectChanges(); - expect(component.value.length).toBe(3); + expect(component.value.length).toBe(4); }); it('when adding a tag on leaving the field, the field is cleared', async () => { @@ -170,17 +168,18 @@ describe('InputTag', () => { }); it('pressing Enter on the field adds the input as a tag', () => { + inputPO.sendText(' 0'); inputPO.sendKeydown('enter'); - expect(component.value.length).toBe(4); - expect(component.value[3]).toBe('89'); + expect(component.value[4]).toBe('0'); + expect(component.value.length).toBe(5); }); it('pressing Enter on the field does not add empty tags', () => { inputPO.sendText(' '); inputPO.sendKeydown('enter'); - expect(component.value.length).toBe(3); + expect(component.value.length).toBe(4); }); it('pressing Enter on a field clears the field', async () => { @@ -197,7 +196,7 @@ describe('InputTag', () => { inputPO.sendText('Tag'); inputPO.sendKeydown('enter'); - expect(component.value.length).toBe(3); + expect(component.value.length).toBe(4); }); }); @@ -213,6 +212,18 @@ describe('InputTag', () => { expect(component.value[1]).toBe('10,5'); expect(component.value[2]).toBe('12,2'); }); + + it('adds tags last tag without separator', () => { + testComponent.separator = ';'; + inputPO.focus(); + fixture.detectChanges(); + inputPO.sendText('10,5;12,2;'); + focusStealer.focus(); + fixture.detectChanges(); + + expect(component.value[1]).toBe('10,5'); + expect(component.value[2]).toBe('12,2'); + }); }); describe('Adding tags with spaces (spaces inside tags are allowed)', () => { @@ -239,7 +250,8 @@ describe('InputTag', () => { expect(component.value[1]).toBe('1234'); expect(component.value[2]).toBe('567'); - expect(component.value.length).toBe(3); + expect(component.value[3]).toBe('89'); + expect(component.value.length).toBe(4); }); it('breaks into tags on non-breaking space', () => { @@ -247,7 +259,8 @@ describe('InputTag', () => { expect(component.value[1]).toBe('1234'); expect(component.value[2]).toBe('567'); - expect(component.value.length).toBe(3); + expect(component.value[3]).toBe('89'); + expect(component.value.length).toBe(4); }); it('splits into tags by comma', () => { @@ -255,7 +268,8 @@ describe('InputTag', () => { expect(component.value[1]).toBe('1234'); expect(component.value[2]).toBe('567'); - expect(component.value.length).toBe(3); + expect(component.value[3]).toBe('89'); + expect(component.value.length).toBe(4); }); it('when adding Space does not add tag or change value if tagValidator returned false', () => {