import {SetStateAction} from 'react' import {isFunctionalUpdate} from '@karma.run/react' import {BlockProps} from './block' export interface TransformBlockProps extends BlockProps { transformTo: (value: T) => O transformFrom: (value: O) => T children: (props: BlockProps) => JSX.Element } export function TransformBlock({ transformTo, transformFrom, children, value, onChange, autofocus }: TransformBlockProps) { const transformedValue = transformTo(value) const onTransformedChange = (newValue: SetStateAction) => { onChange(transformFrom(isFunctionalUpdate(newValue) ? newValue(transformedValue) : newValue)) } return children({ value: transformedValue, onChange: onTransformedChange, autofocus }) }