Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CPLAT-3873 Function Components #606

Merged
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
dd8bb52
Initial spike for other branch
sydneyjodon-wk Jul 7, 2020
a81cdf8
Add v5_functionComponent to parsing versions
sydneyjodon-wk Jul 7, 2020
8e2d970
Update generated file and error handling
sydneyjodon-wk Jul 7, 2020
d773544
Parse ComponentFactoryDeclarations
sydneyjodon-wk Jul 9, 2020
86bc327
Remove v5 and update PropsMapViewOrFunctionComponentDeclaration
sydneyjodon-wk Jul 9, 2020
6958e22
Update example
sydneyjodon-wk Jul 10, 2020
7dc77f6
Add codegen for FunctionComponentConfig
sydneyjodon-wk Jul 10, 2020
027cd66
Fix test failures and format
sydneyjodon-wk Jul 10, 2020
3e62161
Update getPropsNameFromConfig
sydneyjodon-wk Jul 10, 2020
e2da213
Add initial parsing tests
sydneyjodon-wk Jul 11, 2020
42005e2
Generate config for each factory to keep correct displayName
sydneyjodon-wk Jul 13, 2020
685e9d7
Add parsing for generic function components
sydneyjodon-wk Jul 14, 2020
659692e
Move defaultProps to react-dart
sydneyjodon-wk Jul 14, 2020
1d614a9
Update pubspec dependency override
sydneyjodon-wk Jul 14, 2020
f57ea6c
Add codegen tests
sydneyjodon-wk Jul 14, 2020
68db3d4
Handle parsing null config and custom props factory
sydneyjodon-wk Jul 16, 2020
d9d0708
Add function component snippets
sydneyjodon-wk Jul 16, 2020
d71da40
Format
sydneyjodon-wk Jul 16, 2020
4885e6b
Merge branch 'master' into CPLAT-3873-function-components
sydneyjodon-wk Jul 16, 2020
b19542c
Initial integration tests
sydneyjodon-wk Jul 17, 2020
49d1f5e
Merge branch 'master' into CPLAT-3873-function-components
sydneyjodon-wk Jul 17, 2020
27bc400
Revert "Initial integration tests"
sydneyjodon-wk Jul 17, 2020
3828257
Revert "Revert "Initial integration tests""
sydneyjodon-wk Jul 17, 2020
6fa267e
Remove unused import
sydneyjodon-wk Jul 17, 2020
0a4d434
Add tests based on component tests
sydneyjodon-wk Jul 17, 2020
9ef8829
Add test cases for custom PropsFactory
sydneyjodon-wk Jul 17, 2020
a2d31b2
Add tests for function components using UiProps
sydneyjodon-wk Jul 20, 2020
e43751a
Remove defaultProps and update examples
sydneyjodon-wk Jul 20, 2020
f99e40d
Cleanup tests
sydneyjodon-wk Jul 20, 2020
3a2daeb
Remove unused import
sydneyjodon-wk Jul 20, 2020
59e8d8e
Update function name and doc comments
sydneyjodon-wk Jul 22, 2020
b759f8a
Update migration guide examples
sydneyjodon-wk Jul 22, 2020
cec79b9
Update based on reviewer feedback
sydneyjodon-wk Jul 23, 2020
f117e9d
Merge 'upstream/function-components-wip' into CPLAT-3873-function-com…
aaronlademann-wf Jul 23, 2020
d1c01d7
Remove yield for non-generated factories
sydneyjodon-wk Jul 23, 2020
eaa69ca
Add factory grouping and update examples
sydneyjodon-wk Jul 24, 2020
0e38d4d
Add parsing for hocs and update wrapper function typing
sydneyjodon-wk Jul 24, 2020
82f92a4
Run build and add null check
sydneyjodon-wk Jul 24, 2020
0fd444a
Update generic args for uiFunction
sydneyjodon-wk Jul 24, 2020
e7a263d
Update uiFunction args
sydneyjodon-wk Jul 24, 2020
c22f0a2
Address feedback
sydneyjodon-wk Jul 27, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,30 @@ that you get for free from OverReact, you're ready to start building your own cu
}
}
```

* #### Function Component Boilerplate

```dart
import 'package:over_react/over_react.dart';
part 'foo_component.over_react.g.dart';

UiFactory<FooProps> Foo = uiFunction((props) {
// Set default props using null-aware operators.
final isDisabled = props.isDisabled ?? false;
final items = props.items ?? [];

// Return the rendered component contents here.
// The `props` variable is typed; no need for string keys!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we return something here to demonstrate things like prop forwarding, etc.?

},
// The generated props config will match the factory name.
$FooPropsConfig);
sydneyjodon-wk marked this conversation as resolved.
Show resolved Hide resolved

mixin FooProps on UiProps {
// Props go here, declared as fields:
bool isDisabled;
Iterable<String> items;
}
```

&nbsp;

Expand Down
18 changes: 9 additions & 9 deletions doc/new_boilerplate_migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -727,19 +727,19 @@ import 'package:over_react/over_react.dart';

part 'foo.over_react.g.dart';

UiFactory<FooProps> Foo = uiFunctionComponent(
UiFactory<FooProps> Foo = uiFunction(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The RFD used both names for this function. uiFunction was what the ticket said to name it, but if anyone thinks it should be uiFunctionComponent I can update it.

(props) {
return 'foo: ${props.foo}';
},
$fooPropsConfig, // ignore: undefined_identifier
$FooPropsConfig, // ignore: undefined_identifier
);

mixin FooProps on UiProps {
String foo;
}
```

Here, `uiFunctionComponent` gets a generic parameter of `FooProps` inferred
Here, `uiFunction` gets a generic parameter of `FooProps` inferred
from the LHS typing, allowing props to be statically typed as `FooProps`.

The generated `$FooPropsConfig` is passed in as an argument, and serves
Expand All @@ -755,24 +755,24 @@ same behavior as `defaultProps`, but with the restriction that a given prop
__must either be nullable or have a default value, but not both__.

```dart
UiFactory<FooProps> Foo = uiFunctionComponent(
UiFactory<FooProps> Foo = uiFunction(
(props) {
final foo = props.foo ?? 'default foo value';

return 'foo: $foo';
},
$fooPropsConfig, // ignore: undefined_identifier
$FooPropsConfig, // ignore: undefined_identifier
);
```

#### With propTypes

```dart
UiFactory<FooProps> Foo = uiFunctionComponent(
UiFactory<FooProps> Foo = uiFunction(
(props) {
return 'foo: ${props.foo}';
},
$fooPropsConfig, // ignore: undefined_identifier
$FooPropsConfig, // ignore: undefined_identifier
getPropTypes: (keyFor) => {
keyFor((p) => p.foo): (props, info) {
if (props.foo == 'bar') {
Expand Down Expand Up @@ -803,14 +803,14 @@ UiFactory<FooProps> createFooHoc(UiFactory otherFactory) {
Object closureVariable;
// ...

final FooHoc = uiFunctionComponent<FooProps>(
final FooHoc = uiFunction<FooProps>(
sydneyjodon-wk marked this conversation as resolved.
Show resolved Hide resolved
(props) {
return otherFactory()(
Dom.div()('closureVariable: ${closureVariable}'),
Dom.div()('prop foo: ${props.foo}'),
);
},
$fooPropsConfig, // ignore: undefined_identifier
$FooPropsConfig, // ignore: undefined_identifier
);

return FooHoc;
Expand Down
3 changes: 3 additions & 0 deletions example/builder/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import './src/basic.dart';
import './src/basic_library.dart';
import './src/generic_inheritance_sub.dart';
import './src/generic_inheritance_super.dart';
import './src/function_component.dart' as function;

main() {
setClientConfiguration();
Expand Down Expand Up @@ -52,6 +53,8 @@ main() {
..subProp = 'sub prop part of lib'
..superProp = 'super prop part of lib'
)(),
Dom.h3()('Function component:'),
function.functionComponentContent(),
Dom.h3()('getDefaultProps via component factories'),
componentConstructorsByName.keys.map((name) => Dom.div()(
'new $name()',
Expand Down
79 changes: 79 additions & 0 deletions example/builder/src/function_component.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright 2020 Workiva Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import 'package:over_react/over_react.dart';
import 'package:react/react_client.dart';
import 'package:react/react_client/react_interop.dart';
sydneyjodon-wk marked this conversation as resolved.
Show resolved Hide resolved

// ignore_for_file: uri_has_not_been_generated
part 'function_component.over_react.g.dart';

mixin BasicProps on UiProps {
String basicProp;
String basic1;
String basic2;
String basic3;
String basic4;
}

UiFactory<BasicProps> Basic = uiFunction((props) {
return Fragment()(
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);

final Simple = uiFunction<BasicProps>((props) {
final basicProp = props.basicProp ?? 'basicProp';
final basic1 = props.basic1 ?? 'basic1';

return Fragment()(
Dom.div()('prop id: ${props.id}'),
Dom.div()('default prop testing: $basicProp'),
Dom.div()('default prop testing: $basic1'),
Dom.div()(null, props.basic4, 'children: ${props.children}'),
);
}, $SimplePropsConfig);

ReactElement functionComponentContent() {
GenericFactory(props) {
return Dom.div()('prop id: ${props.id}');
}
final genericFactory = uiFunction<UiProps>(GenericFactory, null);

final basicFactory = uiFunction<BasicProps>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO we should also have one utilizing forwardRef.

(props) {
return Fragment()(
Dom.div()('prop id: ${props.id}'),
Dom.div()('prop basic1: ${props.basic1}'),
);
},
null,
propsFactory: PropsFactory.fromUiFactory(Basic),
displayName: 'basicFactory',
);

return Fragment()(
(genericFactory()..id = '1')(),
(basicFactory()
..id = '2'
..basic1 = 'basic1 value')(),
(Basic()
..id = '3'
..basicProp = 'basicProp')(),
(Simple()..id = '4')(),
);
}
166 changes: 166 additions & 0 deletions example/builder/src/function_component.over_react.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/over_react.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export 'src/component_declaration/component_base_2.dart' show
UiStatefulMixin2;
export 'src/component_declaration/built_redux_component.dart';
export 'src/component_declaration/flux_component.dart';
export 'src/component_declaration/function_component.dart';
export 'src/util/character_constants.dart';
export 'src/util/class_names.dart';
export 'src/util/constants_base.dart';
Expand Down
5 changes: 4 additions & 1 deletion lib/src/builder/codegen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ class ImplGenerator {
void generate(BoilerplateDeclaration declaration) {
switch (declaration.type) {
case DeclarationType.propsMapViewOrFunctionComponentDeclaration:
_generatePropsMapViewOrFunctionComponent(declaration);
// Generated code only needed for props config.
if ((declaration as PropsMapViewOrFunctionComponentDeclaration).props != null) {
sydneyjodon-wk marked this conversation as resolved.
Show resolved Hide resolved
_generatePropsMapViewOrFunctionComponent(declaration);
}
break;
case DeclarationType.classComponentDeclaration:
_generateClassComponent(declaration);
Expand Down
Loading