Boa tarde Joao, tudo tranquilo meu querido, e contigo?
Obrigado pelo feedback, então o contexto da minha aplicação seria no seguinte código:
import { FormEvent, Fragment, useEffect, useState } from "react"
import { Colors } from "../../colors"
import { DefaultValues, TypeSetState } from "../../types"
import { CheckInput } from "./check"
import { ComboInput } from "./combo"
import { TextInput } from "./text"
import { LabelCustom } from "./text/style"
export enum TypesFormInput {
TEXT = 'TEXT',
COMBO = 'COMBO',
CHECK = 'CHECK'
}
export type OptionsSelect = Array<{ value: any, label: string }>
export interface PropsFormInput extends DefaultValues {
type: TypesFormInput
color?: string
title?: string
options?: OptionsSelect
onChange?: (event: FormEvent<HTMLInputElement>, setState: TypeSetState<StateFormInput>) => void
}
export interface StateFormInput {
color: string
error: boolean
value: string | null
}
export const FormInput = (props: PropsFormInput) => {
const [ state, setState ] = useState({ } as StateFormInput)
useEffect(() => {
setState(prevState => ({ ...prevState, color: state.error ? 'red' : Colors.gray }))
}, [ state.error ])
const define = ():JSX.Element => {
switch (props.type) {
case TypesFormInput.TEXT:
return (
<TextInput
{ ...props }
{ ...state }
color={ state.color }
onChange={ (event: FormEvent<HTMLInputElement>) => props.onChange?.(event, setState) }
/>
)
case TypesFormInput.COMBO:
return (
<ComboInput
options={ props.options || [] }
{ ...props }
/>
)
case TypesFormInput.CHECK:
return <CheckInput { ...props }/>
default:
return <></>
}
}
return (
<Fragment>
<LabelCustom>
{ props.title }
{ define() }
</LabelCustom>
</Fragment>
)
}
Gostaria de obrigar o programador a quando declarar a props "onChange" que forneça também a "value", embora atualmente esteja funcionando, achei meio gamb a forma que utilizei, outra questão seria validar qual o tipo de formInput foi utilizado, se o pragramador escolheu o input "COMBO" obriga-lo a preencher as "options" isso em tempo de desenvolvimento e evitar tratativas como :
props.options || []