import {submitTender} from '../utils';
const ChequeModal = ({
chequeModalOpen,
handleChequeModalClose,
chequeDetails,
localAmount
}: ChequeModalProps) => {
const handleSubmit = (e) => {
e.preventDefault();
submitTender({
...chequeDetails,
});
handleChequeModalClose();
};
return (
<Panel overlay title = "Cheque details"
variant = {
PanelHeaderVariant.TitleAndCloseButton
}
size = {
PanelSize.Large
}
onClose = {
handleChequeModalClose()
}
open = {
chequeModalOpen
} >
<LabelWrapper> Cheque number < /LabelWrapper>
<InputWrapper name = "chequeNumber"
onInput = {
(e: any) => {
setChequeNumberInput(filterDigits(e.target.value));
}
}
value = {
chequeNumberInput
}
onBlur = {
() => checkError(errorType.chequeNumber)
}
errorMessage = {
chequeNumberError
}
/>
<Button label = "Confirm"
id = "confirm-btn"
data - autofocus onClick = {
handleSubmit
}
/>
<Button label = "Cancel"
variant = {
ButtonVariant.SecondaryFilled
}
onClick = {
handleChequeModalClose()
}
/>
</Panel>
}
///////CheckoutPage.tsx//////
const CheckoutPage = ({
//...some code here}:CheckoutPageProps) =>{
const [chequeModalOpen, setChequeModalOpen] = useState(false);
const handleChequeModalClose = () => {
setChequeModalOpen(false);
};
return (
<ChequeModal chequeModalOpen = {
chequeModalOpen
}
handleChequeModalClose = {
() => handleChequeModalClose
}
localAmount = {
keyInAmount
}
chequeDetails = {
chequeDetails
} >
</ChequeModal>
)
}
In your CheckoutPage component when sending your closing function in the ChequeModal component you are sending a function that will return a reference to it when called:
handleChequeModalClose = {() => handleChequeModalClose}
It is working for the close button as your are calling it upon rendering your component, the actual close function will be then called when the user close the modal.
However in your submit function you are calling the function sent in the props but the actual closing function is never called.
You should send in the props a direct reference to your function as such:
handleChequeModalClose = {handleChequeModalClose}
and then send it to the onClose
props of the modal without calling it:
onClose = {handleChequeModalClose}
In that case your submit function will not have to be modified and your modal will successfully close itself.
P.S For later questions on StackOverflow please use proper formatting and try to send a working example of your code.