(props, defaultProps = {}, options = {})
| 60 | * @returns {string} Formatted props string for JSX |
| 61 | */ |
| 62 | export function generatePropsString(props, defaultProps = {}, options = {}) { |
| 63 | const { exclude = [], include = [], indent = 2 } = options; |
| 64 | const indentStr = ' '.repeat(indent); |
| 65 | |
| 66 | const propsEntries = Object.entries(props).filter(([key, value]) => { |
| 67 | // Always exclude certain internal props |
| 68 | if (exclude.includes(key)) return false; |
| 69 | |
| 70 | // Always include specified props |
| 71 | if (include.includes(key)) return true; |
| 72 | |
| 73 | // Skip if value matches default |
| 74 | if (JSON.stringify(value) === JSON.stringify(defaultProps[key])) { |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | // Skip undefined/null |
| 79 | if (value === undefined || value === null) return false; |
| 80 | |
| 81 | return true; |
| 82 | }); |
| 83 | |
| 84 | if (propsEntries.length === 0) { |
| 85 | return ''; |
| 86 | } |
| 87 | |
| 88 | return propsEntries |
| 89 | .map(([key, value]) => { |
| 90 | const formattedValue = formatPropValue(value, key); |
| 91 | |
| 92 | // For boolean true, just show the prop name |
| 93 | if (typeof value === 'boolean' && value === true) { |
| 94 | return `${indentStr}${key}`; |
| 95 | } |
| 96 | |
| 97 | return `${indentStr}${key}=${formattedValue}`; |
| 98 | }) |
| 99 | .join('\n'); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Injects dynamic props into a usage code template. |
no test coverage detected