-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathfunction_component.dart
306 lines (250 loc) · 8.96 KB
/
function_component.dart
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import 'dart:developer';
import 'package:js/js_util.dart';
import 'package:meta/meta.dart';
import 'package:over_react/over_react.dart';
import 'package:over_react/src/util/prop_errors.dart';
import 'package:react/react.dart' as react;
import 'package:react/react_client.dart';
import 'package:react/react_client/js_backed_map.dart';
import 'package:react/react_client/react_interop.dart';
// ignore_for_file: uri_has_not_been_generated
import 'function_component.example.over_react.g.dart' as $;
mixin BasicPropsMixin on UiProps {
String basicProp;
String basic1;
String basic2;
String basic3;
String basic4;
}
UiFactory<BasicPropsMixin> Basic = uiFunctionComponent((props) {
return Dom.div()(
Dom.div()('prop id: ${props.id}'),
Dom.div()('default prop testing: ${props.basicProp}'),
Dom.div()('default prop testing: ${props.basic1}'),
Dom.div()(props.basic3, 'children: ${props.children}'),
);
}, $.$BasicPropsConfig);
UiFactory<BasicPropsMixin> Basic2 = uiFunctionComponent((props) {
return Dom.div()(
Dom.div()('prop id: ${props.id}'),
Dom.div()('default prop testing: ${props.basicProp}'),
Dom.div()('default prop testing: ${props.basic1}'),
Dom.div()(props.basic3, 'children: ${props.children}'),
);
}, $.$BasicPropsConfig);
// 1. Memory leak?
// 2. Async control-flow is halted
final Simple = uiFunctionComponent<BasicPropsMixin>((props) {
return Dom.div()(
Dom.div()('prop id: ${props.id}'),
Dom.div()('default prop testing: ${props.basicProp}'),
Dom.div()('default prop testing: ${props.basic1}'),
Dom.div()(null, props.basic4, 'children: ${props.children}'),
);
}, $.$BasicPropsConfig, initStatics: (statics) {
statics.defaultProps = (statics.newProps()
..basicProp = 'basicProp'
..basic1 = 'basic1'
);
});
final Simple2 = uiFunctionComponent<BasicPropsMixin>((props) {
return Dom.div()(
Dom.div()('prop id: ${props.id}'),
Dom.div()('default prop testing: ${props.basicProp}'),
Dom.div()('default prop testing: ${props.basic1}'),
Dom.div()(null, props.basic4, 'children: ${props.children}'),
);
}, $.$BasicPropsConfig, initStatics: (statics) {
statics
..defaultProps = (statics.newProps()
..basicProp = 'basicProp'
..basic1 = 'basic1'
)
..propTypes = {
statics.keyFor((p) => p.basicProp): (props, __) {
if (props.id == 'id') return PropError.value(props.id, 'id', 'That isn\'t a very descriptive ID.');
return null;
}
};
});
//UiFactory<BasicPropsMixin> Basic2 = (BasicPropsMixin props) {
// return Dom.div()(
// Dom.div()('prop id: ${props.id}'),
// Dom.div()('default prop testing: ${props.basicProp}'),
// Dom.div()('default prop testing: ${props.basic1}'),
// Dom.div()(null, props.basic4, 'children: ${props.children}'),
// );
//}.withTypedProps($.$Basic.factoryFactory);
//
//
//
//UiFactory<BasicPropsMixin> Basic5 = (BasicPropsMixin props) {
// return Dom.div()(
// Dom.div()('prop id: ${props.id}'),
// Dom.div()('default prop testing: ${props.basicProp}'),
// Dom.div()('default prop testing: ${props.basic1}'),
// Dom.div()(null, props.basic4, 'children: ${props.children}'),
// );
//}.asFunctionComponent();
//
//
//UiFactory<BasicPropsMixin> Basic3 = functionBuilder<BasicPropsMixin>().function((props) {
// return Dom.div()(
// Dom.div()('prop id: ${props.id}'),
// Dom.div()('default prop testing: ${props.basicProp}'),
// Dom.div()('default prop testing: ${props.basic1}'),
// Dom.div()(null, props.basic4, 'children: ${props.children}'),
// );
//}.asFunctionComponent();
//
//extension on ReactDartFunctionComponentFactoryProxy {
// UiFactory<T> withTypedProps<T extends UiProps>(FunctionFactoryFactory<T> factoryFactory) {
// return factoryFactory(this);
// }
// UiFactory<T> withTypedPropsFromOtherFactory<T extends UiProps>(UiFactory<T> otherFactory) {
// T uiFactory([Map backingProps]) => otherFactory(backingProps)..componentFactory = this;
// return uiFactory;
// }
//}
ReactElement functionComponentContent() {
// FIXME look into naming
final genericFactory = uiFunctionComponent<UiProps>((props) {
debugger();
return Dom.div()(
Dom.div()('prop id: ${props.id}'),
);
}, null);
//
//
// UiFactory otherFactory;
// Object closureVariable;
//
// final hocFactory = uiFunctionComponent<BasicPropsMixin>((props) {
// return otherFactory()(
// Dom.div()('closureVariable: ${closureVariable}'),
// Dom.div()('prop basic1: ${props.basic1}'),
// );
// }, $.$basicConfig);
final basicFactory = uiFunctionComponent<BasicPropsMixin>((props) {
return Dom.div()(
Dom.div()('prop id: ${props.id}'),
Dom.div()('prop basic1: ${props.basic1}'),
);
// FIXME should displayName really default to "Basic" in this case?
}, $.$BasicPropsConfig, displayName: 'basicFactory');
return Fragment()(
(genericFactory()..id = '1')(),
(basicFactory()..id = '2'..basic1 = 'basic1 value')(),
(Basic()..id = '3'..basicProp = 'basicProp')(),
(Simple()..basicProp = 'basicProp')(),
);
}
UiFactory<T> uiFunctionComponent<T extends UiProps>(
dynamic Function(T props) functionComponent,
// FIXME allow passing in displayName for generic function components
FunctionComponentConfig<T> config,
{
PropsFactory<T> propsFactory,
String displayName,
void Function(UiFunctionComponentStatics<T>) initStatics,
}) {
if (config != null) {
if (propsFactory != null) throw ArgumentError('propsFactory cannot be used along with config');
propsFactory = config.propsFactory;
displayName ??= config.componentName;
}
// Get the display name from the inner function if possible so it doesn't become `_uiFunctionComponentWrapper`
// FIXME make this work in DDC and make more robust
displayName ??= getFunctionName(functionComponent);
dynamic _uiFunctionComponentWrapper(Map props) {
return functionComponent(propsFactory.jsMap(props as JsBackedMap));
}
/// FIXME DartFunctionComponent should be JsBackedMap?
final factory = react.registerFunctionComponent(_uiFunctionComponentWrapper,
displayName: displayName);
if (propsFactory == null) {
// todo allow passing in of custom uiFactory/typedPropsFactory
// TODO make it easier to pass in parts of generatedInfo
if (T != UiProps && T != GenericUiProps) {
throw ArgumentError('config.propsFactory must be provided when using custom props classes');
}
propsFactory = PropsFactory.fromUiFactory(([backingMap]) => GenericUiProps(factory, backingMap)) as PropsFactory<T>;
}
if (initStatics != null) {
final statics = UiFunctionComponentStatics<T>._(
newProps: () => propsFactory.jsMap(JsBackedMap()),
keyFor: (accessProps) => getPropKey(accessProps, propsFactory.map)
);
initStatics(statics);
if (statics.defaultProps != null) {
// fixme need to move to react-dart
(factory.reactFunction as ReactClass).defaultProps = JsBackedMap.from(statics.defaultProps).jsObject;
}
// fixme need to implement in react-dart
// if (statics.propTypes != null) {}
}
T _uiFactory([Map backingMap]) {
T builder;
if (backingMap == null) {
builder = propsFactory.jsMap(JsBackedMap());
} else if (backingMap is JsBackedMap) {
builder = propsFactory.jsMap(backingMap);
} else {
builder = propsFactory.map(backingMap);
}
return builder..componentFactory = factory;
}
return _uiFactory;
}
String getFunctionName(Function f) {
if (f == null) throw ArgumentError.notNull('f');
// DDC
// todo
// Dart2js
final constructor = getProperty(f, 'constructor');
if (constructor != null) {
return getProperty(constructor, 'name');
}
return null;
}
class UiFunctionComponentStatics<T> {
Map defaultProps;
Map<String, react.PropValidator<T>> propTypes;
final String Function(void Function(T) accessProps) keyFor;
final T Function() newProps;
UiFunctionComponentStatics._({this.keyFor, this.newProps});
//
// T newProps() => this._newProps();
//
// String keyFor(void Function(T) accessProps) => this._keyFor(accessProps);
}
class GenericUiProps extends UiProps {
@override
final Map props;
GenericUiProps(ReactComponentFactoryProxy componentFactory, [Map props]) :
this.props = props ?? JsBackedMap() {
this.componentFactory = componentFactory;
}
@override
String get propKeyNamespace => '';
@override
bool get $isClassGenerated => true;
}
typedef FunctionFactoryFactory<T extends UiProps> = UiFactory<T> Function(ReactDartFunctionComponentFactoryProxy);
@protected
class FunctionComponentConfig<T extends UiProps> {
@protected
final PropsFactory<T> propsFactory;
final String componentName;
@protected
FunctionComponentConfig({this.propsFactory, this.componentName});
}
class PropsFactory<T extends UiProps> {
final T Function(Map props) map;
final T Function(JsBackedMap props) jsMap;
PropsFactory({
@required this.map,
@required this.jsMap,
});
PropsFactory.fromUiFactory(UiFactory<T> factory) : this.map = factory, this.jsMap = factory;
}