1- import { AfterViewInit , ComponentFactoryResolver , ComponentRef , ElementRef , Injector , TemplateRef , Type , ChangeDetectorRef , OnChanges , SimpleChanges , HostBinding , Input } from "@angular/core" ;
2- import { isReactNode } from "../renderer/react-node" ;
3- import { renderComponent , renderFunc , renderTemplate } from "../renderer/renderprop-helpers" ;
4- import { unreachable } from "../utils/types/unreachable" ;
1+ import {
2+ AfterViewInit ,
3+ ComponentFactoryResolver ,
4+ ComponentRef ,
5+ ElementRef ,
6+ Injector ,
7+ TemplateRef ,
8+ Type ,
9+ ChangeDetectorRef ,
10+ OnChanges ,
11+ SimpleChanges ,
12+ HostBinding ,
13+ Input
14+ } from '@angular/core' ;
15+ import { isReactNode } from '../renderer/react-node' ;
16+ import { renderComponent , renderFunc , renderTemplate } from '../renderer/renderprop-helpers' ;
17+ import { unreachable } from '../utils/types/unreachable' ;
518import toStyle from 'css-to-style' ;
619
720type PropMapper = ( value : any ) => [ string , any ] ;
@@ -14,9 +27,7 @@ const forbiddenAttributesAsProps: ReadonlyArray<AttributeNameAlternative> = [
1427 [ 'style' , 'rStyle' ] ,
1528] ;
1629
17- const ignoredAttributeMatchers = [
18- / ^ _ ? n g - ? .* /
19- ] ;
30+ const ignoredAttributeMatchers = [ / ^ _ ? n g - ? .* / ] ;
2031
2132const ngClassRegExp = / ^ n g - / ;
2233
@@ -27,7 +38,7 @@ export interface RenderComponentOptions<TContext extends object> {
2738}
2839
2940export type InputRendererOptions < TContext extends object > =
30- TemplateRef < TContext >
41+ | TemplateRef < TContext >
3142 | ( ( context : TContext ) => HTMLElement )
3243 | ComponentRef < TContext >
3344 | RenderComponentOptions < TContext > ;
@@ -40,17 +51,18 @@ export type JsxRenderFunc<TContext> = (context: TContext) => JSX.Element;
4051 */
4152// NOTE: TProps is not used at the moment, but a preparation for a potential future change.
4253export abstract class ReactWrapperComponent < TProps extends { } > implements AfterViewInit , OnChanges {
43-
4454 protected abstract reactNodeRef : ElementRef ;
4555
46- @Input ( ) set rClass ( value : string ) {
56+ @Input ( )
57+ set rClass ( value : string ) {
4758 if ( isReactNode ( this . reactNodeRef . nativeElement ) ) {
4859 this . reactNodeRef . nativeElement . setProperty ( 'className' , value ) ;
4960 this . changeDetectorRef . detectChanges ( ) ;
5061 }
5162 }
5263
53- @Input ( ) set rStyle ( value : string ) {
64+ @Input ( )
65+ set rStyle ( value : string ) {
5466 if ( isReactNode ( this . reactNodeRef . nativeElement ) ) {
5567 this . reactNodeRef . nativeElement . setProperty ( 'style' , toStyle ( value ) ) ;
5668 this . changeDetectorRef . detectChanges ( ) ;
@@ -62,7 +74,11 @@ export abstract class ReactWrapperComponent<TProps extends {}> implements AfterV
6274 * @param elementRef The host element.
6375 * @param setHostDisplay Whether the host's `display` should be set to the root child node's `display`. defaults to `false`
6476 */
65- constructor ( public readonly elementRef : ElementRef , private readonly changeDetectorRef : ChangeDetectorRef , private readonly setHostDisplay : boolean = false ) { }
77+ constructor (
78+ public readonly elementRef : ElementRef ,
79+ private readonly changeDetectorRef : ChangeDetectorRef ,
80+ private readonly setHostDisplay : boolean = false
81+ ) { }
6682
6783 ngAfterViewInit ( ) {
6884 this . _passAttributesAsProps ( ) ;
@@ -90,7 +106,9 @@ export abstract class ReactWrapperComponent<TProps extends {}> implements AfterV
90106 * Create an JSX renderer for an `@Input` property.
91107 * @param input The input property
92108 */
93- protected createInputJsxRenderer < TContext extends object > ( input : InputRendererOptions < TContext > ) : JsxRenderFunc < TContext > | undefined {
109+ protected createInputJsxRenderer < TContext extends object > (
110+ input : InputRendererOptions < TContext >
111+ ) : JsxRenderFunc < TContext > | undefined {
94112 if ( input === undefined ) {
95113 return undefined ;
96114 }
@@ -107,7 +125,7 @@ export abstract class ReactWrapperComponent<TProps extends {}> implements AfterV
107125 return ( context : TContext ) => renderFunc ( input , context ) ;
108126 }
109127
110- if ( typeof input === " object" ) {
128+ if ( typeof input === ' object' ) {
111129 const { componentType, factoryResolver, injector } = input ;
112130 const componentFactory = factoryResolver . resolveComponentFactory ( componentType ) ;
113131 const componentRef = componentFactory . create ( injector ) ;
@@ -123,7 +141,10 @@ export abstract class ReactWrapperComponent<TProps extends {}> implements AfterV
123141 * @param renderInputValue the value of the render `@Input` property.
124142 * @param jsxRenderer an optional renderer to use.
125143 */
126- protected createRenderPropHandler < TProps extends object > ( renderInputValue : InputRendererOptions < TProps > , jsxRenderer ?: JsxRenderFunc < TProps > ) : ( props ?: TProps , defaultRender ?: JsxRenderFunc < TProps > ) => JSX . Element | null {
144+ protected createRenderPropHandler < TProps extends object > (
145+ renderInputValue : InputRendererOptions < TProps > ,
146+ jsxRenderer ?: JsxRenderFunc < TProps >
147+ ) : ( props ?: TProps , defaultRender ?: JsxRenderFunc < TProps > ) => JSX . Element | null {
127148 const renderer = jsxRenderer || this . createInputJsxRenderer ( renderInputValue ) ;
128149
129150 return ( props ?: TProps , defaultRender ?: JsxRenderFunc < TProps > ) => {
@@ -136,9 +157,7 @@ export abstract class ReactWrapperComponent<TProps extends {}> implements AfterV
136157 }
137158
138159 private _passAttributesAsProps ( ) {
139- const hostAttributes = Array . from (
140- ( this . elementRef . nativeElement as HTMLElement ) . attributes
141- ) ;
160+ const hostAttributes = Array . from ( ( this . elementRef . nativeElement as HTMLElement ) . attributes ) ;
142161
143162 if ( ! this . reactNodeRef || ! isReactNode ( this . reactNodeRef . nativeElement ) ) {
144163 throw new Error ( 'reactNodeRef must hold a reference to a ReactNode' ) ;
@@ -148,15 +167,23 @@ export abstract class ReactWrapperComponent<TProps extends {}> implements AfterV
148167 hostAttributes . forEach ( attr => {
149168 const [ forbidden , alternativeAttrName ] = this . _isForbiddenAttribute ( attr ) ;
150169 if ( forbidden ) {
151- throw new Error ( `[${ ( this . elementRef . nativeElement as HTMLElement ) . tagName . toLowerCase ( ) } ] React wrapper components cannot have the '${ attr . name } ' attribute set. Use the following alternative: ${ alternativeAttrName || '' } ` ) ;
170+ throw new Error (
171+ `[${ ( this . elementRef
172+ . nativeElement as HTMLElement ) . tagName . toLowerCase ( ) } ] React wrapper components cannot have the '${
173+ attr . name
174+ } ' attribute set. Use the following alternative: ${ alternativeAttrName || '' } `
175+ ) ;
152176 }
153177 } ) ;
154178
155179 const whitelistedHostAttributes = hostAttributes . filter ( attr => ! this . _isIgnoredAttribute ( attr ) ) ;
156- const props = whitelistedHostAttributes . reduce ( ( acc , attr ) => ( {
157- ...acc ,
158- [ attr . name ] : attr . value ,
159- } ) , { } ) ;
180+ const props = whitelistedHostAttributes . reduce (
181+ ( acc , attr ) => ( {
182+ ...acc ,
183+ [ attr . name ] : attr . value ,
184+ } ) ,
185+ { }
186+ ) ;
160187
161188 this . reactNodeRef . nativeElement . setProperties ( props ) ;
162189 }
0 commit comments