Skip to content

Commit

Permalink
Use ReactNode typedef where appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronlademann-wf committed Mar 1, 2024
1 parent 86bfdaf commit 5e84679
Show file tree
Hide file tree
Showing 20 changed files with 94 additions and 55 deletions.
2 changes: 1 addition & 1 deletion lib/react_dom.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import 'package:react/react_dom.dart' as react_dom show render, unmountComponent
/// Use [unmountComponentAtNode] to unmount the instance.
///
/// > Proxies [react_dom.render].
dynamic render(/*ReactNode*/ dynamic element, Element mountNode) {
dynamic render(ReactNode element, Element mountNode) {
return react_dom.render(element, mountNode);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/src/component/_deprecated/error_boundary_mixins.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ abstract class _$ErrorBoundaryPropsMixin implements UiProps {
/// component tree that crashed.
///
/// > Related: [onComponentIsUnrecoverable], [onComponentDidCatch]
ReactElement Function(/*Error||Exception*/dynamic error, ReactErrorInfo? info)? fallbackUIRenderer;
ReactNode Function(/*Error||Exception*/dynamic error, ReactErrorInfo? info)? fallbackUIRenderer;

/// The amount of time that is "acceptable" between consecutive identical errors thrown from a component
/// within the tree wrapped by this [ErrorBoundary].
Expand Down Expand Up @@ -361,7 +361,7 @@ mixin ErrorBoundaryMixin<T extends ErrorBoundaryPropsMixin, S extends ErrorBound
}

// [2.2]
ReactElement? _renderStringDomAfterUnrecoverableErrors(_, __) {
ReactNode _renderStringDomAfterUnrecoverableErrors(_, __) {
return (Dom.div()
..key = 'ohnoes'
..addTestId('ErrorBoundary.unrecoverableErrorInnerHtmlContainerNode')
Expand Down

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

2 changes: 1 addition & 1 deletion lib/src/component/error_boundary.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ mixin ErrorBoundaryProps on UiProps {
/// component tree that crashed.
///
/// > Related: [onComponentIsUnrecoverable], [onComponentDidCatch]
ReactElement? Function(/*Error||Exception*/dynamic error, ReactErrorInfo? info)? fallbackUIRenderer;
ReactNode Function(/*Error||Exception*/dynamic error, ReactErrorInfo? info)? fallbackUIRenderer;

/// The amount of time that is "acceptable" between consecutive identical errors thrown from a component
/// within the tree wrapped by this [ErrorBoundary].
Expand Down
6 changes: 3 additions & 3 deletions lib/src/component/error_boundary.over_react.g.dart

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

2 changes: 1 addition & 1 deletion lib/src/component/error_boundary_recoverable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ class RecoverableErrorBoundaryComponent<T extends RecoverableErrorBoundaryProps,
}

// [2.2]
ReactElement? _renderStringDomAfterUnrecoverableErrors(_, __) {
ReactNode _renderStringDomAfterUnrecoverableErrors(_, __) {
return (Dom.div()
..key = 'ohnoes'
..addTestId('ErrorBoundary.unrecoverableErrorInnerHtmlContainerNode')
Expand Down
8 changes: 4 additions & 4 deletions lib/src/component/prop_mixins.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/// Various prop related mixins to be used with `UiComponent` descendants.
library over_react.prop_mixins;

import 'package:over_react/over_react.dart' show AriaPropsMapView, AriaPropsMixin, DomProps, PropsMeta;
import 'package:over_react/over_react.dart' show AriaPropsMapView, AriaPropsMixin, DomProps, PropsMeta, ReactNode;
// Must import these consts because they are used in the transformed code.
// ignore: deprecated_member_use, unused_shown_name
import 'package:over_react/over_react.dart' show PropDescriptor, ConsumedProps, PropsMeta;
Expand Down Expand Up @@ -46,10 +46,10 @@ abstract class _$ReactPropsMixin {

// This private field is namespaced to avoid colliding with other classes.
@Accessor(key: 'children')
dynamic _raw$ReactProps$children;
ReactNode _raw$ReactProps$children;

/// The children that were passed in to this component when it was built.
List<dynamic>? get children {
List<ReactNode>? get children {
final value = _raw$ReactProps$children;

// Most common case; Dart components should all have List children
Expand All @@ -68,7 +68,7 @@ abstract class _$ReactPropsMixin {
return [value];
}

set children(List<dynamic>? value) => _raw$ReactProps$children = value;
set children(List<ReactNode>? value) => _raw$ReactProps$children = value;

/// A String that differentiates a component from its siblings.
///
Expand Down
10 changes: 5 additions & 5 deletions lib/src/component/prop_mixins.over_react.g.dart

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

4 changes: 2 additions & 2 deletions lib/src/component/ref_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ Ref<T?> createRef<T>() => react_interop.createRef();
/// _$Foo2Config, // ignore: undefined_identifier
/// );
UiFactory<TProps> uiForwardRef<TProps extends bh.UiProps>(
dynamic Function(TProps props, dynamic ref) functionComponent, dynamic _config) {
ReactNode Function(TProps props, dynamic ref) functionComponent, dynamic _config) {
ArgumentError.checkNotNull(_config, '_config');
if (_config is! UiFactoryConfig<TProps>) {
Expand All @@ -231,7 +231,7 @@ UiFactory<TProps> uiForwardRef<TProps extends bh.UiProps>(
// this will be an empty string.
final displayName = config.displayName ?? getFunctionName(functionComponent);
dynamic _uiFunctionWrapper(JsBackedMap props, dynamic ref) {
ReactNode _uiFunctionWrapper(JsBackedMap props, dynamic ref) {
return functionComponent(propsFactory!.jsMap(props), ref);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/component/suspense_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ mixin SuspensePropsMixin on UiProps {
/// The actual UI you intend to render. If children suspends while rendering, the Suspense boundary will
/// switch to rendering fallback.
@override
/*ReactNode*/ List<dynamic>? get children;
List<ReactNode>? get children;

/// An alternate UI to render in place of the actual UI if it has not finished loading. Any valid React node is
/// accepted, though in practice, a fallback is a lightweight placeholder view, such as a loading spinner or skeleton.
/// Suspense will automatically switch to fallback when children suspends, and back to children when the data is ready.
/// If fallback suspends while rendering, it will activate the closest parent Suspense boundary.
/*ReactNode*/ dynamic fallback;
ReactNode fallback;
}
6 changes: 3 additions & 3 deletions lib/src/component/suspense_component.over_react.g.dart

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

2 changes: 2 additions & 0 deletions lib/src/component/with_transition.dart
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ class WithTransitionComponent extends UiStatefulComponent2<WithTransitionProps,
render() {
assert(_hasSingleValidChild(props));

// ok to ignore because of the above assert
// ignore: cast_nullable_to_non_nullable
final childElement = props.children!.single as ReactElement;
final childProps = domProps(getProps(childElement));
final phaseProps = props.childPropsByPhase![state.$transitionPhase] ?? const {};
Expand Down
4 changes: 2 additions & 2 deletions lib/src/component_declaration/component_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ abstract class UiProps extends MapBase
}

/// Returns a new component with this builder's [props] and the specified [children].
ReactElement build([dynamic children]) {
ReactElement build([ReactNode children]) {
assert(_validateChildren(children));
_sharedAsserts();

Expand Down Expand Up @@ -643,7 +643,7 @@ abstract class UiProps extends MapBase

/// Validates that no [children] are instances of [UiProps], and prints a helpful message for a better debugging
/// experience.
bool _validateChildren(dynamic children) {
bool _validateChildren(ReactNode children) {
// Should not validate non-list iterables to avoid more than one iteration.
if (children != null && (children is! Iterable || children is List)) {
final childrenList = children is List ? children : [children];
Expand Down
2 changes: 1 addition & 1 deletion lib/src/component_declaration/function_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export 'component_type_checking.dart'
/// Learn more: <https://reactjs.org/docs/components-and-props.html#function-and-class-components>.
// TODO: right now only top level factory declarations will generate props configs.
UiFactory<TProps> uiFunction<TProps extends UiProps>(
dynamic Function(TProps props) functionComponent,
ReactNode Function(TProps props) functionComponent,
dynamic _config,
) {
ArgumentError.checkNotNull(_config, '_config');
Expand Down
2 changes: 1 addition & 1 deletion lib/src/over_react_redux/redux_multi_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class ReduxMultiProviderComponent

@override
render() {
dynamic content = props.children;
ReactNode content = props.children;
props.storesByContext.forEach((context, store) {
content = (ReduxProvider()
..store = store
Expand Down
1 change: 1 addition & 0 deletions lib/src/util/context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

import 'package:meta/meta.dart';
import 'package:over_react/over_react.dart';
import 'package:over_react/src/component_declaration/builder_helpers.dart';
import 'package:over_react/src/util/prop_key_util.dart';

Expand Down
2 changes: 1 addition & 1 deletion lib/src/util/react_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ abstract class UiPropsMapView extends UiProps {
Map get componentDefaultProps => throw UnimplementedError('@PropsMixin instances do not implement defaultProps');

@override
ReactElement build([dynamic children]) =>
ReactElement build([ReactNode children]) =>
throw UnimplementedError('@PropsMixin instances do not implement build');

@override
Expand Down
10 changes: 5 additions & 5 deletions lib/src/util/react_wrappers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ Map getProps(dynamic/* ReactElement|ReactComponent */ instance, {bool traverseWr
///
/// This method simply wraps react.findDOMNode with strong typing for the return value
/// (and for the function itself, which is declared using `var` in react-dart).
Element? findDomNode(dynamic instance) => react_dom.findDOMNode(instance) as Element?;
Element? findDomNode(ReactNode instance) => react_dom.findDOMNode(instance) as Element?;

/// Returns a portal that renders [children] into a [container].
///
Expand All @@ -204,7 +204,7 @@ Element? findDomNode(dynamic instance) => react_dom.findDOMNode(instance) as Ele
/// [children] can be any renderable React child, such as a [ReactElement], [String], or fragment.
///
/// See: <https://reactjs.org/docs/portals.html>
ReactPortal createPortal(dynamic children, Element container) => ReactDom.createPortal(children, container);
ReactPortal createPortal(ReactNode children, Element container) => ReactDom.createPortal(children, container);

/// Dart wrapper for React.isValidElement.
///
Expand All @@ -223,7 +223,7 @@ bool isDomElement(dynamic instance) {
/// Returns whether [instance] is a composite [ReactComponent].
///
/// __Not for external use.__
bool _isCompositeComponent(Object? instance) {
bool _isCompositeComponent(ReactNode instance) {
return instance != null && getProperty(instance, 'isReactComponent') != null;
}

Expand All @@ -240,7 +240,7 @@ bool _isCompositeComponent(Object? instance) {
/// * Children are likewise copied and potentially overwritten with [newChildren] as expected.
/// * For JS components, a JS copy of [newProps] is returned, since React will merge the props without any special handling.
/// If these values might contain event handlers
dynamic preparePropsChangeset(ReactElement element, Map? newProps, [Iterable? newChildren]) {
dynamic preparePropsChangeset(ReactElement element, Map? newProps, [ReactNode newChildren]) {
final type = element.type;
final dartComponentVersion = ReactDartComponentVersion.fromType(type); // ignore: invalid_use_of_protected_member

Expand Down Expand Up @@ -294,7 +294,7 @@ external ReactElement _cloneElement(element, [props, children]);
/// > Unlike React.addons.cloneWithProps, key and ref from the original element will be preserved.
/// > There is no special behavior for merging any props (unlike cloneWithProps).
/// > See the [v0.13 RC2 blog post](https://facebook.github.io/react/blog/2015/03/03/react-v0.13-rc2.html) for additional details.
ReactElement cloneElement(ReactElement element, [Map? props, Iterable? children]) {
ReactElement cloneElement(ReactElement element, [Map? props, ReactNode children]) {
ArgumentError.checkNotNull(element, 'element');

var propsChangeset = preparePropsChangeset(element, props, children);
Expand Down
Loading

0 comments on commit 5e84679

Please sign in to comment.