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

fix(legacy): Multiselect do not clear input on separator keydown, fix pasting #9345

Merged
merged 1 commit into from
Oct 4, 2024
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<tui-multi-select
placeholder="Ignored text"
[editable]="false"
[formControl]="control"
[tagValidator]="tagValidator"
[tuiTextfieldLabelOutside]="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
TuiMapper,
TuiStringHandler,
} from '@taiga-ui/cdk/types';
import {tuiGetClipboardDataText} from '@taiga-ui/cdk/utils/dom';
import {tuiIsNativeFocused} from '@taiga-ui/cdk/utils/focus';
import {tuiArrayToggle, tuiIsString, tuiPure} from '@taiga-ui/cdk/utils/miscellaneous';
import type {
Expand All @@ -39,7 +40,10 @@ import {
TuiStringifiableItem,
} from '@taiga-ui/legacy/classes';
import {TUI_ARROW_MODE} from '@taiga-ui/legacy/components/arrow';
import {TuiInputTagComponent} from '@taiga-ui/legacy/components/input-tag';
import {
TUI_INPUT_TAG_OPTIONS,
TuiInputTagComponent,
} from '@taiga-ui/legacy/components/input-tag';
import {
TEXTFIELD_CONTROLLER_PROVIDER,
TUI_TEXTFIELD_WATCHED_CONTROLLER,
Expand Down Expand Up @@ -91,6 +95,7 @@ export class TuiMultiSelectComponent<T>
private readonly arrowMode = inject(TUI_ARROW_MODE);
private readonly itemsHandlers = inject<TuiItemsHandlers<T>>(TUI_ITEMS_HANDLERS);
private readonly options = inject<TuiMultiSelectOptions<T>>(TUI_MULTI_SELECT_OPTIONS);
private readonly inputTagOptions = inject(TUI_INPUT_TAG_OPTIONS);

@ContentChild(TuiDataListDirective, {read: TemplateRef})
protected readonly datalist: PolymorpheusContent<TuiContext<TuiActiveZone>>;
Expand Down Expand Up @@ -249,6 +254,31 @@ export class TuiMultiSelectComponent<T>
this.updateSearch(null);
}

protected onKeyDown(event: KeyboardEvent): void {
if (event.key === this.inputTagOptions.separator) {
this.onEnter(event);
}
}

protected onPaste(event: Event): void {
const pasted = tuiGetClipboardDataText(event as ClipboardEvent);
const tags = pasted
.split(this.inputTagOptions.separator)
.map((tag) => tag.trim());
const options = this.accessor?.getOptions() ?? [];
const separator = tuiIsString(this.inputTagOptions.separator)
? this.inputTagOptions.separator
: ',';

const matches =
options?.filter((option) => tags.includes(this.stringify(option))) ?? [];
const matchingStrings = matches.map((v) => String(v));
const invalid = tags.filter((value) => !matchingStrings.includes(value));

this.value = this.filterValue([...this.value, ...matches]);
this.updateSearch(invalid.length ? invalid.join(separator) : null);
}

protected onClick({nativeFocusableElement}: TuiInputTagComponent): void {
if (
this.interactive &&
Expand All @@ -267,6 +297,15 @@ export class TuiMultiSelectComponent<T>
this.updateFocused(active);
}

private filterValue(value: T[]): T[] {
const seen = new Set();

return value
.reverse()
.filter((item) => item && !seen.has(item) && seen.add(item))
.reverse();
}

private updateSearch(search: string | null): void {
if (this.search === search) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
(click.prevent)="onClick(inputTag)"
(keydown.enter)="onEnter($event)"
(keydown.space)="onSpace($event)"
(keydown)="onKeyDown($event)"
(ngModelChange)="onInput($event)"
(paste.capture.stop.prevent)="onPaste($event)"
(searchChange)="onSearch($event)"
>
<ng-content />
Expand Down
Loading