|
1 | | -import { |
2 | | - PureComponent, |
3 | | - createElement, |
4 | | - Component, |
5 | | - ComponentElement, |
6 | | - ComponentClass, |
7 | | - StatelessComponent, |
8 | | - InputHTMLAttributes, |
9 | | - DetailedReactHTMLElement, |
10 | | - forwardRef, |
11 | | -} from 'react'; |
| 1 | +import {createElement, forwardRef} from 'react'; |
12 | 2 | import {Scope} from './scope'; |
13 | 3 | import {ScopeContext} from './context'; |
| 4 | +import Incorporator from './Incorporator'; |
14 | 5 |
|
15 | 6 | const wrapperComponents: Map<any, React.ComponentType<any>> = new Map(); |
16 | 7 |
|
17 | | -type Props = { |
18 | | - targetProps: any; |
19 | | - targetRef: any; |
20 | | - target: any; |
21 | | - scope: Scope; |
22 | | -}; |
23 | | - |
24 | | -type State = { |
25 | | - flip: boolean; |
26 | | -}; |
27 | | - |
28 | | -export class Incorporator extends PureComponent<Props, State> { |
29 | | - constructor(props: Props) { |
30 | | - super(props); |
31 | | - this.state = {flip: false}; |
32 | | - this.selector = props.targetProps.sel; |
33 | | - } |
34 | | - |
35 | | - private selector: string | symbol; |
36 | | - private unsubscribe: any; |
37 | | - |
38 | | - public componentDidMount() { |
39 | | - this.unsubscribe = this.props.scope.subscribe(this.selector, () => { |
40 | | - this.setState((prev: any) => ({flip: !prev.flip})); |
41 | | - }); |
42 | | - } |
43 | | - |
44 | | - private incorporateHandlers<P>(props: P, scope: Scope): P { |
45 | | - const handlers = scope.getSelectorHandlers(this.selector); |
46 | | - for (const evType of Object.keys(handlers)) { |
47 | | - const onFoo = `on${evType[0].toUpperCase()}${evType.slice(1)}`; |
48 | | - props[onFoo] = (ev: any) => handlers[evType]._n(ev); |
49 | | - } |
50 | | - return props; |
51 | | - } |
52 | | - |
53 | | - private materializeTargetProps() { |
54 | | - const {targetProps, targetRef, scope} = this.props; |
55 | | - let output = {...targetProps}; |
56 | | - output = this.incorporateHandlers(output, scope); |
57 | | - if (targetRef) { |
58 | | - output.ref = targetRef; |
59 | | - } |
60 | | - delete output.sel; |
61 | | - return output; |
62 | | - } |
63 | | - |
64 | | - public render() { |
65 | | - const {target} = this.props; |
66 | | - const targetProps = this.materializeTargetProps(); |
67 | | - if (targetProps.children) { |
68 | | - return createElement(target, targetProps, targetProps.children); |
69 | | - } else { |
70 | | - return createElement(target, targetProps); |
71 | | - } |
72 | | - } |
73 | | - |
74 | | - public componentWillUnmount() { |
75 | | - this.unsubscribe(); |
76 | | - } |
77 | | -} |
78 | | - |
79 | 8 | export function incorporate(type: any) { |
80 | 9 | if (!wrapperComponents.has(type)) { |
81 | 10 | wrapperComponents.set( |
|
0 commit comments