Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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;
type Component = (arg: any) => any & { propTypes?: object };

export default function requirePropFactory(componentNameInError: string, Component?: Component): any;
12 changes: 10 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,22 @@ export default function requirePropFactory(componentNameInError) {
) => {
const propFullNameSafe = propFullName || propName;

const defaultPropType = Component?.propTypes?.[propFullNameSafe];
let defaultPropTypeVal = null;

if(defaultPropType) {
console.log("Default prop type existed");
defaultPropTypeVal = defaultPropType(props, propName, componentName, location, propFullName)
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Properties in Component.propTypes (e.g. PropTypes.string) are referred to as "type checkers" or "validators". And then you could name their return value validatorResult or typeCheckerResult.

The current naming seems overly generic to me. Especially looking at the return statement "return defaultPropTypeVal".

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed to defaultTypeChecker & defaultTypeCheckerResult. I use the default prefix, as we are not defining a new type checking for the property

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 defaultPropTypeVal;
};
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