Skip to content

Commit

Permalink
ngx-trim update
Browse files Browse the repository at this point in the history
* #7 Angular 8 compatibility issue addressed
* test case update
* rename the parameter for trim setter
  • Loading branch information
KingMario committed Sep 17, 2019
1 parent d78314d commit 5b3dd84
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
1 change: 1 addition & 0 deletions projects/ngx-trim-directive/README.md
Expand Up @@ -84,6 +84,7 @@ trimOPtion: '' | 'blur' | false = '';
6.Version History
* 1.0.7 #7 Angular 8 compatibility issue addressed
* 1.0.7 use NgControl DI to get the ValueAccessor
* 1.0.6 trim value after setValue of formControl, not initially trim value for `trim="blur"` or for ngModel bug fixed
* 1.0.5 [#5](https://github.com/KingMario/packages/issues/5) bug fixed
Expand Down
2 changes: 1 addition & 1 deletion projects/ngx-trim-directive/package.json
@@ -1,6 +1,6 @@
{
"name": "ngx-trim-directive",
"version": "1.0.7",
"version": "1.0.8",
"author": "KingMario <gcyyq@hotmail.com>",
"license": "MIT",
"bugs": {
Expand Down
12 changes: 6 additions & 6 deletions projects/ngx-trim-directive/src/lib/ngx-trim.directive.ts
Expand Up @@ -19,20 +19,20 @@ export class NgxTrimDirective implements OnInit, OnDestroy {

private _trim: '' | 'blur' | false;
@Input('trim')
public set trim (value: '' | 'blur' | false) {
if (value !== '' && value !== 'blur' && value !== false) {
console.warn(`Note: The value ${JSON.stringify(value)} is not assignable to the trim attribute.
public set trim (trimOption: '' | 'blur' | false) {
if (trimOption !== '' && trimOption !== 'blur' && trimOption !== false) {
console.warn(`Note: The value ${JSON.stringify(trimOption)} is not assignable to the trim attribute.
Only blank string (""), "blur" or false is allowed.`);

this._trim = false;
return;
}

this._trim = value;
this._trim = trimOption;

const elem = this.elementRef.nativeElement;
const eleValue = elem.value;
if (value !== false && eleValue !== eleValue.trim()) {
if (trimOption !== false && eleValue !== eleValue.trim()) {
// initially trim the value if needed
NgxTrimDirective.dispatchEvent(elem, 'blur');
}
Expand Down Expand Up @@ -100,7 +100,7 @@ export class NgxTrimDirective implements OnInit, OnDestroy {
this._writeValue = this._valueAccessor.writeValue;
this._valueAccessor.writeValue = (value) => {

const _value = value.trim();
const _value = value && value.trim();

if (this._writeValue) {
this._writeValue.call(this._valueAccessor, _value);
Expand Down
Expand Up @@ -19,6 +19,7 @@ import { NgxTrimDirective } from '../lib';

@Component({
template: `
<input type="text" #input0 [(ngModel)]="uninitializedValue" trim>
<input type="text" #input1 [(ngModel)]="value" trim>
<div [formGroup]="formA">
<input type="text" #input2 formControlName="fieldA" trim="blur">
Expand All @@ -30,6 +31,7 @@ import { NgxTrimDirective } from '../lib';
`,
})
class TestComponent {
@ViewChild('input0', { static: false }) input0: ElementRef;
@ViewChild('input1', { static: false }) input1: ElementRef;
@ViewChild('input2', { static: false }) input2: ElementRef;
@ViewChild('input3', { static: false }) input3: ElementRef;
Expand All @@ -50,6 +52,8 @@ class TestComponent {
updateOn: 'blur',
});

uninitializedValue: string;

value = ' ngxTrimDirective';
}

Expand Down

0 comments on commit 5b3dd84

Please sign in to comment.