Skip to content

Commit

Permalink
fix valueChanged #14
Browse files Browse the repository at this point in the history
  • Loading branch information
icecoldfire committed Nov 3, 2019
1 parent 1d05fd4 commit f9130b6
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 38 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,8 @@ Every single demo is separate component. Bellow you can find links to components
- default value
- tags

#### [Demo](https://github.com/tealpartners/ng-select2/tree/master/src/app/demos/value-changed)
- Demo with value changed output to console log


Demo forked from: https://github.com/NejcZdovc/ng2-select2-demo
4 changes: 2 additions & 2 deletions projects/ng-select2/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ng-select2",
"version": "1.2.1",
"version": "1.2.2",
"description": "Angular 8 wrapper component of jQuery select2.",
"repository": {
"type": "git",
Expand Down Expand Up @@ -28,4 +28,4 @@
"@types/select2": "^4.0.x",
"select2": "^4.0.x"
}
}
}
9 changes: 5 additions & 4 deletions projects/ng-select2/src/lib/ng-select2.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class NgSelect2Component implements AfterViewInit, OnChanges, OnDestroy,
@Input() options: Options;

// emitter when value is changed
@Output() valueChanged = new EventEmitter();
@Output() valueChanged = new EventEmitter<string | string[]>();

private element: any = undefined;
private check = false;
Expand Down Expand Up @@ -104,6 +104,7 @@ export class NgSelect2Component implements AfterViewInit, OnChanges, OnDestroy,

const newValue: string | string[] = this.value;
this.setElementValue(newValue);
this.valueChanged.emit(newValue);
this.propagateChange(newValue);
}

Expand All @@ -112,6 +113,7 @@ export class NgSelect2Component implements AfterViewInit, OnChanges, OnDestroy,
const newValue: string = changes['value'].currentValue;

this.setElementValue(newValue);
this.valueChanged.emit(newValue);
this.propagateChange(newValue);
}

Expand Down Expand Up @@ -148,8 +150,8 @@ export class NgSelect2Component implements AfterViewInit, OnChanges, OnDestroy,
// const newValue: string = (e.type === 'select2:unselect') ? '' : this.element.val();
const newValue = this.element.val();

this.valueChanged.emit(newValue);
this.propagateChange(newValue);
this.setElementValue(newValue);
});
}

Expand Down Expand Up @@ -236,11 +238,10 @@ export class NgSelect2Component implements AfterViewInit, OnChanges, OnDestroy,
}
}

propagateChange = (value: any) => { };
propagateChange = (value: string | string[]) => { };

registerOnChange(fn: any) {
this.propagateChange = fn;
this.valueChanged.subscribe(fn);
}

registerOnTouched() {
Expand Down
2 changes: 2 additions & 0 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ <h1>ng-select2 demo's</h1>
<app-multiple></app-multiple>
<hr>
<app-custom-array></app-custom-array>
<hr>
<app-value-changed></app-value-changed>
</div>
10 changes: 6 additions & 4 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';
import { NgSelect2Module } from 'ngSelect2';

import { AppComponent } from './app.component';
import { BasicComponent } from './demos/basic/basic.component';
import { OptionsComponent } from './demos/options/options.component';
import { ChangeComponent } from './demos/change/change.component';
import { CustomArrayComponent } from './demos/custom-array/custom-array.component';
import { DynamicComponent } from './demos/dynamic/dynamic.component';
import { MultipleComponent } from './demos/multiple/multiple.component';
import { OptionsComponent } from './demos/options/options.component';
import { TemplateComponent } from './demos/template/template.component';
import { ValueChangedComponent } from './demos/value-changed/value-changed.component';
import { DataService } from './services/data.service';
import { FormsModule } from '@angular/forms';
import { CustomArrayComponent } from './demos/custom-array/custom-array.component';


@NgModule({
Expand All @@ -23,7 +24,8 @@ import { CustomArrayComponent } from './demos/custom-array/custom-array.componen
DynamicComponent,
TemplateComponent,
MultipleComponent,
CustomArrayComponent
CustomArrayComponent,
ValueChangedComponent
],
imports: [
BrowserModule,
Expand Down
1 change: 1 addition & 0 deletions src/app/demos/change/change.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class ChangeComponent implements OnInit {
return this._value;
}
set value(value: string) {
console.log('set value with ' + value);
this._value = value;
}

Expand Down
28 changes: 0 additions & 28 deletions src/app/demos/template/template.component.spec.ts

This file was deleted.

Empty file.
7 changes: 7 additions & 0 deletions src/app/demos/value-changed/value-changed.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h2>8. Value changed</h2>
<ng-select2 [data]="exampleData"
[ngModel]="test"
(valueChanged)="valueChanged($event)"
(ngModelChange)="modelChanged($event)"
[width]="300">
</ng-select2>
42 changes: 42 additions & 0 deletions src/app/demos/value-changed/value-changed.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Component, OnInit } from '@angular/core';
import { Select2OptionData } from 'ngSelect2';

@Component({
selector: 'app-value-changed',
templateUrl: './value-changed.component.html',
styleUrls: ['./value-changed.component.css']
})
export class ValueChangedComponent implements OnInit {
public test = 'value-changed-1';
public exampleData: Array<Select2OptionData>;

ngOnInit() {
this.exampleData = [
{
id: 'value-changed-1',
text: 'Value changed 1'
},
{
id: 'value-changed-2',
disabled: true,
text: 'Value changed 2'
},
{
id: 'value-changed-3',
text: 'Value changed 3'
},
{
id: 'value-changed-4',
text: 'Value changed 4'
}
];
}

public valueChanged(event: string) {
console.log('value changed: ' + event);
}

public modelChanged(event: string) {
console.log('model changed: ' + event);
}
}

0 comments on commit f9130b6

Please sign in to comment.