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 5 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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,10 @@ that you get for free from OverReact, you're ready to start building your own cu

// Return the rendered component contents here.
// The `props` variable is typed; no need for string keys!
return Fragment()(
Dom.div()(items),
(Dom.button()..disabled = isDisabled)('Click me!'),
);
},
// The generated props config will match the factory name.
$FooConfig, // ignore: undefined_identifier
Expand Down
47 changes: 46 additions & 1 deletion doc/new_boilerplate_migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,19 @@ UiFactory<FooProps> Foo = uiFunction(
);
```

#### With UiProps

```dart
UiFactory<UiProps> Foo = uiFunction(
(props) {
return 'id: ${props.id}';
},
FunctionComponentConfig(
displayName: 'Foo',
),
);
```

#### With propTypes

```dart
Expand Down Expand Up @@ -810,13 +823,45 @@ UiFactory<FooProps> createFooHoc(UiFactory otherFactory) {
Dom.div()('prop foo: ${props.foo}'),
);
},
$FooConfig, // ignore: undefined_identifier
FunctionComponentConfig(
displayName: 'FooHoc',
propsFactory: PropsFactory.fromUiFactory(Foo),
),
);

return FooHoc;
}
```

#### With forwardRef

```dart
mixin FooProps on UiProps {
Ref forwardedRef;
Function doSomething;
}

UiFactory<FooProps> Foo = forwardRef<FooProps>((props, ref) {
return (_Foo()
..forwardedRef = ref
..doSomething = props.doSomething
)();
})(_Foo);

UiFactory<FooProps> _Foo = uiFunction(
(props) {
return Fragment()(
Dom.div()('Some text.'),
(Dom.button()
..ref = props.forwardedRef
..onClick = props.doSomething
)('Click me!'),
);
},
$_FooConfig, // ignore: undefined_identifier
);
sydneyjodon-wk marked this conversation as resolved.
Show resolved Hide resolved
```

## Upgrading Existing Code

To update your repository to the new boilerplate, you can use
Expand Down
48 changes: 39 additions & 9 deletions example/builder/src/function_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,35 @@ import 'package:over_react/over_react.dart';
part 'function_component.over_react.g.dart';

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

UiFactory<BasicProps> Basic = uiFunction((props) {
UiFactory<BasicProps> Basic = forwardRef<BasicProps>((props, ref) {
sydneyjodon-wk marked this conversation as resolved.
Show resolved Hide resolved
return (_Basic()
..forwardedRef = ref
..addProps(props))();
})(_Basic);

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}'),
(Dom.div()..ref = props.forwardedRef)(
props.basic3, 'children: ${props.children}'),
);
},
$BasicConfig, // ignore: undefined_identifier
$_BasicConfig, // ignore: undefined_identifier
);

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

Expand All @@ -45,17 +55,30 @@ final Simple = uiFunction<BasicProps>((props) {
Dom.div()('default prop testing: $basicProp'),
Dom.div()('default prop testing: $basic1'),
Dom.div()(null, props.basic4, 'children: ${props.children}'),
(Foo()..content = props.basic2)(),
);
},
$SimpleConfig, // ignore: undefined_identifier
);

mixin FooProps on UiProps {
String content;
}

final Foo = uiFunction<FooProps>(
(props) => Dom.div()('forwarded prop: ${props.content}'),
$FooConfig, // ignore: undefined_identifier
);

ReactElement functionComponentContent() {
GenericFactory(props) {
return Dom.div()('prop id: ${props.id}');
}

final genericFactory = uiFunction<UiProps>(GenericFactory, null);
final genericFactory = uiFunction<UiProps>(
GenericFactory,
FunctionComponentConfig(),
);

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) {
Expand All @@ -64,19 +87,26 @@ ReactElement functionComponentContent() {
Dom.div()('prop basic1: ${props.basic1}'),
);
},
null,
propsFactory: PropsFactory.fromUiFactory(Basic),
displayName: 'basicFactory',
FunctionComponentConfig(
propsFactory: PropsFactory.fromUiFactory(Basic),
displayName: 'basicFactory',
)
);

// Access the div element later using `divRef.current`.
Ref divRef = createRef();

return Fragment()(
(genericFactory()..id = '1')(),
(basicFactory()
..id = '2'
..basic1 = 'basic1 value')(),
(Basic()
..ref = divRef
..id = '3'
..basicProp = 'basicProp')(),
(Simple()..id = '4')(),
(Simple()
..id = '4'
..basic2 = 'basic2 value')(),
);
}
117 changes: 115 additions & 2 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.

5 changes: 1 addition & 4 deletions lib/src/builder/codegen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ class ImplGenerator {
void generate(BoilerplateDeclaration declaration) {
switch (declaration.type) {
case DeclarationType.propsMapViewOrFunctionComponentDeclaration:
// Generated code only needed for props config.
if ((declaration as PropsMapViewOrFunctionComponentDeclaration).props != null) {
_generatePropsMapViewOrFunctionComponent(declaration);
}
_generatePropsMapViewOrFunctionComponent(declaration);
break;
case DeclarationType.classComponentDeclaration:
_generateClassComponent(declaration);
Expand Down
Loading