File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -24,3 +24,9 @@ test('change value via onChange', () => {
2424 act ( ( ) => result . current . onChange ( { target : { value : newValue } } ) ) ;
2525 expect ( result . current . value ) . toBe ( newValue ) ;
2626} ) ;
27+
28+ test ( 'target not in change value' , ( ) => {
29+ const { result} = renderHook ( ( ) => useInputValue ( ) ) ;
30+ act ( ( ) => result . current . onChange ( 'foo' ) ) ;
31+ expect ( result . current . value ) . toBe ( 'foo' ) ;
32+ } ) ;
Original file line number Diff line number Diff line change 11import { useState , useCallback } from 'react' ;
22
3- interface ChangeEvent {
4- target : { value : string } ;
5- }
6-
73export interface InputValueState {
84 value : string ;
9- onChange ( e : ChangeEvent ) : void ;
5+ onChange ( e : any ) : void ;
106}
117
128export function useInputValue ( initialValue : string = '' ) : InputValueState {
139 const [ value , setValue ] = useState ( initialValue ) ;
1410 const onChange = useCallback (
15- ( e : ChangeEvent ) => setValue ( e . target . value ) ,
11+ ( event : any ) => {
12+ const v = event && event . target && 'value' in event . target
13+ ? ( event . target as HTMLInputElement ) . value
14+ : event ;
15+ setValue ( v ) ;
16+ } ,
1617 [ ]
1718 ) ;
1819 return { value, onChange} ;
You can’t perform that action at this time.
0 commit comments