import React, { useContext, useState } from 'react'; import { Input, FormControl } from '../../../index'; import { CodePreviewContext } from '../CodePreviewProvider'; export const InputControl = ({ defaultValue, componentProp }: any) => { const { propsString, updatePropsString } = useContext(CodePreviewContext); const [value, setValue] = useState(defaultValue ?? ''); const handleChange = (e: any) => { const inputValue = e.target.value; setValue(inputValue); const componentPropValueRegex = new RegExp(componentProp + '="(.*?)"', 'g'); if (propsString.match(componentPropValueRegex)) { updatePropsString( propsString.replace( componentPropValueRegex, `${componentProp}="${inputValue}"` ) ); } else { updatePropsString(`${propsString} ${componentProp}="${inputValue}"`); } if (inputValue === '') { updatePropsString(propsString.replace(componentPropValueRegex, '')); } }; return ( {componentProp} ); };