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

UIP-2529 Add validateProps method to UiComponent #88

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 23 additions & 2 deletions lib/src/component_declaration/component_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,27 @@ abstract class UiComponent<TProps extends UiProps> extends react.Component imple
);
}

/// Throws a [PropError] if [appliedProps] are invalid.
///
/// This is called automatically with the latest props available during [componentWillReceiveProps] and
/// [componentWillMount], and can also be called manually for custom validation.
///
/// Override with a custom implementation to easily add validation (and don't forget to call super!)
///
/// @mustCallSuper
/// void validateProps(Map appliedProps) {
/// super.validateProps(appliedProps);
///
/// var tProps = typedPropsFactory(appliedProps);
/// if (tProps.items.length.isOdd) {
/// throw new PropError.value(tProps.items, 'items', 'must have an even number of items, because reasons');
/// }
/// }
@mustCallSuper
void validateProps(Map appliedProps) {
validateRequiredProps(appliedProps);
}

void validateRequiredProps(Map appliedProps) {
consumedProps?.forEach((ConsumedProps consumedProps) {
consumedProps.props.forEach((PropDescriptor prop) {
Expand All @@ -154,13 +175,13 @@ abstract class UiComponent<TProps extends UiProps> extends react.Component imple
@override
@mustCallSuper
void componentWillReceiveProps(Map newProps) {
validateRequiredProps(newProps);
validateProps(newProps);
}

@override
@mustCallSuper
void componentWillMount() {
validateRequiredProps(props);
validateProps(props);
}

@override
Expand Down
43 changes: 43 additions & 0 deletions test/over_react/component_declaration/component_base_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,42 @@ main() {
expect(classes.toClassNameBlacklist(), equals('blacklist-1'));
});

group('calls validateProps in', () {
test('componentWillMount', () {
var calls = [];
var appliedProps;
var initialProps = {
'onValidateProps': (Map propsMap) {
appliedProps = propsMap;
calls.add('onValidateProps');
},
};
component.props = initialProps;

component.componentWillMount();

expect(calls, ['onValidateProps']);
expect(appliedProps, initialProps);
});

test('componentWillReceiveProps', () {
var calls = [];
var appliedProps;
var newProps = {'key': 'value'};
component.props = {
'onValidateProps': (Map propsMap) {
appliedProps = propsMap;
calls.add('onValidateProps');
},
};

component.componentWillReceiveProps(newProps);

expect(calls, ['onValidateProps']);
expect(appliedProps, newProps);
});
});

group('on unmount', () {
TestComponentComponent component;
ReactElement instance;
Expand Down Expand Up @@ -920,6 +956,13 @@ class TestComponentComponent extends UiComponent<TestComponentProps> {

@override
TestComponentProps typedPropsFactory(Map propsMap) => new TestComponentProps(propsMap);

@override
void validateProps(Map appliedProps) {
super.validateProps(appliedProps);

if (props['onValidateProps'] != null) props['onValidateProps'](appliedProps);
}
}


Expand Down