11/* eslint-disable @typescript-eslint/no-explicit-any */
2- /* eslint-disable @typescript-eslint/no-non-null-assertion */
32import type { Updater as IUpdater } from './immer' ;
43
54import { freeze , produce } from 'immer' ;
@@ -12,10 +11,10 @@ import { useStateBackflow } from './state-backflow';
1211
1312export type Updater < S > = ( value : S ) => void ;
1413
15- export function useTwoWayBinding < T > (
14+ export function useTwoWayBinding < T , S = T > (
1615 initialValue : T | ( ( ) => T ) ,
17- input ?: [ T , Updater < T > ?] ,
18- onValueChange ?: ( value : any ) => void ,
16+ input ?: [ T , Updater < S > ?] ,
17+ onValueChange ?: ( value : S ) => void ,
1918 opt ?: {
2019 id : string ;
2120 formControlName : string ;
@@ -57,24 +56,23 @@ export function useTwoWayBinding<T>(
5756
5857 const setValue = input ?. [ 1 ] ;
5958 const [ autoValue , setAutoValue ] = useState < T > ( initialValue ) ;
60- const value = isUndefined ( input ?. [ 0 ] ) ? autoValue : input ! [ 0 ] ;
59+ const value = isUndefined ( input ) ? autoValue : input [ 0 ] ;
6160
6261 const currentValue = formControl ? formControl . value : value ;
6362
6463 const changeValue = useCallback (
6564 ( updater : any ) => {
6665 const val = isFunction ( updater ) ? produce ( currentValue , updater ) : freeze ( updater ) ;
6766
68- if ( formControl ) {
69- if ( ! Object . is ( val , currentValue ) ) {
67+ if ( ! Object . is ( val , currentValue ) ) {
68+ if ( formControl ) {
7069 formControl . markAsDirty ( true ) ;
7170 formControl . setValue ( val ) ;
72- formInstance ! . updateForm ( ) ;
73- }
74- } else {
75- setValue ?.( val ) ;
76- setAutoValue ( val ) ;
77- if ( ! Object . is ( val , currentValue ) ) {
71+ onValueChange ?.( val ) ;
72+ formInstance ?. updateForm ( ) ;
73+ } else {
74+ setValue ?.( val ) ;
75+ setAutoValue ( val ) ;
7876 onValueChange ?.( val ) ;
7977 }
8078 }
@@ -85,7 +83,7 @@ export function useTwoWayBinding<T>(
8583 const res = useMemo <
8684 [
8785 T ,
88- IUpdater < T > ,
86+ IUpdater < S > ,
8987 {
9088 validateClassName ?: string ;
9189 ariaAttribute ?: React . HTMLAttributes < HTMLElement > ;
0 commit comments