@@ -21,12 +21,13 @@ export default class EditableString extends React.Component<
2121 editedValue : this . props . value ,
2222 } ;
2323
24- measureWidthRef = React . createRef < HTMLSpanElement > ( ) ;
25- inputRef = React . createRef < HTMLInputElement | HTMLTextAreaElement > ( ) ;
24+ private _measureWidthSpan : HTMLSpanElement | null = null ;
25+ private _inputEl : HTMLInputElement | HTMLTextAreaElement | null = null ;
2626
27- _isMounted : boolean = false ;
27+ private _isMounted : boolean = false ;
2828 componentDidMount ( ) : void {
2929 this . _isMounted = true ;
30+ this . _updateWidth ( ) ;
3031 }
3132 componentWillUnmount ( ) : void {
3233 this . _isMounted = false ;
@@ -41,20 +42,24 @@ export default class EditableString extends React.Component<
4142 prevProps . value !== this . props . value ||
4243 prevState . editedValue !== this . state . editedValue
4344 ) {
44- if ( this . props . expanded ) return ;
45+ this . _updateWidth ( ) ;
46+ }
47+ }
4548
46- const measureWidth = this . measureWidthRef . current ;
47- const input = this . inputRef . current ;
49+ private _updateWidth ( ) {
50+ if ( this . props . expanded ) return ;
4851
49- if ( ! measureWidth ) return ;
50- if ( ! input ) return ;
52+ const measureWidthSpan = this . _measureWidthSpan ;
53+ const inputEl = this . _inputEl ;
5154
52- measureWidth . style . display = "inline" ;
53- const rect = measureWidth . getBoundingClientRect ( ) ;
54- measureWidth . style . display = "none" ;
55+ if ( ! measureWidthSpan ) return ;
56+ if ( ! inputEl ) return ;
5557
56- input . style . width = rect . width + "px" ;
57- }
58+ measureWidthSpan . style . display = "inline" ;
59+ const rect = measureWidthSpan . getBoundingClientRect ( ) ;
60+ measureWidthSpan . style . display = "none" ;
61+
62+ inputEl . style . width = rect . width + "px" ;
5863 }
5964
6065 render ( ) {
@@ -69,13 +74,17 @@ export default class EditableString extends React.Component<
6974 < >
7075 < span
7176 style = { { display : "none" } }
72- ref = { this . measureWidthRef }
77+ ref = { ( el ) => {
78+ this . _measureWidthSpan = el ;
79+ } }
7380 className = "measure-width"
7481 >
7582 { currentValue }
7683 </ span >
7784 < TagName
78- ref = { this . inputRef as any }
85+ ref = { ( el : HTMLInputElement | HTMLTextAreaElement | null ) => {
86+ this . _inputEl = el ;
87+ } }
7988 style = { { color, font : "inherit" , "max-width" : "200px" } }
8089 value = { currentValue }
8190 onFocus = { ( ) => {
@@ -87,11 +96,7 @@ export default class EditableString extends React.Component<
8796 onInput = { ( event : {
8897 currentTarget : HTMLTextAreaElement | HTMLInputElement ;
8998 } ) => {
90- if ( ! expanded && this . measureWidthRef . current ) {
91- event . currentTarget . style . width =
92- this . measureWidthRef . current . getBoundingClientRect ( ) . width +
93- "px" ;
94- }
99+ this . _updateWidth ( ) ;
95100
96101 this . setState ( { editedValue : event . currentTarget . value } , ( ) => {
97102 if ( this . _isMounted ) {
0 commit comments