diff options
author | 2021-02-08 19:35:57 +0200 | |
---|---|---|
committer | 2021-02-08 19:35:57 +0200 | |
commit | da34ee3a362c04a52ea4c0d1f8e353aec8ab34ab (patch) | |
tree | b5c49af21b33e10c9f48fd28d168b27db671a471 | |
parent | Implement focusing text fields if empty on submit (diff) |
Create refMap to avoid errors on direct ref access
-rw-r--r-- | src/pages/FormPage.tsx | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/src/pages/FormPage.tsx b/src/pages/FormPage.tsx index 50f48ba..7798be6 100644 --- a/src/pages/FormPage.tsx +++ b/src/pages/FormPage.tsx @@ -197,10 +197,13 @@ function FormPage(): JSX.Element { if (!form) { return <Loading/>; } - + + const refMap: Map<string, React.RefObject<RenderedQuestion>> = new Map(); const questions = form.questions.map((question, index) => { + const questionRef = createRef<RenderedQuestion>(); + refMap.set(question.id, questionRef); // eslint-disable-next-line @typescript-eslint/no-explicit-any - return <RenderedQuestion ref={createRef<RenderedQuestion>()} focus_ref={createRef<any>()} scroll_ref={createRef<HTMLDivElement>()} question={question} public_state={new Map()} key={index + Date.now()}/>; + return <RenderedQuestion ref={questionRef} focus_ref={createRef<any>()} scroll_ref={createRef<HTMLDivElement>()} question={question} public_state={new Map()} key={index + Date.now()}/>; }); async function handleSubmit(event: SyntheticEvent) { @@ -213,7 +216,10 @@ function FormPage(): JSX.Element { return; } - prop.ref.current.validateField(); + const questionRef = refMap.get(question.id); + if (questionRef && questionRef.current) { + questionRef.current.validateField(); + } // In case when field is invalid, add this to invalid fields list. if (prop.props.public_state.get("valid") === false) { invalidFieldIDs.push(i); |