fix(striker-ui): add unmount event and simplify mount/first render event in InputWithRef

main
Tsu-ba-me 2 years ago
parent f6b7a2df5a
commit bc25f1a2ad
  1. 13
      striker-ui/components/InputWithRef.tsx

@ -13,7 +13,6 @@ import {
import createInputOnChangeHandler from '../lib/createInputOnChangeHandler';
import { createTestInputFunction } from '../lib/test_input';
import useIsFirstRender from '../hooks/useIsFirstRender';
type InputWithRefOptionalPropsWithDefault<
TypeName extends keyof MapToInputType,
@ -27,6 +26,7 @@ type InputWithRefOptionalPropsWithoutDefault<
> = {
inputTestBatch?: InputTestBatch;
onFirstRender?: InputFirstRenderFunction;
onUnmount?: () => void;
valueKey?: CreateInputOnChangeHandlerOptions<TypeName>['valueKey'];
};
@ -70,6 +70,7 @@ const InputWithRef = forwardRef(
input,
inputTestBatch,
onFirstRender,
onUnmount,
required: isRequired = INPUT_WITH_REF_DEFAULT_PROPS.required,
valueKey,
valueType = INPUT_WITH_REF_DEFAULT_PROPS.valueType as TypeName,
@ -97,8 +98,6 @@ const InputWithRef = forwardRef(
...restInitProps
} = inputProps;
const isFirstRender = useIsFirstRender();
const [inputValue, setInputValue] =
useState<MapToInputType[TypeName]>(initValue);
const [isChangedByUser, setIsChangedByUser] = useState<boolean>(false);
@ -175,7 +174,6 @@ const InputWithRef = forwardRef(
* render function completes.
*/
useEffect(() => {
if (isFirstRender) {
const isValid =
testInput?.call(null, {
inputs: { [INPUT_TEST_ID]: { value: inputValue } },
@ -183,8 +181,11 @@ const InputWithRef = forwardRef(
}) ?? false;
onFirstRender?.call(null, { isValid });
}
}, [inputValue, isFirstRender, onFirstRender, testInput]);
return onUnmount;
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
useImperativeHandle(
ref,

Loading…
Cancel
Save