Replies: 1 comment
-
Just wondering.maybe it is easier to put some div on top of form so user will not be able to interact with form at all? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Working demo
I have a usage question around React. React docs specify Stack Overflow as the place to get support, but nobody was able to provide an answer for my question there (not even a wrong answer), so I want to try asking my question here and hope that the knowledgeable members of the Preact community could help me.
I have implemented a SafeForm component. The idea is, this would be used exactly like the built-in HTML
<form>
component. However, behind the scenes, it would disable allINPUT
(andSELECT
andBUTTON
) fields while the request is in progress, so that the user wouldn't double-submit a form by mistake, or edit some fields in the form and think that they were able to edit the value of a submitted field. This component would be useful especially when the server is slow to respond, or when the network is slow. After the request has been completed, the fields would be enabled again.Although I have implemented such a component and it functionally works, the code is pretty hacky. I can't help but think that there must be a better, less hacky and more correct solution to it. However, my limited knowledge of React prevents me from discovering such solution. Hence, I would like to ask the members of the Preact community for their help.
Without further ado, this is how you would use the SafeForm component:
and this is how the SafeForm component is implemented:
The hackiest part here is manually dismounting and re-mounting the component in order to revert (reset) the manual changes that have been done on the DOM (that is, for re-enabling the manually disabled the input fields). For this purpose, a state variable named
mounted
exists and this variable is set and subsequently unset after the request has been completed.Previously, SafeForm was implemented as follows:
This was fine until I realized that it doesn't work when an
INPUT
element is declared within the render method of a child, instead of being declared as a child of the SafeForm or one of its descendants.BONUS: The SafeForm component actually contains functionality to replace an error message placeholder. That is, the user of the SafeForm would declare a placeholder
div
for any potential error messages to be placed into and the SafeForm component would replace it with the actual HTML elements to show the error message. Example usage:and SafeForm is implemented as follows:
Similarly, the issue here is not using (not being able to use?) JSX and instead, creating regular HTML Element objects with the help of the
document.createElement
method. I believe the proper solution for the first issue would automatically solve this issue as well but in case it doesn't (that is, in case this issue requires a different approach), I wanted to include this here too.Beta Was this translation helpful? Give feedback.
All reactions