Skip to content
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

feat(legacy): input tag converts the last element to tag after paste event #10270

Merged
Merged
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
5 changes: 3 additions & 2 deletions projects/legacy/components/input-tag/input-tag.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,35 +129,33 @@ 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', () => {
inputPO.sendText(' ');
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 () => {
Expand All @@ -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 () => {
Expand All @@ -197,7 +196,7 @@ describe('InputTag', () => {
inputPO.sendText('Tag');
inputPO.sendKeydown('enter');

expect(component.value.length).toBe(3);
expect(component.value.length).toBe(4);
});
});

Expand All @@ -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)', () => {
Expand All @@ -239,23 +250,26 @@ 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', () => {
inputPO.sendText(`1234${CHAR_NO_BREAK_SPACE}567${CHAR_NO_BREAK_SPACE}89`);

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', () => {
inputPO.sendText('1234,567,89');

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', () => {
Expand Down
Loading