|
| 1 | +import {Component, ComponentType, forwardRef, createRef, createElement} from 'react'; |
| 2 | +import Incorporator from './Incorporator' |
| 3 | + |
| 4 | +let moduleEntries: any = [] |
| 5 | + |
| 6 | +let onMounts: any[] = [] |
| 7 | +let onUpdates: any[] = [] |
| 8 | +let onUnmounts: any[] = [] |
| 9 | + |
| 10 | +export function setModules(mods: any) { |
| 11 | + if (mods === null || typeof mods !== 'object') return; |
| 12 | + moduleEntries = Object.entries(mods) |
| 13 | + onMounts = moduleEntries.map(mod => [mod[0], mod[1].componentDidMount]).filter(mod => mod[1]) |
| 14 | + onUpdates = moduleEntries.map(mod => [mod[0], mod[1].componentDidUpdate]).filter(mod => mod[1]) |
| 15 | + onUnmounts = moduleEntries.map(mod => [mod[0], mod[1].componentWillUnmount]).filter(mod => mod[1]) |
| 16 | +} |
| 17 | + |
| 18 | +export function usesModules() { |
| 19 | + return moduleEntries.length > 0 |
| 20 | +} |
| 21 | + |
| 22 | +export function hasModuleProps (props) { |
| 23 | + return props |
| 24 | + ? moduleEntries.some(([mkey]) => props.hasOwnProperty(mkey)) |
| 25 | + : false |
| 26 | +} |
| 27 | + |
| 28 | +function moduleProcessor (base, current, props) { |
| 29 | + if (current && base.length) { |
| 30 | + base.forEach(([key, f]) => { |
| 31 | + const prop = props[key] |
| 32 | + if (prop) f(current, prop) |
| 33 | + }); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +export class Modulizer extends Component<any, any> { |
| 38 | + private ref: any; |
| 39 | + private element: any; |
| 40 | + private setRef: any; |
| 41 | + constructor(props) { |
| 42 | + super(props); |
| 43 | + this.element = null |
| 44 | + |
| 45 | + const {targetProps, targetRef} = props |
| 46 | + const useRef = hasModuleProps(targetProps) |
| 47 | + if (targetRef) { |
| 48 | + if (typeof targetRef === 'function' && useRef) { |
| 49 | + this.setRef = element => { |
| 50 | + this.element = element; |
| 51 | + targetRef(element); |
| 52 | + }; |
| 53 | + |
| 54 | + this.ref = this.setRef; |
| 55 | + } else { |
| 56 | + this.ref = targetRef; |
| 57 | + } |
| 58 | + } else { |
| 59 | + this.ref = useRef ? createRef() : null; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public componentDidMount() { |
| 64 | + moduleProcessor(onMounts, this.element || (this.ref && this.ref.current), this.props.targetProps) |
| 65 | + } |
| 66 | + |
| 67 | + public componentDidUpdate() { |
| 68 | + moduleProcessor(onUpdates, this.element || (this.ref && this.ref.current), this.props.targetProps) |
| 69 | + } |
| 70 | + |
| 71 | + public componentWillUnmount() { |
| 72 | + moduleProcessor(onUnmounts, this.element || (this.ref && this.ref.current), this.props.targetProps) |
| 73 | + } |
| 74 | + |
| 75 | + render() { |
| 76 | + const targetProps = {...this.props.targetProps} |
| 77 | + moduleEntries.forEach(pair => delete targetProps[pair[0]]) |
| 78 | + const output: any = {...this.props, targetRef: this.ref, targetProps}; |
| 79 | + |
| 80 | + return createElement(Incorporator, output); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// export function Modulizer(Comp: any): ComponentType<any> { |
| 85 | +// class ModulizerComponent extends Component<any, any> { |
| 86 | +// private ref: any; |
| 87 | +// private element: any; |
| 88 | +// private setRef: any; |
| 89 | +// constructor(props) { |
| 90 | +// super(props); |
| 91 | +// this.element = null |
| 92 | + |
| 93 | +// const {targetProps, targetRef} = props.modularizerProps |
| 94 | +// const useRef = hasModuleProps(targetProps) |
| 95 | +// if (targetRef) { |
| 96 | +// if (typeof targetRef === 'function' && useRef) { |
| 97 | +// this.setRef = element => { |
| 98 | +// this.element = element; |
| 99 | +// targetRef(element); |
| 100 | +// }; |
| 101 | + |
| 102 | +// this.ref = this.setRef; |
| 103 | +// } else { |
| 104 | +// this.ref = targetRef; |
| 105 | +// } |
| 106 | +// } else { |
| 107 | +// this.ref = useRef ? createRef() : null; |
| 108 | +// } |
| 109 | +// } |
| 110 | + |
| 111 | +// public componentDidMount() { |
| 112 | +// moduleProcessor(onMounts, this.element || (this.ref && this.ref.current), this.props.modularizerProps.targetProps) |
| 113 | +// } |
| 114 | + |
| 115 | +// public componentDidUpdate() { |
| 116 | +// moduleProcessor(onUpdates, this.element || (this.ref && this.ref.current), this.props.modularizerProps.targetProps) |
| 117 | +// } |
| 118 | + |
| 119 | +// public componentWillUnmount() { |
| 120 | +// moduleProcessor(onUnmounts, this.element || (this.ref && this.ref.current), this.props.modularizerProps.targetProps) |
| 121 | +// } |
| 122 | + |
| 123 | +// render() { |
| 124 | +// const targetProps = {...this.props.modularizerProps.targetProps} |
| 125 | +// moduleEntries.forEach(pair => delete targetProps[pair[0]]) |
| 126 | +// const output: any = {...this.props.modularizerProps, targetRef: this.ref, targetProps}; |
| 127 | + |
| 128 | +// return createElement(Comp, output); |
| 129 | +// } |
| 130 | +// } |
| 131 | + |
| 132 | +// return forwardRef<any, any>((props, ref) => { |
| 133 | +// return createElement(ModulizerComponent, {modularizerProps: props, targetRef: ref} ) |
| 134 | +// }); |
| 135 | +// } |
| 136 | + |
| 137 | +// export default class Modulizer extends PureComponent<Props, State> { |
| 138 | +// private ref: any; |
| 139 | +// private element: any; |
| 140 | +// private setRef: any; |
| 141 | + |
| 142 | +// constructor(props: Props) { |
| 143 | +// super(props); |
| 144 | +// this.element = null |
| 145 | + |
| 146 | +// const useRef = hasModuleProps(props.targetProps) |
| 147 | +// if (props.targetRef) { |
| 148 | +// if (typeof props.targetRef === 'function' && useRef) { |
| 149 | +// this.setRef = element => { |
| 150 | +// this.element = element; |
| 151 | +// props.targetRef(element); |
| 152 | +// }; |
| 153 | + |
| 154 | +// this.ref = this.setRef; |
| 155 | +// } else { |
| 156 | +// this.ref = props.targetRef; |
| 157 | +// } |
| 158 | +// } else { |
| 159 | +// this.ref = useRef ? createRef() : null; |
| 160 | +// } |
| 161 | +// } |
| 162 | + |
| 163 | +// public componentDidMount() { |
| 164 | +// this.unsubscribe = this.props.scope.subscribe(this.selector, () => { |
| 165 | +// this.setState((prev: any) => ({flip: !prev.flip})); |
| 166 | +// }); |
| 167 | + |
| 168 | +// moduleProcessor(onMounts, this.element || (this.ref && this.ref.current), this.props.targetProps) |
| 169 | +// } |
| 170 | + |
| 171 | +// public componentDidUpdate() { |
| 172 | +// moduleProcessor(onUpdates, this.element || (this.ref && this.ref.current), this.props.targetProps) |
| 173 | +// } |
| 174 | + |
| 175 | +// public componentWillUnmount() { |
| 176 | +// moduleProcessor(onUnmounts, this.element || (this.ref && this.ref.current), this.props.targetProps) |
| 177 | + |
| 178 | +// this.unsubscribe(); |
| 179 | +// } |
| 180 | + |
| 181 | +// private incorporateHandlers<P>(props: P, scope: Scope): P { |
| 182 | +// const handlers = scope.getSelectorHandlers(this.selector); |
| 183 | +// for (const evType of Object.keys(handlers)) { |
| 184 | +// const onFoo = `on${evType[0].toUpperCase()}${evType.slice(1)}`; |
| 185 | +// props[onFoo] = (ev: any) => handlers[evType]._n(ev); |
| 186 | +// } |
| 187 | +// return props; |
| 188 | +// } |
| 189 | + |
| 190 | +// private materializeTargetProps() { |
| 191 | +// const {targetProps, scope} = this.props; |
| 192 | +// let output = {...targetProps}; |
| 193 | +// output = this.incorporateHandlers(output, scope); |
| 194 | +// if (this.ref) { |
| 195 | +// output.ref = this.ref; |
| 196 | +// } |
| 197 | +// delete output.sel; |
| 198 | +// moduleEntries.forEach(pair => delete output[pair[0]]) |
| 199 | +// return output; |
| 200 | +// } |
| 201 | + |
| 202 | +// public render() { |
| 203 | +// const {target} = this.props; |
| 204 | +// const targetProps = this.materializeTargetProps(); |
| 205 | + |
| 206 | +// if (targetProps.children) { |
| 207 | +// return createElement(target, targetProps, targetProps.children); |
| 208 | +// } else { |
| 209 | +// return createElement(target, targetProps); |
| 210 | +// } |
| 211 | +// } |
| 212 | + |
| 213 | + |
| 214 | +// } |
0 commit comments