MCPcopy Create free account
hub / github.com/DavidHDev/react-bits / createUsageExample

Function createUsageExample

src/utils/codeGeneration.js:148–169  ·  view source on GitHub ↗
(componentName, importPath, props, defaultProps = {}, options = {})

Source from the content-addressed store, hash-verified

146 * @returns {string} Complete usage example code
147 */
148export 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}

Callers

nothing calls this directly

Calls 1

generatePropsStringFunction · 0.85

Tested by

no test coverage detected