-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added decorators and interfaces to replace AbstractFdNgxClass (#…
…1770) * upstream synced with fork * added decorators and interfaces instead of abstract class * decorators and interfaces added replacing abstract class * small improvements * added changes acording to comments * Update NEW_COMPONENT.md * Update NEW_COMPONENT.md * Update NEW_COMPONENT.md * Update README.md * changelod sync with upstream * fixing pr comments and unit tests
- Loading branch information
1 parent
ad56ab9
commit d5893eb
Showing
10 changed files
with
166 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export type Hash<valueType> = { | ||
[key in string | number]: valueType; | ||
}; |
23 changes: 23 additions & 0 deletions
23
libs/core/src/lib/utils/decorators/apply-css-class.decorator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { ELEMENT_REF_EXCEPTION } from '../public_api'; | ||
|
||
/** | ||
* Method decorator to apply css class to a component through native element | ||
* decorator will store original method in variable and wrap it with custom one | ||
* component has to implement CssClassBuilder interface | ||
* more info abour method decorator: https://www.typescriptlang.org/docs/handbook/decorators.html#method-decorators | ||
* @param target a component | ||
* @param propertyKey name of the method | ||
* @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 _class = originalMethod.apply(this); | ||
|
||
(this.elementRef().nativeElement as HTMLElement).classList.value = `${_class} ${this.class}` | ||
|
||
return _class; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
libs/core/src/lib/utils/decorators/apply-css-style.decorator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Hash, ELEMENT_REF_EXCEPTION } from '../public_api'; | ||
|
||
/** | ||
* Method decorator to apply css styles to a component through native element | ||
* decorator will store original method in variable and wrap it with custom one | ||
* component has to implement HasElementRef or CssStyleBuilder interface | ||
* more info abour method decorator: https://www.typescriptlang.org/docs/handbook/decorators.html#method-decorators | ||
* @param target a component | ||
* @param propertyKey name of the method | ||
* @param descriptor method | ||
*/ | ||
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; } | ||
|
||
const _styles: Hash<number | string> = originalMethod.apply(this); | ||
|
||
Object.keys(_styles).forEach(key => { | ||
(this.elementRef().nativeElement as HTMLElement).style[key] = _styles[key]; | ||
}); | ||
|
||
return _styles; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
libs/core/src/lib/utils/interfaces/css-class-builder.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { HasElementRef } from '../public_api'; | ||
|
||
export interface CssClassBuilder extends HasElementRef { | ||
class: string | ||
buildComponentCssClass(): string; | ||
} |
5 changes: 5 additions & 0 deletions
5
libs/core/src/lib/utils/interfaces/css-style-builder.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { HasElementRef, Hash } from '../public_api'; | ||
|
||
export interface CssStyleBuilder extends HasElementRef { | ||
buildComponentCssStyle(): Hash<number | string>; | ||
} |
7 changes: 7 additions & 0 deletions
7
libs/core/src/lib/utils/interfaces/has-element-ref.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { ElementRef } from '@angular/core'; | ||
|
||
export interface HasElementRef { | ||
elementRef(): ElementRef; | ||
} | ||
|
||
export const ELEMENT_REF_EXCEPTION = 'HasElementRef interface has to be implemented'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters