()} question={question} public_state={new Map()} key={index + Date.now()}/>);
});
function handleSubmit(event: SyntheticEvent) {
- questions.forEach(prop => {
+ // Client-side required validation
+ const invalidFieldIds: string[] = [];
+ questionsMap.forEach((prop, id) => {
+ const question: Question = prop.props.question;
+ if (!question.required) {
+ return;
+ }
+
+ prop.ref.current.validateField();
+ // In case when field is invalid, add this to invalid fields list.
+ if (prop.props.public_state.get("valid") === false) {
+ invalidFieldIds.push(id);
+ }
+ });
+
+ if (invalidFieldIds.length) {
+ const firstErrored = questionsMap.get(invalidFieldIds[0]);
+ if (firstErrored !== undefined) {
+ firstErrored.props.scroll_ref.current.scrollIntoView({ behavior: "smooth", block: "center" });
+ }
+ return;
+ }
+
+ questionsMap.forEach(prop => {
const question = prop.props.question;
// TODO: Parse input from each question, and submit
@@ -192,6 +217,9 @@ function FormPage(): JSX.Element {
closed_header = This form is now closed. You will not be able to submit your response.
;
}
+ const questions: JSX.Element[] = [];
+ questionsMap.forEach(val => questions.push(val));
+
return (
--
cgit v1.2.3