Skip to content

Commit

Permalink
fix: null check in decorator added (#1784)
Browse files Browse the repository at this point in the history
* upstream synced with fork

* added null check for elementRef

* removed changes from new_component and readme

* changes removed from readme.md
  • Loading branch information
rengare authored and InnaAtanasova committed Jan 9, 2020
1 parent cabd353 commit 56b0914
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
15 changes: 10 additions & 5 deletions libs/core/src/lib/utils/decorators/apply-css-class.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@ import { ELEMENT_REF_EXCEPTION } from '../public_api';
* @param descriptor method
*/
export function applyCssClass(target: any, propertyKey: string, descriptor: PropertyDescriptor): void {
const originalMethod = descriptor.value
descriptor.value = function (): string {
if (!this.elementRef) { throw ELEMENT_REF_EXCEPTION; }
const originalMethod = descriptor.value;
descriptor.value = function(): string {
if (!this.elementRef) {
throw ELEMENT_REF_EXCEPTION;
}

const _class = originalMethod.apply(this);
const elementRef = this.elementRef();

(this.elementRef().nativeElement as HTMLElement).classList.value = `${_class} ${this.class}`
if (elementRef) {
(elementRef.nativeElement as HTMLElement).classList.value = `${_class} ${this.class}`;
}

return _class;
}
};
}
17 changes: 11 additions & 6 deletions libs/core/src/lib/utils/decorators/apply-css-style.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@ import { Hash, ELEMENT_REF_EXCEPTION } from '../public_api';
*/
export function applyCssStyle(target: any, propertyKey: string, descriptor: PropertyDescriptor): void {
const originalMethod = descriptor.value;
descriptor.value = function (): Hash<number | string> {
if (!this.elementRef) { throw ELEMENT_REF_EXCEPTION; }
descriptor.value = function(): Hash<number | string> {
if (!this.elementRef) {
throw ELEMENT_REF_EXCEPTION;
}

const _styles: Hash<number | string> = originalMethod.apply(this);
const elementRef = this.elementRef();

Object.keys(_styles).forEach(key => {
(this.elementRef().nativeElement as HTMLElement).style[key] = _styles[key];
});
if (elementRef) {
Object.keys(_styles).forEach(key => {
(elementRef.nativeElement as HTMLElement).style[key] = _styles[key];
});
}

return _styles;
}
};
}

0 comments on commit 56b0914

Please sign in to comment.