Forgive me I've been through a lot of the existing questions around on this topic without success.
I have a filter dialog with several input fields (child components) and a submit button. I want the submit button to be disabled if a validation message exists on one of the child components.
Child (simplified):
const TextSearch: FC<TextSearchProps> = memo((props) => {
const [validationMessage, setValidationMessage] = useState('')
const onTextChange = (e: React.FormEvent<HTMLInputElement>) => {
const input = e.currentTarget.value
if(input && !checkNumericOnly(input)) {
setValidationMessage('Only numbers allowed')
} else {
setValidationMessage('')
}
return (
<div className={classes.textSearch}>
<InputText
className={validationMessage ? 'p-invalid' : ''}
onChange={onTextChange}
/>
<div className={classes.invalid}>{validationMessage}</div>
</div>
)
Parent:
const AdvancedSearch: FC<SearchProps> = memo(_props => {
const dispatch: AppDispatch = useDispatch()
const data = useSelector(selectSearchData())
const [validInput, setValidInput] = useState(true) // this is what I'm trying to set
How do I setValidInput based on whether the child validationMessage is true?
Have tried a function in the parent dialog:
function handleValidInput(valid: boolean) {
setValidInput(valid)
}
But have not been able to figure out how to call it from the child.