Skip to content
Open
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
3 changes: 3 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ rules:
# disable no-unresolved since we don't install the deps
# of our react example app
import/no-unresolved: off
react/require-default-props:
- warn
- functions: defaultArguments
jest/expect-expect:
- warn
- assertFunctionNames:
Expand Down
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,8 @@ class TitleComponent extends Component {
children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
};

static defaultProps = {
children: null,
};

render() {
const {title, children} = this.props;
const {title, children = null} = this.props;
return (
<Overridable id="TitleComponent.container" title={title}>
<>
Expand Down
12 changes: 2 additions & 10 deletions src/overridable.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function parametrize(Component, extraProps) {
/**
* React component to enable overriding children when rendering.
*/
function Overridable({id, children, ...restProps}) {
function Overridable({id = null, children = null, ...restProps}) {
const overriddenComponents = useContext(OverridableContext);
const child = children ? React.Children.only(children) : null;
const childProps = child ? child.props : {};
Expand All @@ -62,26 +62,18 @@ Overridable.propTypes = {
id: PropTypes.string,
};

Overridable.defaultProps = {
id: null,
children: null,
};

/**
* High-order component to override an existing React component and provide a new component instead.
*/
Overridable.component = (id, Component) => {
const Overridden = ({children, ...props}) => {
const Overridden = ({children = null, ...props}) => {
const overriddenComponents = useContext(OverridableContext);
const overriddenComponent = overriddenComponents[id];
return React.createElement(overriddenComponent || Component, props, children);
};
Overridden.propTypes = {
children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
};
Overridden.defaultProps = {
children: null,
};
const name = Component.displayName || Component.name;
Overridden.displayName = `Overridable(${name})`;
Overridden.originalComponent = Component;
Expand Down
7 changes: 1 addition & 6 deletions src/overridable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,12 @@
class ExampleComponent extends Component {
static propTypes = {
title: PropTypes.string.isRequired,
color: PropTypes.oneOf(['blue', 'green', 'red']),

Check warning on line 9 in src/overridable.test.js

View workflow job for this annotation

GitHub Actions / Tests

propType "color" is not required, but has no corresponding defaultProps declaration
children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),

Check warning on line 10 in src/overridable.test.js

View workflow job for this annotation

GitHub Actions / Tests

propType "children" is not required, but has no corresponding defaultProps declaration
};

static defaultProps = {
color: 'blue',
children: null,
};

render() {
const {title, color, children} = this.props;
const {title, color = 'blue', children = null} = this.props;
return (
<Overridable id="ExampleComponent.container" cmpTitle={title} cmpColor={color}>
<>
Expand Down
6 changes: 1 addition & 5 deletions src/store.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@
class ExampleComponent extends Component {
static propTypes = {
title: PropTypes.string.isRequired,
children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),

Check warning on line 9 in src/store.test.js

View workflow job for this annotation

GitHub Actions / Tests

propType "children" is not required, but has no corresponding defaultProps declaration
};

static defaultProps = {
children: null,
};

render() {
const {title, children} = this.props;
const {title, children = null} = this.props;
return (
<>
<h1>{title}</h1>
Expand Down