(componentName, importPath, props, defaultProps = {}, options = {})
| 146 | * @returns {string} Complete usage example code |
| 147 | */ |
| 148 | export function createUsageExample(componentName, importPath, props, defaultProps = {}, options = {}) { |
| 149 | const { exclude = ['key', 'ref'], children = null, callbacks = {} } = options; |
| 150 | |
| 151 | const propsString = generatePropsString(props, defaultProps, { exclude, indent: 2 }); |
| 152 | |
| 153 | // Generate callback declarations |
| 154 | const callbackDeclarations = Object.entries(callbacks) |
| 155 | .map(([name, body]) => { |
| 156 | return `const ${name} = () => {\n ${body}\n};`; |
| 157 | }) |
| 158 | .join('\n\n'); |
| 159 | |
| 160 | const callbackSection = callbackDeclarations ? `\n${callbackDeclarations}\n` : ''; |
| 161 | |
| 162 | const componentJsx = children |
| 163 | ? `<${componentName}${propsString ? `\n${propsString}` : ''}>\n ${children}\n</${componentName}>` |
| 164 | : `<${componentName}${propsString ? `\n${propsString}\n` : ' '}/>`; |
| 165 | |
| 166 | return `import ${componentName} from "${importPath}"; |
| 167 | ${callbackSection} |
| 168 | ${componentJsx}`; |
| 169 | } |
nothing calls this directly
no test coverage detected