From 2b90bf3a9fc93373ac16190385cc0fdb0eb68fd8 Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Tue, 19 Dec 2023 18:10:56 -0500 Subject: [PATCH] fix(striker-ui): allow auto update input value if not changed by user --- striker-ui/components/InputWithRef.tsx | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/striker-ui/components/InputWithRef.tsx b/striker-ui/components/InputWithRef.tsx index 00faccd7..c8f21696 100644 --- a/striker-ui/components/InputWithRef.tsx +++ b/striker-ui/components/InputWithRef.tsx @@ -198,6 +198,23 @@ const InputWithRef = forwardRef( // eslint-disable-next-line react-hooks/exhaustive-deps }, []); + /** + * Update the input value to the init value until it's changed by the user. + * This allows us to populate the input based on value from other field(s). + */ + useEffect(() => { + if (isChangedByUser || !initValue) return; + + const valid = + testInput?.call(null, { + inputs: { [INPUT_TEST_ID]: { value: initValue } }, + isIgnoreOnCallbacks: true, + }) ?? false; + + setIsInputValid(valid); + setInputValue(initValue); + }, [initValue, isChangedByUser, testInput]); + useImperativeHandle( ref, () => ({