|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import * as React from 'react'; |
| 5 | +import { |
| 6 | + Component, |
| 7 | + ElementRef, |
| 8 | + ViewChild, |
| 9 | + ChangeDetectionStrategy, |
| 10 | + Input, |
| 11 | + ChangeDetectorRef, |
| 12 | + Renderer2, |
| 13 | + NgZone, |
| 14 | + Output, |
| 15 | + EventEmitter, |
| 16 | + Type, |
| 17 | +} from '@angular/core'; |
| 18 | + |
| 19 | +declare const __decorate: typeof import('tslib').__decorate; |
| 20 | + |
| 21 | +import { ReactWrapperComponent } from './wrapper-component'; |
| 22 | +import { registerElement } from '../renderer/registry'; |
| 23 | + |
| 24 | +export interface WrapComponentOptions<TProps extends object> { |
| 25 | + /** |
| 26 | + * The type of the component to wrap. |
| 27 | + */ |
| 28 | + ReactComponent: React.ComponentType<TProps>; |
| 29 | + |
| 30 | + /** |
| 31 | + * The selector to use. |
| 32 | + */ |
| 33 | + selector: string; |
| 34 | + |
| 35 | + /** |
| 36 | + * The prop names to pass to the `reactComponent`, if any. |
| 37 | + * Note that any prop starting with `on` will be converted to an `Output`, and other to `Input`s. |
| 38 | + * |
| 39 | + * @note If `reactComponent` has `propTypes`, this can be omitted. |
| 40 | + */ |
| 41 | + propNames?: string[]; |
| 42 | + |
| 43 | + /** |
| 44 | + * @see `WrapperComponentOptions#setHostDisplay`. |
| 45 | + */ |
| 46 | + setHostDisplay?: boolean; |
| 47 | + |
| 48 | + /** |
| 49 | + * An _optional_ callback for specified wether a prop should be considered an `Output`. |
| 50 | + * @default propName => propName.startsWith('on') |
| 51 | + */ |
| 52 | + isOutputProp?: (propName: string) => boolean; |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Gets the display name of a component. |
| 57 | + * @param WrappedComponent The type of the wrapper component |
| 58 | + */ |
| 59 | +function getDisplayName<TProps extends object>(WrappedComponent: React.ComponentType<TProps>): string { |
| 60 | + return WrappedComponent.displayName || WrappedComponent.name || 'Component'; |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Checks if the propName is an output one. |
| 65 | + * Currently uses a simple check - anything that starts with `on` is considered an output prop. |
| 66 | + */ |
| 67 | +function defaultIsOutputProp(propName: string): boolean { |
| 68 | + return propName.startsWith('on'); |
| 69 | +} |
| 70 | + |
| 71 | +function getPropNames<TProps extends object>(ReactComponent: React.ComponentType<TProps>) { |
| 72 | + if (!ReactComponent.propTypes) { |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + return Object.keys(ReactComponent.propTypes); |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * Wrap a React component with an Angular one. |
| 81 | + * |
| 82 | + * @template TProps The type of props of the underlying React element. |
| 83 | + * @param options Options for wrapping the component. |
| 84 | + * @returns A class of a wrapper Angular component. |
| 85 | + */ |
| 86 | +export function WrapComponent<TProps extends object>( |
| 87 | + options: Readonly<WrapComponentOptions<TProps>> |
| 88 | +): Type<ReactWrapperComponent<TProps>> { |
| 89 | + const Tag = getDisplayName(options.ReactComponent); |
| 90 | + registerElement(Tag, () => options.ReactComponent); |
| 91 | + |
| 92 | + const propNames = options.propNames || getPropNames(options.ReactComponent); |
| 93 | + const isOutputProp = options.isOutputProp || defaultIsOutputProp; |
| 94 | + |
| 95 | + const inputProps = propNames.filter(propName => !isOutputProp(propName)); |
| 96 | + const outputProps = propNames.filter(isOutputProp); |
| 97 | + |
| 98 | + const inputPropsBindings = inputProps.map(propName => `[${propName}]="${propName}"`); |
| 99 | + const outputPropsBindings = outputProps.map(propName => `(${propName})="${propName}.emit($event)"`); |
| 100 | + const propsBindings = [...inputPropsBindings, ...outputPropsBindings].join('\n'); |
| 101 | + |
| 102 | + @Component({ |
| 103 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 104 | + styles: ['react-renderer'], |
| 105 | + selector: options.selector, |
| 106 | + template: ` |
| 107 | + <${Tag} |
| 108 | + #reactNode |
| 109 | + ${propsBindings} |
| 110 | + > |
| 111 | + <ReactContent><ng-content></ng-content></ReactContent> |
| 112 | + </${Tag}> |
| 113 | + `, |
| 114 | + }) |
| 115 | + class WrapperComponent extends ReactWrapperComponent<TProps> { |
| 116 | + @ViewChild('reactNode') protected reactNodeRef: ElementRef; |
| 117 | + |
| 118 | + constructor(elementRef: ElementRef, changeDetectorRef: ChangeDetectorRef, renderer: Renderer2, ngZone: NgZone) { |
| 119 | + super(elementRef, changeDetectorRef, renderer, { ngZone, setHostDisplay: options.setHostDisplay }); |
| 120 | + |
| 121 | + outputProps.forEach(propName => { |
| 122 | + this[propName] = new EventEmitter<any>(); |
| 123 | + }); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + inputProps.forEach(propName => __decorate([Input()], WrapperComponent.prototype, propName)); |
| 128 | + outputProps.forEach(propName => __decorate([Output()], WrapperComponent.prototype, propName)); |
| 129 | + |
| 130 | + return WrapperComponent; |
| 131 | +} |
0 commit comments