diff --git a/projects/ng-select2-component/src/lib/select2-interfaces.ts b/projects/ng-select2-component/src/lib/select2-interfaces.ts index a033142..4eab256 100644 --- a/projects/ng-select2-component/src/lib/select2-interfaces.ts +++ b/projects/ng-select2-component/src/lib/select2-interfaces.ts @@ -48,6 +48,15 @@ export interface Select2UpdateEvent readonly options: Select2Option[]; } +export interface Select2AutoCreateEvent { + /** component */ + readonly component: Select2; + /** current selected value */ + readonly value: U; + /** selected option */ + readonly options: Select2Option[]; +} + export interface Select2SearchEvent { /** component */ readonly component: Select2; diff --git a/projects/ng-select2-component/src/lib/select2.component.html b/projects/ng-select2-component/src/lib/select2.component.html index 4425fa8..09cdfa6 100644 --- a/projects/ng-select2-component/src/lib/select2.component.html +++ b/projects/ng-select2-component/src/lib/select2.component.html @@ -149,7 +149,7 @@ #searchInput [id]="id + '-search-field'" [value]="searchText" - (keydown)="keyDown($event)" + (keydown)="keyDown($event, autoCreate)" (keyup)="searchUpdate($event)" (change)="prevChange($event)" class="select2-search__field" diff --git a/projects/ng-select2-component/src/lib/select2.component.ts b/projects/ng-select2-component/src/lib/select2.component.ts index 4bf0407..5fb2bec 100644 --- a/projects/ng-select2-component/src/lib/select2.component.ts +++ b/projects/ng-select2-component/src/lib/select2.component.ts @@ -31,6 +31,7 @@ import { ControlValueAccessor, FormGroupDirective, NgControl, NgForm } from '@an import { Subject } from 'rxjs'; import { + Select2AutoCreateEvent, Select2Data, Select2Group, Select2Option, @@ -101,7 +102,7 @@ export class Select2 implements ControlValueAccessor, OnInit, DoCheck, AfterView /** infinite scroll activated */ @Input({ transform: booleanAttribute }) infiniteScroll = false; - /** auto create if not existe */ + /** auto create if not exist */ @Input({ transform: booleanAttribute }) autoCreate = false; /** no template for label selection */ @@ -110,7 +111,7 @@ export class Select2 implements ControlValueAccessor, OnInit, DoCheck, AfterView /** use it for change the pattern of the filter search */ @Input() editPattern: (str: string) => string; - /** template for formating */ + /** template for formatting */ @Input() templates: TemplateRef | { [key: string]: TemplateRef }; /** the max height of the results container when opening the select */ @@ -188,6 +189,7 @@ export class Select2 implements ControlValueAccessor, OnInit, DoCheck, AfterView @Input() resetSelectedValue: any; @Output() update = new EventEmitter>(); + @Output() autoCreateItem = new EventEmitter>(); @Output() open = new EventEmitter(); @Output() close = new EventEmitter(); @Output() focus = new EventEmitter(); @@ -836,6 +838,11 @@ export class Select2 implements ControlValueAccessor, OnInit, DoCheck, AfterView const item = this.addItem(value.trim()); this.click(item); (e.target as HTMLInputElement).value = ''; + this.autoCreateItem.emit({ + value: item, + component: this, + options: Array.isArray(this.option) ? this.option : this.option ? [this.option] : null + }); } this.stopEvent(e); } diff --git a/src/app/app-examples.component.html b/src/app/app-examples.component.html index 62b6a4c..d050e37 100644 --- a/src/app/app-examples.component.html +++ b/src/app/app-examples.component.html @@ -1,7 +1,7 @@

Examples

- +

1. options in group ({{ value1 }})

@@ -376,4 +376,17 @@

31. change list ({{ value31 }})

id="selec2-31b" (update)="update('value31b', $event)" > + +

32. auto create when ({{ value30 }})

+ +
diff --git a/src/app/app.component.html b/src/app/app.component.html index 1e50ed8..7037150 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -2,7 +2,7 @@