import React, { useContext } from 'react'; import { Switch, FormControl } from '../../../index'; import { CodePreviewContext } from '../CodePreviewProvider'; export const SwitchControl = ({ defaultValue, componentProp }: any) => { const [value, setValue] = React.useState(defaultValue ?? false); const { propsString, updatePropsString } = useContext(CodePreviewContext); const toggleSwitch = (val: any) => { setValue((previousState: boolean) => !previousState); const componentPropsValueRegex = new RegExp( componentProp + '={(.*?)}', 'g' ); if (propsString.match(componentPropsValueRegex)) { updatePropsString( propsString.replace( componentPropsValueRegex, `${componentProp}={${val}}` ) ); } }; return ( {componentProp} ); };