Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set Up #6

Open
ezzabuzaid opened this issue Aug 3, 2021 · 1 comment
Open

Set Up #6

ezzabuzaid opened this issue Aug 3, 2021 · 1 comment

Comments

@ezzabuzaid
Copy link
Owner

ezzabuzaid commented Aug 3, 2021

Our directive would be used like this

<ng-template [dynamic-component]="component" [inputs]="{}" [outputs]="{}"> </ng-template>

To complete the setup we need to make sure that

  1. outputs/inputs object corresponds to component outputs/inputs.
  2. ngOnChange runs on input change.
  3. outputs EventEmitter are auto unsubscribed from.

Types that will be used in the code

type UserOutputs = Record<string, (event: any) => void>;
type UserInputs = Record<string, any>;
type ComponentInputs = ComponentFactory<any>['inputs'];
type ComponentOutputs = ComponentFactory<any>['outputs'];

Utility function for strict mode people 😅

function assertNotNullOrUndefined<T>(value: T): asserts value is NonNullable<T> {
    if (value === null || value === undefined) {
        throw new Error(`cannot be undefined or null.`);
    }
}

The directive

@Directive({
    selector: '[dynamic-component]',
})
export class DynamicComponentDirective implements OnDestroy, OnChanges {
  @Input('dynamic-component') component!: Type<any>;
  @Input() outputs?: UserOutputs = {};
  @Input() inputs?: UserInputs = {};
  ngOnChanges(changes: SimpleChanges) { }
  ngOnDestroy() { }
}
@ezzabuzaid
Copy link
Owner Author

Our directive would be used like this

<ng-template [dynamic-component]="component" [inputs]="{}" [outputs]="{}"> </ng-template>

To complete the setup we need to make sure that

  1. outputs/inputs object corresponds to component outputs/inputs.
  2. ngOnChange runs on input change.
  3. outputs EventEmitter are auto unsubscribed from.

Types that will be used in the code

type UserOutputs = Record<string, (event: any) => void>;
type UserInputs = Record<string, any>;
type ComponentInputs = ComponentFactory<any>['inputs'];
type ComponentOutputs = ComponentFactory<any>['outputs'];
type Color = 'red' | 'blue' | 'green';

Utility function for strict mode people 😅

function assertNotNullOrUndefined<T>(value: T): asserts value is NonNullable<T> {
    if (value === null || value === undefined) {
        throw new Error(`cannot be undefined or null.`);
    }
}

The directive

@Directive({
    selector: '[dynamic-component]',
})
export class DynamicComponentDirective implements OnDestroy, OnChanges {
  @Input('dynamic-component') component!: Type<any>;
  @Input() outputs?: UserOutputs = {};
  @Input() inputs?: UserInputs = {};
  ngOnChanges(changes: SimpleChanges) { }
  ngOnDestroy() { }

  private makeComponentChanges(): Record<string, SimpleChange> {}
  private createComponent(): void {}
  private bindOutputs(): void {}
  private bindInputs(): void {}
  private validateInputs(): void {}
  private validateOutputs(): void {}
  private destroyComponent(): void {}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant