import * as React from 'react' import { PurePtr } from '@pure-ptr/core' function appendClass( classes: string, name: string ){ return classes ? classes + ' ' + name : name; } interface ValidationProps { className?: string; requiredClass?: string; invalidClass?: string; } export function validationClasses(props: ValidationProps, value: any, error: any): string { const classes = props.className || ''; if (!error) return classes; return appendClass( classes, value === '' ? props.requiredClass || 'required' : props.invalidClass || 'invalid' ); } export type InputProps = React.HTMLProps & ValidationProps & { valuePtr? : PurePtr, checkedPtr? : PurePtr } /** * A custom input component that binds its value to a `PurePtr` object. * * @param {PurePtr} props.valuePtr - A pointer object that holds the value of the input element. * @param {PurePtr} props.checkedPtr - A pointer object that holds the checked state of the input element. * @returns {JSX.Element} The rendered input element. * * @example * * */ export function Input( props : InputProps ) : JSX.Element { const { valuePtr, checkedPtr, ...rest } = props as any, type = props.type, ptr = valuePtr || checkedPtr; switch( type ){ case 'checkbox': return ptr.set( !ptr.value ) }/> case 'radio' : return { e.target.checked && ( checkedPtr ? checkedPtr.set( true ) : ptr.set( props.value ) ) } }/>; default: return ptr.set( e.target.value ) }/> } }; /** * A custom textarea component that binds its value to a `PurePtr` object. * * @param {PurePtr} props.valuePtr - A pointer object that holds the value of the textarea element. * @returns {JSX.Element} The rendered textarea element. * * @example *