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

Function injectPropsIntoUsage

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

Source from the content-addressed store, hash-verified

111 * @returns {string} Updated usage code with current props
112 */
113export function injectPropsIntoUsage(usageTemplate, componentName, props, defaultProps = {}, options = {}) {
114 const { exclude = ['className', 'key', 'ref'], include = [] } = options;
115
116 // Generate the props string
117 const propsString = generatePropsString(props, defaultProps, { exclude, include, indent: 2 });
118
119 // Find the component usage in the template and replace its props
120 // This regex matches <ComponentName followed by props and closing />
121 const componentRegex = new RegExp(`(<${componentName})([^>]*)(\\s*/>)`, 's');
122
123 const match = usageTemplate.match(componentRegex);
124
125 if (!match) {
126 // If no match found, return original
127 return usageTemplate;
128 }
129
130 // Build new component usage
131 if (propsString) {
132 return usageTemplate.replace(componentRegex, `$1\n${propsString}\n$3`);
133 }
134
135 return usageTemplate;
136}
137
138/**
139 * Creates a simple usage example for a component with given props.

Callers

nothing calls this directly

Calls 1

generatePropsStringFunction · 0.85

Tested by

no test coverage detected