This repository was archived by the owner on Jan 14, 2025. It is now read-only.
File tree 3 files changed +26
-8
lines changed
3 files changed +26
-8
lines changed Original file line number Diff line number Diff line change @@ -29,7 +29,7 @@ export interface InputProps<T extends HTMLElement = HTMLInputElement> {
29
29
isValid ?: boolean ;
30
30
foundation ?: MDCTextFieldFoundation ;
31
31
handleValueChange ?: ( value : string | number | string [ ] | undefined , cb : ( ) => void ) => void ;
32
- ref ?: ( inputInstance : Input < T > ) => void ,
32
+ syncInput ?: ( inputInstance : Input < T > ) => void ,
33
33
onBlur ?: Pick < React . HTMLProps < T > , 'onBlur' > ;
34
34
onChange ?: Pick < React . HTMLProps < T > , 'onChange' > ;
35
35
onFocus ?: Pick < React . HTMLProps < T > , 'onFocus' > ;
@@ -81,6 +81,7 @@ export default class Input<T extends HTMLElement = HTMLInputElement> extends Rea
81
81
componentDidMount ( ) {
82
82
const {
83
83
id,
84
+ syncInput,
84
85
disabled,
85
86
value,
86
87
setInputId,
@@ -89,6 +90,9 @@ export default class Input<T extends HTMLElement = HTMLInputElement> extends Rea
89
90
foundation,
90
91
isValid,
91
92
} = this . props ;
93
+ if ( syncInput ) {
94
+ syncInput ( this ) ;
95
+ }
92
96
if ( setInputId && id ) {
93
97
setInputId ( id ! ) ;
94
98
}
@@ -257,6 +261,7 @@ export default class Input<T extends HTMLElement = HTMLInputElement> extends Rea
257
261
/* eslint-disable no-unused-vars */
258
262
className,
259
263
foundation,
264
+ syncInput,
260
265
isValid,
261
266
value,
262
267
handleFocusChange,
Original file line number Diff line number Diff line change @@ -275,18 +275,13 @@ class TextField<T extends HTMLElement = HTMLInputElement> extends React.Componen
275
275
inputProps ( child : React . ReactElement < InputProps < T > > ) {
276
276
// ref does exist on React.ReactElement<InputProps<T>>
277
277
// @ts -ignore
278
- const { props, ref } = child ;
278
+ const { props} = child ;
279
279
return Object . assign ( { } , props , {
280
280
foundation : this . state . foundation ,
281
281
handleFocusChange : ( isFocused : boolean ) => this . setState ( { isFocused} ) ,
282
282
setDisabled : ( disabled : boolean ) => this . setState ( { disabled} ) ,
283
283
setInputId : ( id : string ) => this . setState ( { inputId : id } ) ,
284
- ref : ( input : Input < T > ) => {
285
- if ( typeof ref === 'function' ) {
286
- ref ( input ) ;
287
- }
288
- this . inputComponent_ = input ;
289
- } ,
284
+ syncInput : ( input : Input < T > ) => ( this . inputComponent_ = input ) ,
290
285
inputType : this . props . textarea ? 'textarea' : 'input' ,
291
286
} ) ;
292
287
}
Original file line number Diff line number Diff line change @@ -696,3 +696,21 @@ test('Useless test for code coverage', () => {
696
696
wrapper . instance ( ) . adapter . registerInputInteractionHandler ( temp , temp ) ;
697
697
wrapper . instance ( ) . adapter . deregisterInputInteractionHandler ( temp , temp ) ;
698
698
} ) ;
699
+
700
+ test ( 'Input component sync test in TextField' , ( ) => {
701
+ class TestComponent extends React . Component {
702
+ state = {
703
+ disabled : false ,
704
+ } ;
705
+ render ( ) {
706
+ return < TextField >
707
+ < Input disabled = { this . state . disabled } />
708
+ </ TextField > ;
709
+ }
710
+ }
711
+ const wrapper = mount < TestComponent > ( < TestComponent /> ) ;
712
+ // If inputComponent is null and disabled is true,
713
+ // setDisabled called #inputAdapter.getNativeInput
714
+ // and throw error because there is no inputComponent
715
+ assert . doesNotThrow ( ( ) => wrapper . instance ( ) . setState ( { disabled : true } ) ) ;
716
+ } ) ;
You can’t perform that action at this time.
0 commit comments