File tree Expand file tree Collapse file tree 4 files changed +47
-21
lines changed Expand file tree Collapse file tree 4 files changed +47
-21
lines changed Original file line number Diff line number Diff line change @@ -96,18 +96,18 @@ describe('InlineSearchInput', () => {
96
96
97
97
it ( 'should clear controlled text input' , ( ) => {
98
98
const { input, clearButton } = getInlineSearchInput (
99
- < InlineSearchInput value = "1234" onChange = { handleEventMock } /> ,
99
+ < InlineSearchInput onChange = { handleEventMock } /> ,
100
100
) ;
101
101
102
- if ( input && clearButton ) {
103
- fireEvent . change ( input , { target : { value : 'test' } } ) ;
102
+ if ( ! input || ! clearButton ) return ;
104
103
105
- expect ( input . value ) . toBe ( '1234' ) ;
104
+ fireEvent . change ( input , { target : { value : 'test' } } ) ;
106
105
107
- fireEvent . click ( clearButton ) ;
106
+ expect ( input . value ) . toBe ( 'test' ) ;
108
107
109
- expect ( handleEventMock ) . toBeCalledWith ( { target : { value : '' } } ) ;
110
- }
108
+ fireEvent . click ( clearButton ) ;
109
+
110
+ expect ( handleEventMock ) . toBeCalledWith ( { target : { value : '' } } ) ;
111
111
112
112
expect ( input ) . toBeDefined ( ) ;
113
113
} ) ;
Original file line number Diff line number Diff line change @@ -103,13 +103,13 @@ describe('SearchInput', () => {
103
103
104
104
it ( 'should clear controlled text input' , ( ) => {
105
105
const { input, clearButton } = getSearchInput (
106
- < SearchInput value = "1234" onChange = { handleEventMock } /> ,
106
+ < SearchInput onChange = { handleEventMock } /> ,
107
107
) ;
108
108
109
109
if ( input && clearButton ) {
110
110
fireEvent . change ( input , { target : { value : 'test' } } ) ;
111
111
112
- expect ( input . value ) . toBe ( '1234 ' ) ;
112
+ expect ( input . value ) . toBe ( 'test ' ) ;
113
113
114
114
fireEvent . click ( clearButton ) ;
115
115
Original file line number Diff line number Diff line change @@ -126,18 +126,25 @@ describe('TextInput', () => {
126
126
} ) ;
127
127
128
128
it ( 'should clear controlled text input' , ( ) => {
129
+ const mockedOnChanged = vi . fn ( ) ;
130
+
129
131
const { input, clearButton } = getTextInput (
130
- < TextInput value = "1234" onChange = { handleEventMock } hasClearButton /> ,
132
+ < TextInput onChange = { mockedOnChanged } hasClearButton /> ,
131
133
) ;
132
134
133
135
if ( input && clearButton ) {
134
- fireEvent . change ( input , { target : { value : 'test' } } ) ;
136
+ fireEvent . change ( input , { target : { value : 'test value ' } } ) ;
135
137
136
- expect ( input . value ) . toBe ( '1234 ' ) ;
138
+ expect ( input . value ) . toBe ( 'test value ' ) ;
137
139
138
140
fireEvent . click ( clearButton ) ;
139
141
140
- expect ( handleEventMock ) . toBeCalledWith ( { target : { value : '' } } ) ;
142
+ expect ( mockedOnChanged ) . toHaveBeenCalledWith (
143
+ expect . objectContaining ( {
144
+ _reactName : 'onChange' ,
145
+ target : expect . objectContaining ( { value : '' } ) ,
146
+ } ) ,
147
+ ) ;
141
148
}
142
149
143
150
expect ( input ) . toBeDefined ( ) ;
Original file line number Diff line number Diff line change 1
1
import {
2
- ChangeEvent ,
3
2
ChangeEventHandler ,
4
3
MouseEventHandler ,
5
4
FocusEvent ,
@@ -53,13 +52,33 @@ export const useTextInput = ({
53
52
[ onChange ] ,
54
53
) ;
55
54
56
- const handleOnClear : MouseEventHandler < HTMLButtonElement > =
57
- useCallback ( ( ) => {
58
- setInnerValue ( '' ) ;
59
- onChange ?.( {
60
- target : { value : '' } ,
61
- } as ChangeEvent < HTMLInputElement > ) ;
62
- } , [ onChange ] ) ;
55
+ const handleOnClear : MouseEventHandler < HTMLButtonElement > = ( ) => {
56
+ setInnerValue ( '' ) ;
57
+
58
+ const input = containerRef . current ?. querySelector ( 'input' ) ;
59
+
60
+ if ( ! input ) return ;
61
+
62
+ const valueSetter = Object . getOwnPropertyDescriptor (
63
+ input . constructor . prototype ,
64
+ 'value' ,
65
+ ) ?. set ;
66
+
67
+ const prototype = Object . getPrototypeOf ( input ) ;
68
+ const prototypeValueSetter = Object . getOwnPropertyDescriptor (
69
+ prototype ,
70
+ 'value' ,
71
+ ) ?. set ;
72
+
73
+ if ( valueSetter && valueSetter !== prototypeValueSetter ) {
74
+ prototypeValueSetter ?. call ( input , '' ) ;
75
+ } else {
76
+ valueSetter ?. call ( input , '' ) ;
77
+ }
78
+
79
+ input . dispatchEvent ( new Event ( 'input' , { bubbles : true } ) ) ;
80
+ } ;
81
+
63
82
return {
64
83
innerValue,
65
84
styles,
You can’t perform that action at this time.
0 commit comments