Skip to content

Commit

Permalink
Merge pull request #48 from jacehensley-wf/1.6.0/UIP-1897_traverse_ge…
Browse files Browse the repository at this point in the history
…t_props/dev

UIP-1897: Add support for traversing wrapper components to getProps
  • Loading branch information
leviwith-wf authored Feb 20, 2017
2 parents 6543e36 + f1e528c commit a0af902
Show file tree
Hide file tree
Showing 12 changed files with 391 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: dart
dart:
- "1.19.1"
- "1.21.1"
with_content_shell: true
before_install:
- export DISPLAY=:99.0
Expand Down
30 changes: 29 additions & 1 deletion lib/src/util/react_wrappers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import 'dart:collection';
import 'dart:html';

import 'package:js/js.dart';
import 'package:over_react/src/component_declaration/component_type_checking.dart';
import 'package:react/react.dart' as react;
import 'package:react/react_client.dart';
import 'package:react/react_client/js_interop_helpers.dart';
Expand Down Expand Up @@ -107,11 +108,38 @@ Expando<UnmodifiableMapView> _elementPropsCache = new Expando('_elementPropsCach
/// For a native Dart component, this returns its [react.Component.props] in an unmodifiable Map view.
/// For a JS component, this returns the result of [getJsProps] in an unmodifiable Map view.
///
/// If [traverseWrappers] is `true` then it will return an unmodifiable Map view of props of the first non-"Wrapper"
/// instance.
///
/// Throws if [instance] is not a valid [ReactElement] or composite [ReactComponent] .
Map getProps(/* ReactElement|ReactComponent */ instance) {
Map getProps(/* ReactElement|ReactComponent */ instance, {bool traverseWrappers: false}) {
var isCompositeComponent = _isCompositeComponent(instance);

if (isValidElement(instance) || isCompositeComponent) {
if (traverseWrappers) {
ComponentTypeMeta instanceTypeMeta;

if (isCompositeComponent && isDartComponent(instance)) {
ReactClass type = getProperty(getDartComponent(instance).jsThis, 'constructor');
instanceTypeMeta = getComponentTypeMeta(type);
} else if (isValidElement(instance)) {
instanceTypeMeta = getComponentTypeMeta(instance.type);
} else {
throw new ArgumentError.value(instance, 'instance',
'must either be a Dart component ReactComponent or ReactElement when traverseWrappers is true.');
}

if (instanceTypeMeta.isWrapper) {
assert(isDartComponent(instance) && 'Non-dart components should not be wrappers' is String);

List children = getProps(instance)['children'];

if (children != null && children.isNotEmpty && isValidElement(children.first)) {
return getProps(children.first, traverseWrappers: true);
}
}
}

if (!isCompositeComponent) {
var cachedView = _elementPropsCache[instance];
if (cachedView != null) return cachedView;
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ homepage: https://github.com/Workiva/over_react/
authors:
- Workiva UI Platform Team <uip@workiva.com>
environment:
sdk: ">=1.19.1"
sdk: ">=1.21.1"
dependencies:
analyzer: ">=0.26.1+3 <0.30.0"
barback: "^0.15.0"
Expand Down
4 changes: 2 additions & 2 deletions smithy.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
project: dart
language: dart

# dart 1.19.1, built from https://github.com/Workiva/smithy-runner-dart/tree/0.0.4
runner_image: drydock-prod.workiva.org/workiva/smithy-runner-dart:74173
# dart 1.21.1, built from https://github.com/Workiva/smithy-runner-dart/tree/0.0.4
runner_image: drydock-prod.workiva.net/workiva/smithy-runner-dart:119662

script:
- pub get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import 'package:react/react_client.dart';
import 'package:react/react_client/react_interop.dart';
import 'package:test/test.dart';

import 'component_type_checking_test/one_level_wrapper.dart';
import '../../test_util/one_level_wrapper.dart';
import '../../test_util/two_level_wrapper.dart';
import 'component_type_checking_test/test_a.dart';
import 'component_type_checking_test/test_b.dart';
import 'component_type_checking_test/two_level_wrapper.dart';
import 'component_type_checking_test/type_inheritance/abstract_inheritance/abstract.dart';
import 'component_type_checking_test/type_inheritance/abstract_inheritance/extendedtype.dart';
import 'component_type_checking_test/type_inheritance/parent.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ main() {

group('\$PropKeys (ungenerated)', () {
setUpAll(() {
expect(() => const $PropKeys(Null), isNot(throwsNoSuchMethodError),
expect(() => const $PropKeys(Null), returnsNormally,
reason: 'Instanitating a const \$PropKeys should not have thrown an error. '
'Ensure that the over_react transformer is NOT running for this test file.'
);
Expand All @@ -164,7 +164,7 @@ main() {

group('\$Props (ungenerated)', () {
setUpAll(() {
expect(() => const $Props(Null), isNot(throwsNoSuchMethodError),
expect(() => const $Props(Null), returnsNormally,
reason: 'Instanitating a const \$Props should not have thrown an error. '
'Ensure that the over_react transformer is NOT running for this test file.'
);
Expand Down
Loading

0 comments on commit a0af902

Please sign in to comment.