mirror of
https://github.com/logto-io/logto.git
synced 2024-12-16 20:26:19 -05:00
fix(ui): reset InputField on enabledTypes change (#3319)
This commit is contained in:
parent
32dff20633
commit
1c5bdda02a
2 changed files with 26 additions and 0 deletions
|
@ -3,6 +3,7 @@ import { assert } from '@silverhand/essentials';
|
|||
import { useState, useCallback, useMemo } from 'react';
|
||||
import type { ChangeEventHandler } from 'react';
|
||||
|
||||
import useUpdateEffect from '@/hooks/use-update-effect';
|
||||
import { getDefaultCountryCallingCode } from '@/utils/country-code';
|
||||
import { parseIdentifierValue } from '@/utils/form';
|
||||
|
||||
|
@ -121,6 +122,12 @@ const useSmartInputField = ({ _defaultType, defaultValue, enabledTypes }: Props)
|
|||
setCurrentType(enabledTypeSet.size === 1 ? defaultType : undefined);
|
||||
}, [defaultType, enabledTypeSet.size]);
|
||||
|
||||
// CAUTION: For preview use only, enabledTypes and defaultType should not be changed after component mounted
|
||||
useUpdateEffect(() => {
|
||||
setInputValue('');
|
||||
setCurrentType(defaultType);
|
||||
}, [defaultType]);
|
||||
|
||||
return {
|
||||
countryCode,
|
||||
onCountryCodeChange,
|
||||
|
|
19
packages/ui/src/hooks/use-update-effect.ts
Normal file
19
packages/ui/src/hooks/use-update-effect.ts
Normal file
|
@ -0,0 +1,19 @@
|
|||
import type { DependencyList, EffectCallback } from 'react';
|
||||
import { useEffect, useRef } from 'react';
|
||||
|
||||
const useUpdateEffect = (effect: EffectCallback, dependencies: DependencyList | undefined = []) => {
|
||||
const isMounted = useRef(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (!isMounted.current) {
|
||||
// eslint-disable-next-line @silverhand/fp/no-mutation
|
||||
isMounted.current = true;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
return effect();
|
||||
}, [...dependencies]);
|
||||
};
|
||||
|
||||
export default useUpdateEffect;
|
Loading…
Reference in a new issue