-
Notifications
You must be signed in to change notification settings - Fork 58
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -124,6 +124,16 @@ abstract class UiComponent<TProps extends UiProps> extends react.Component { | |
); | ||
} | ||
|
||
/// Validation method called during [componentWillReceiveProps], and [componentWillMount]. | ||
/// | ||
/// Use this as an opportunity validate props during the correct lifecycle of the component. | ||
@mustCallSuper | ||
void validateProps([Map appliedProps]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be a required positional parameter, so that consumers don't have to null-coalesce the argument in overrides. |
||
appliedProps ??= props; | ||
|
||
validateRequiredProps(appliedProps); | ||
} | ||
|
||
void validateRequiredProps(Map appliedProps) { | ||
consumedProps?.forEach((ConsumedProps consumedProps) { | ||
consumedProps.props.forEach((PropDescriptor prop) { | ||
|
@@ -147,13 +157,13 @@ abstract class UiComponent<TProps extends UiProps> extends react.Component { | |
@override | ||
@mustCallSuper | ||
void componentWillReceiveProps(Map newProps) { | ||
validateRequiredProps(newProps); | ||
validateProps(newProps); | ||
} | ||
|
||
@override | ||
@mustCallSuper | ||
void componentWillMount() { | ||
validateRequiredProps(props); | ||
validateProps(); | ||
} | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -623,6 +623,30 @@ main() { | |
expect(classes.toClassName(), equals('class-1')); | ||
expect(classes.toClassNameBlacklist(), equals('blacklist-1')); | ||
}); | ||
|
||
group('calls validateProps in', () { | ||
test('componentWillMount', () { | ||
var calls = []; | ||
component.props = { | ||
'onValidateProps': () { calls.add('onValidateProps'); }, | ||
}; | ||
|
||
component.componentWillMount(); | ||
|
||
expect(calls, ['onValidateProps']); | ||
}); | ||
|
||
test('componentWillReceiveProps', () { | ||
var calls = []; | ||
component.props = { | ||
'onValidateProps': () { calls.add('onValidateProps'); }, | ||
}; | ||
|
||
component.componentWillReceiveProps({}); | ||
|
||
expect(calls, ['onValidateProps']); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These should also test that the correct props maps are passed into them |
||
}); | ||
}); | ||
|
||
group('UiStatefulComponent', () { | ||
|
@@ -799,6 +823,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'](); | ||
} | ||
} | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#nit I think this comment could be a little more descriptive.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea 👍