(usageTemplate, componentName, props, defaultProps = {}, options = {})
| 111 | * @returns {string} Updated usage code with current props |
| 112 | */ |
| 113 | export 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. |
nothing calls this directly
no test coverage detected