From c28c8b1afe662acbb843dc8015d34ee7b0386731 Mon Sep 17 00:00:00 2001 From: Alexey Date: Wed, 28 Jun 2017 22:33:07 +1000 Subject: [PATCH 1/3] Fix #1305 by passing parent PropVal properties into child elements for arrays and objects. --- addons/info/src/components/PropVal.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/addons/info/src/components/PropVal.js b/addons/info/src/components/PropVal.js index 13901ab87fef..c02e3014a686 100644 --- a/addons/info/src/components/PropVal.js +++ b/addons/info/src/components/PropVal.js @@ -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}`] = ; + items[`n${i}`] = ; items[`c${i}`] = ', '; }); if (val.length > maxPropArrayLength) { @@ -55,13 +56,14 @@ function previewArray(val, maxPropArrayLength) { ); } -function previewObject(val, maxPropObjectKeys) { +function previewObject(props) { + const { val, maxPropObjectKeys } = props; const names = Object.keys(val); const items = {}; names.slice(0, maxPropObjectKeys).forEach((name, i) => { items[`k${i}`] = {name}; items[`c${i}`] = ': '; - items[`v${i}`] = ; + items[`v${i}`] = ; items[`m${i}`] = ', '; }); if (names.length > maxPropObjectKeys) { @@ -93,7 +95,7 @@ export default function PropVal(props) { } else if (typeof val === 'boolean') { content = {`${val}`}; } else if (Array.isArray(val)) { - content = previewArray(val, maxPropArrayLength); + content = previewArray(props); } else if (typeof val === 'function') { content = {val.name ? `${val.name}()` : 'anonymous()'}; } else if (!val) { @@ -107,7 +109,7 @@ export default function PropVal(props) { ); } else { - content = previewObject(val, maxPropObjectKeys); + content = previewObject(props); } if (!braceWrap) return content; From acbf148b35c81ccd1c0fc61be64df5428d4181df Mon Sep 17 00:00:00 2001 From: Alexey Date: Wed, 28 Jun 2017 23:23:25 +1000 Subject: [PATCH 2/3] Eslint fixes --- addons/info/src/components/PropVal.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/addons/info/src/components/PropVal.js b/addons/info/src/components/PropVal.js index c02e3014a686..7f83c21a9f3e 100644 --- a/addons/info/src/components/PropVal.js +++ b/addons/info/src/components/PropVal.js @@ -37,11 +37,11 @@ const valueStyles = { }, }; -function previewArray(props) { - const { val, maxPropArrayLength } = props; +function previewArray(val, maxPropObjectKeys, maxPropArrayLength, maxPropStringLength) { + const restProps = { maxPropObjectKeys, maxPropArrayLength, maxPropStringLength }; const items = {}; val.slice(0, maxPropArrayLength).forEach((item, i) => { - items[`n${i}`] = ; + items[`n${i}`] = ; items[`c${i}`] = ', '; }); if (val.length > maxPropArrayLength) { @@ -56,14 +56,14 @@ function previewArray(props) { ); } -function previewObject(props) { - const { val, maxPropObjectKeys } = props; +function previewObject(val, maxPropObjectKeys, maxPropArrayLength, maxPropStringLength) { + const restProps = { maxPropObjectKeys, maxPropArrayLength, maxPropStringLength }; const names = Object.keys(val); const items = {}; names.slice(0, maxPropObjectKeys).forEach((name, i) => { items[`k${i}`] = {name}; items[`c${i}`] = ': '; - items[`v${i}`] = ; + items[`v${i}`] = ; items[`m${i}`] = ', '; }); if (names.length > maxPropObjectKeys) { @@ -95,7 +95,7 @@ export default function PropVal(props) { } else if (typeof val === 'boolean') { content = {`${val}`}; } else if (Array.isArray(val)) { - content = previewArray(props); + content = previewArray(val, maxPropObjectKeys, maxPropArrayLength, maxPropStringLength); } else if (typeof val === 'function') { content = {val.name ? `${val.name}()` : 'anonymous()'}; } else if (!val) { @@ -109,7 +109,7 @@ export default function PropVal(props) { ); } else { - content = previewObject(props); + content = previewObject(val, maxPropObjectKeys, maxPropArrayLength, maxPropStringLength); } if (!braceWrap) return content; From 3cb2585012fc7a09f12014bdecabfa9bb74dfc8b Mon Sep 17 00:00:00 2001 From: Alexey Shpakov Date: Thu, 29 Jun 2017 20:19:02 +1000 Subject: [PATCH 3/3] Weird refactor to make eslint and codebeat happy. Makes me unhappy :'(. --- addons/info/src/components/PropVal.js | 30 +++++++++++++++++---------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/addons/info/src/components/PropVal.js b/addons/info/src/components/PropVal.js index 7f83c21a9f3e..d00eae830a17 100644 --- a/addons/info/src/components/PropVal.js +++ b/addons/info/src/components/PropVal.js @@ -37,11 +37,11 @@ const valueStyles = { }, }; -function previewArray(val, maxPropObjectKeys, maxPropArrayLength, maxPropStringLength) { - const restProps = { maxPropObjectKeys, maxPropArrayLength, maxPropStringLength }; +function previewArray(props) { + const { val, maxPropArrayLength } = props; const items = {}; val.slice(0, maxPropArrayLength).forEach((item, i) => { - items[`n${i}`] = ; + items[`n${i}`] = ; items[`c${i}`] = ', '; }); if (val.length > maxPropArrayLength) { @@ -56,14 +56,19 @@ function previewArray(val, maxPropObjectKeys, maxPropArrayLength, maxPropStringL ); } -function previewObject(val, maxPropObjectKeys, maxPropArrayLength, maxPropStringLength) { - const restProps = { maxPropObjectKeys, maxPropArrayLength, maxPropStringLength }; +previewArray.propTypes = { + 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}`] = {name}; items[`c${i}`] = ': '; - items[`v${i}`] = ; + items[`v${i}`] = ; items[`m${i}`] = ', '; }); if (names.length > maxPropObjectKeys) { @@ -78,8 +83,13 @@ function previewObject(val, maxPropObjectKeys, maxPropArrayLength, maxPropString ); } +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; @@ -95,7 +105,7 @@ export default function PropVal(props) { } else if (typeof val === 'boolean') { content = {`${val}`}; } else if (Array.isArray(val)) { - content = previewArray(val, maxPropObjectKeys, maxPropArrayLength, maxPropStringLength); + content = previewArray(props); } else if (typeof val === 'function') { content = {val.name ? `${val.name}()` : 'anonymous()'}; } else if (!val) { @@ -109,7 +119,7 @@ export default function PropVal(props) { ); } else { - content = previewObject(val, maxPropObjectKeys, maxPropArrayLength, maxPropStringLength); + content = previewObject(props); } if (!braceWrap) return content; @@ -119,7 +129,5 @@ export default function PropVal(props) { PropVal.propTypes = { val: PropTypes.any.isRequired, // eslint-disable-line - maxPropObjectKeys: PropTypes.number.isRequired, - maxPropArrayLength: PropTypes.number.isRequired, maxPropStringLength: PropTypes.number.isRequired, };