import React from 'react'; import createFragment from 'react-addons-create-fragment'; const valueStyles = { func: { color: '#170', }, attr: { color: '#666', }, object: { color: '#666', }, array: { color: '#666', }, number: { color: '#a11', }, string: { color: '#22a', wordBreak: 'break-word', }, bool: { color: '#a11', }, empty: { color: '#777', }, }; function previewArray(val) { const items = {}; val.slice(0, 3).forEach((item, i) => { items[`n${i}`] = ; items[`c${i}`] = ', '; }); if (val.length > 3) { items.last = '…'; } else { delete items[`c${val.length - 1}`]; } return ( [{createFragment(items)}] ); } function previewObject(val) { const names = Object.keys(val); const items = {}; names.slice(0, 3).forEach((name, i) => { items[`k${i}`] = {name}; items[`c${i}`] = ': '; items[`v${i}`] = ; items[`m${i}`] = ', '; }); if (names.length > 3) { items.rest = '…'; } else { delete items[`m${names.length - 1}`]; } return ( {'{'}{createFragment(items)}{'}'} ); } function previewProp(val) { let braceWrap = true; let content = null; if (typeof val === 'number') { content = {val}; } else if (typeof val === 'string') { if (val.length > 50) { val = `${val.slice(0, 50)}…`; } content = "{val}"; braceWrap = false; } else if (typeof val === 'boolean') { content = {`${val}`}; } else if (Array.isArray(val)) { content = previewArray(val); } else if (typeof val === 'function') { content = {val.name ? `${val.name}()` : 'anonymous()'}; } else if (!val) { content = {`${val}`}; } else if (typeof val !== 'object') { content = ; } else if (React.isValidElement(val)) { content = ( {`<${val.type.displayName || val.type.name || val.type} />`} ); } else { content = previewObject(val); } if (!braceWrap) return content; return {content}; } export default class PropVal extends React.Component { render() { return previewProp(this.props.val); } } module.exports = PropVal;