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 } ;
3+ export interface InputValueState < T > {
4+ value : T ;
5+ onChange ( e : any ) : void ;
56}
67
7- export interface InputValueState {
8- value : string ;
9- onChange ( e : ChangeEvent ) : void ;
10- }
11-
12- export function useInputValue ( initialValue : string = '' ) : InputValueState {
13- const [ value , setValue ] = useState ( initialValue ) ;
8+ export function useInputValue < T = string > ( initialValue : T ) : InputValueState < T > {
9+ const [ value , setValue ] = useState < T > ( 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 as T ) ;
16+ } ,
1617 [ ]
1718 ) ;
1819 return { value, onChange} ;
You can’t perform that action at this time.
0 commit comments