-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
191 lines (160 loc) · 5.48 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import React from "react";
import hoistNonReactStatics from "hoist-non-react-statics";
import LiftPropsContext from "./context";
import { IAnyObject, ILifterContext, IPropHolder } from "./types";
import { getDisplayName } from "./utils";
export interface ICreateLifterOptions {
displayName?: string;
wrapper?: boolean;
extraProps?: {
[key: string]: any;
};
}
const Lifters = ({ children }) => children;
export function createLifter<T extends object>(options: ICreateLifterOptions = {}): React.ComponentClass<T> {
type Props = {
contextValue: ILifterContext,
} & T;
const Lifter = class extends React.PureComponent<Props> { // tslint:disable-line max-classes-per-file
public static displayName = "Lifter";
private componentId: number;
private lifterRef: React.RefObject<null>;
constructor(props: Props) {
super(props);
this.componentId = -1;
this.lifterRef = React.createRef();
}
public componentWillUnmount() {
this.removeProps();
}
public componentDidMount() {
// This is a little hacky, but it's the best way I can think of
// to determine the index this lifter is in the DOM. It also means this
// will only work with ReactDOM :(
if (this.props.contextValue) {
const domNodes = document.querySelectorAll(`.${this.getClassName()}`);
const index = [ ...domNodes ].findIndex((domNode) => domNode === this.lifterRef.current);
this.componentId = this.props.contextValue.registerComponent(index);
}
this.liftProps();
}
public componentDidUpdate() {
this.liftProps();
}
public removeProps() {
if (this.props.contextValue) {
this.props.contextValue.removeProps(this.componentId);
}
}
public liftProps() {
const props = { ...(this.props as IAnyObject) };
delete props.contextValue;
if (this.props.contextValue) {
this.props.contextValue.liftProps(this.componentId, props);
}
}
public getClassName() {
if (this.props.contextValue && this.props.contextValue.id) {
return `__react-lift-props-lifter__${this.props.contextValue.id}__`;
}
return `__react-lift-props-lifter__`;
}
public render() {
return <div className={this.getClassName()} ref={this.lifterRef}></div>;
}
};
const WrappedLifter = class extends React.PureComponent<T> { // tslint:disable-line max-classes-per-file
public static displayName = options.displayName || "Wrapped(Lifter)";
public render() {
return (
<>
<LiftPropsContext.Consumer>
{(contextValue) => (
<Lifter
{...(this.props as T)}
{...(options.extraProps)}
contextValue={contextValue}
/>
)}
</LiftPropsContext.Consumer>
{options.wrapper && this.props.children}
</>
);
}
};
return WrappedLifter;
}
let parentIdCounter = 0;
export type UnwrappedComponent = React.ComponentClass<IAnyObject & { liftedProps: IAnyObject[] }>;
export function withLiftedProps(component: UnwrappedComponent): React.ComponentClass {
const WrappedComponent = class extends React.PureComponent<
IAnyObject,
{ liftedProps: IAnyObject[] }
> { // tslint:disable-line max-classes-per-file
public static displayName = `withLiftedProps(${getDisplayName(component)})`;
public idCounter = 0;
public id = 0;
public liftedProps: IPropHolder[] = [];
public contextValue: ILifterContext;
constructor(props: IAnyObject) {
super(props);
this.id = parentIdCounter = parentIdCounter + 1;
this.contextValue = {
id: this.id,
liftProps: this.liftProps,
registerComponent: this.registerComponent,
removeProps: this.removeProps,
};
}
public registerComponent = (index: number) => {
const componentId = this.idCounter = this.idCounter + 1;
const newLiftedProps: IPropHolder[] = [ ...this.liftedProps ];
if (newLiftedProps[index]) {
newLiftedProps.splice(index, 0, { id: componentId });
} else {
newLiftedProps.push({ id: componentId });
}
this.liftedProps = newLiftedProps;
return componentId;
}
public removeProps = (id: number) => {
const newLiftedProps = this.liftedProps.filter((propsHolder) => propsHolder.id !== id);
this.liftedProps = newLiftedProps;
this.updateLiftedProps();
}
public liftProps = (id: number, props: IAnyObject) => {
const newLiftedProps = this.liftedProps.map((propsHolder) => {
if (propsHolder.id === id) {
return {
id,
props,
};
}
return propsHolder;
});
this.liftedProps = newLiftedProps;
this.updateLiftedProps();
}
public updateLiftedProps() {
const liftedProps = this.liftedProps
.map((propsHolder) => propsHolder.props)
.filter(Boolean) as IAnyObject[];
this.setState({ liftedProps });
}
public render() {
const props = { ...this.props };
delete props.children;
return (
<LiftPropsContext.Provider value={this.contextValue}>
<Lifters>
{this.props.children}
</Lifters>
{this.state && this.state.liftedProps &&
React.createElement(component, { ...props, liftedProps: this.state.liftedProps })
}
</LiftPropsContext.Provider>
);
}
};
return hoistNonReactStatics(WrappedComponent, component);
}