Skip to content
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

Fixed prop type validation errors in info addon #1374

Closed
wants to merge 13 commits into from
26 changes: 19 additions & 7 deletions addons/info/src/components/PropVal.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ const valueStyles = {
},
};

function previewArray(val, maxPropArrayLength) {
function previewArray(props) {
const { val, maxPropArrayLength } = props;
const items = {};
val.slice(0, maxPropArrayLength).forEach((item, i) => {
items[`n${i}`] = <PropVal val={item} />;
items[`n${i}`] = <PropVal {...props} val={item} />;
items[`c${i}`] = ', ';
});
if (val.length > maxPropArrayLength) {
Expand All @@ -51,13 +52,19 @@ function previewArray(val, maxPropArrayLength) {
return <span style={valueStyles.array}>[{createFragment(items)}]</span>;
}

function previewObject(val, maxPropObjectKeys) {
previewArray.propTypes = {
Copy link
Member

Choose a reason for hiding this comment

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

previewArray is never rendered as a component, it's only called as a function. So looks like those propTypes aren't really checked at any point. You should either remove them, or capitalize the name (PreviewArray) and replace previewArray(props) below with <PreviewArray {...props} />.

If you decide to do the latter, please note that val should rather be PropTypes.array, and don't disable all of the eslint rules — only the particular ones (// eslint-disable react/forbid-prop-types)

val: PropTypes.any.isRequired, // eslint-disable-line
maxPropArrayLength: PropTypes.number.isRequired,
};

function previewObject(props) {
const { val, maxPropObjectKeys } = props;
const names = Object.keys(val);
const items = {};
names.slice(0, maxPropObjectKeys).forEach((name, i) => {
items[`k${i}`] = <span style={valueStyles.attr}>{name}</span>;
items[`c${i}`] = ': ';
items[`v${i}`] = <PropVal val={val[name]} />;
items[`v${i}`] = <PropVal {...props} val={val[name]} />;
items[`m${i}`] = ', ';
});
if (names.length > maxPropObjectKeys) {
Expand All @@ -74,8 +81,13 @@ function previewObject(val, maxPropObjectKeys) {
);
}

previewObject.propTypes = {
val: PropTypes.any.isRequired, // eslint-disable-line
maxPropObjectKeys: PropTypes.number.isRequired,
};

export default function PropVal(props) {
const { maxPropObjectKeys, maxPropArrayLength, maxPropStringLength } = props;
const { maxPropStringLength } = props;
let val = props.val;
let braceWrap = true;
let content = null;
Expand All @@ -91,7 +103,7 @@ export default function PropVal(props) {
} else if (typeof val === 'boolean') {
content = <span style={valueStyles.bool}>{`${val}`}</span>;
} else if (Array.isArray(val)) {
content = previewArray(val, maxPropArrayLength);
content = previewArray(props);
} else if (typeof val === 'function') {
content = <span style={valueStyles.func}>{val.name ? `${val.name}()` : 'anonymous()'}</span>;
} else if (!val) {
Expand All @@ -105,7 +117,7 @@ export default function PropVal(props) {
</span>
);
} else {
content = previewObject(val, maxPropObjectKeys);
content = previewObject(props);
}

if (!braceWrap) return content;
Expand Down