-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check that minimum required props are passed to controllers
- Loading branch information
Showing
5 changed files
with
50 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
packages/ra-core/src/controller/checkMinimumRequiredProps.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import React from 'react'; | ||
|
||
const checkMinimumRequiredProps = (displayName, requiredProps) => WrappedComponent => (props) => { | ||
const propNames = Object.keys(props); | ||
const missingProps = requiredProps.filter(prop => !propNames.includes(prop)); | ||
|
||
if (missingProps.length > 0) { | ||
console.error( | ||
`<${displayName}> component is not properly configured, some essential props are missing. | ||
Be sure to pass the props from the parent. Example: | ||
const My${displayName} = props => ( | ||
<${displayName} {...props}></${displayName}> | ||
); | ||
The missing props are: ${missingProps.join(', ')}`); | ||
return null; | ||
} | ||
|
||
return <WrappedComponent {...props} />; | ||
}; | ||
|
||
export default checkMinimumRequiredProps; |