From afdd3767592d20efc7da9febee87bfad20751d7a Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Fri, 3 Mar 2023 21:09:44 -0500 Subject: [PATCH] fix(striker-ui): correct validity test on first render in InputWithRef --- striker-ui/components/InputWithRef.tsx | 17 ++++++++++------- .../components/StrikerConfig/AddPeerDialog.tsx | 4 ++-- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/striker-ui/components/InputWithRef.tsx b/striker-ui/components/InputWithRef.tsx index 89c016f4..16f769aa 100644 --- a/striker-ui/components/InputWithRef.tsx +++ b/striker-ui/components/InputWithRef.tsx @@ -5,7 +5,6 @@ import { forwardRef, ReactElement, useCallback, - useEffect, useImperativeHandle, useMemo, useState, @@ -26,7 +25,7 @@ type InputWithRefOptionalPropsWithoutDefault< TypeName extends keyof MapToInputType, > = { inputTestBatch?: InputTestBatch; - onFirstRender?: (args: { isRequired: boolean }) => void; + onFirstRender?: InputFirstRenderFunction; valueKey?: CreateInputOnChangeHandlerOptions['valueKey']; }; @@ -167,11 +166,15 @@ const InputWithRef = forwardRef( [initOnFocus, inputTestBatch], ); - useEffect(() => { - if (isFirstRender) { - onFirstRender?.call(null, { isRequired }); - } - }, [isFirstRender, isRequired, onFirstRender]); + if (isFirstRender) { + const isValid = + testInput?.call(null, { + inputs: { [INPUT_TEST_ID]: { value: inputValue } }, + isIgnoreOnCallbacks: true, + }) ?? false; + + onFirstRender?.call(null, { isValid }); + } useImperativeHandle( ref, diff --git a/striker-ui/components/StrikerConfig/AddPeerDialog.tsx b/striker-ui/components/StrikerConfig/AddPeerDialog.tsx index e8cb8dc3..1ccfcaad 100644 --- a/striker-ui/components/StrikerConfig/AddPeerDialog.tsx +++ b/striker-ui/components/StrikerConfig/AddPeerDialog.tsx @@ -61,8 +61,8 @@ const AddPeerDialog = forwardRef< const buildInputFirstRenderFunction = useCallback( (key: string) => - ({ isRequired }: { isRequired: boolean }) => { - setFormValidity(buildObjectStateSetterCallback(key, !isRequired)); + ({ isValid }: InputFirstRenderFunctionArgs) => { + setFormValidity(buildObjectStateSetterCallback(key, isValid)); }, [], );