Skip to content
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
4 changes: 3 additions & 1 deletion packages/material-ui-utils/src/requirePropFactory.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export default function requirePropFactory(componentNameInError: string): any;
import * as React from 'react';

export default function requirePropFactory(componentNameInError: string, Component?: React.ComponentType): any;
11 changes: 9 additions & 2 deletions packages/material-ui-utils/src/requirePropFactory.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default function requirePropFactory(componentNameInError) {
export default function requirePropFactory(componentNameInError, Component) {
if (process.env.NODE_ENV === 'production') {
return () => null;
}
Expand All @@ -12,14 +12,21 @@ export default function requirePropFactory(componentNameInError) {
) => {
const propFullNameSafe = propFullName || propName;

const defaultTypeChecker = Component?.propTypes?.[propFullNameSafe];
let defaultTypeCheckerResult = null;

if(defaultTypeChecker) {
defaultTypeCheckerResult = defaultTypeChecker(props, propName, componentName, location, propFullName)
}

if (typeof props[propName] !== 'undefined' && !props[requiredProp]) {
return new Error(
`The prop \`${propFullNameSafe}\` of ` +
`\`${componentNameInError}\` can only be used together with the \`${requiredProp}\` prop.`,
);
}

return null;
return defaultTypeCheckerResult;
};
return requireProp;
}
23 changes: 23 additions & 0 deletions packages/material-ui-utils/src/requirePropFactory.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from 'chai';
import { spy } from 'sinon';
import requirePropFactory from './requirePropFactory';

describe('requirePropFactory', () => {
Expand Down Expand Up @@ -83,6 +84,28 @@ describe('requirePropFactory', () => {
});
});
});

it('should chain the proptypes with the default prop types coming from the component', () => {
const Test = () => null;
const mock = spy();
Test.propTypes = {
test: mock,
};

const props = {};
const propName = 'test';
props[propName] = true;

const requireProp = requirePropFactory('Test', Test);

const result = requireProp('otherProp');
result(props, propName, undefined, undefined, undefined);

expect(mock.callCount).to.equal(1);
expect(mock.args[0]).to.deep.equal(
[props, propName, undefined, undefined, undefined],
);
});
});
});
});
2 changes: 1 addition & 1 deletion packages/material-ui/src/Grid/Grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ Grid.propTypes = {
};

if (process.env.NODE_ENV !== 'production') {
const requireProp = requirePropFactory('Grid');
const requireProp = requirePropFactory('Grid', Grid);
// eslint-disable-next-line no-useless-concat
Grid['propTypes' + ''] = {
// eslint-disable-next-line react/forbid-foreign-prop-types
Expand Down