22
22
23
23
import * as React from 'react' ;
24
24
import classnames from 'classnames' ;
25
- // @ts -ignore no mdc .d.ts
26
- import { MDCFloatingLabelFoundation } from '@material/floating-label/dist/mdc.floatingLabel ' ;
25
+ import { MDCFloatingLabelFoundation } from '@material/floating-label/foundation' ;
26
+ import { MDCFloatingLabelAdapter } from '@material/floating-label/adapter ' ;
27
27
28
28
export interface FloatingLabelProps extends React . LabelHTMLAttributes < HTMLLabelElement > {
29
29
className ?: string ;
@@ -39,8 +39,8 @@ export default class FloatingLabel extends React.Component<
39
39
FloatingLabelProps ,
40
40
FloatingLabelState
41
41
> {
42
- foundation_ ? : MDCFloatingLabelFoundation ;
43
- labelElement_ : React . RefObject < HTMLLabelElement > = React . createRef ( ) ;
42
+ foundation ! : MDCFloatingLabelFoundation ;
43
+ labelElement : React . RefObject < HTMLLabelElement > = React . createRef ( ) ;
44
44
45
45
static defaultProps : Partial < FloatingLabelProps > = {
46
46
className : '' ,
@@ -55,26 +55,26 @@ export default class FloatingLabel extends React.Component<
55
55
this . initializeFoundation ( ) ;
56
56
this . handleWidthChange ( ) ;
57
57
if ( this . props . float ) {
58
- this . foundation_ . float ( true ) ;
58
+ this . foundation . float ( true ) ;
59
59
}
60
60
}
61
61
62
62
componentWillUnmount ( ) {
63
- this . foundation_ . destroy ( ) ;
63
+ this . foundation . destroy ( ) ;
64
64
}
65
65
66
66
componentDidUpdate ( prevProps : FloatingLabelProps ) {
67
67
if ( this . props . children !== prevProps . children ) {
68
68
this . handleWidthChange ( ) ;
69
69
}
70
70
if ( this . props . float !== prevProps . float ) {
71
- this . foundation_ . float ( this . props . float ) ;
71
+ this . foundation . float ( this . props . float ! ) ;
72
72
}
73
73
}
74
74
75
75
initializeFoundation = ( ) => {
76
- this . foundation_ = new MDCFloatingLabelFoundation ( this . adapter ) ;
77
- this . foundation_ . init ( ) ;
76
+ this . foundation = new MDCFloatingLabelFoundation ( this . adapter ) ;
77
+ this . foundation . init ( ) ;
78
78
} ;
79
79
80
80
get classes ( ) {
@@ -83,17 +83,26 @@ export default class FloatingLabel extends React.Component<
83
83
return classnames ( 'mdc-floating-label' , Array . from ( classList ) , className ) ;
84
84
}
85
85
86
- get adapter ( ) {
86
+ get adapter ( ) : MDCFloatingLabelAdapter {
87
87
return {
88
88
addClass : ( className : string ) =>
89
89
this . setState ( { classList : this . state . classList . add ( className ) } ) ,
90
90
removeClass : this . removeClassFromClassList ,
91
+ // the adapter methods below are effectively useless since React
92
+ // handles events and width differently
93
+ registerInteractionHandler : ( ) => undefined ,
94
+ deregisterInteractionHandler : ( ) => undefined ,
95
+ // Always returns 0 beacuse MDC Web component does
96
+ // only proxies to foundation.getWidth.
97
+ // MDC React instead passes it from the text-field
98
+ // component to floating-label component.
99
+ getWidth : ( ) => 0 ,
91
100
} ;
92
101
}
93
102
94
103
// must be called via ref
95
104
shake = ( ) => {
96
- this . foundation_ . shake ( true ) ;
105
+ this . foundation . shake ( true ) ;
97
106
} ;
98
107
99
108
removeClassFromClassList = ( className : string ) => {
@@ -103,9 +112,8 @@ export default class FloatingLabel extends React.Component<
103
112
} ;
104
113
105
114
handleWidthChange = ( ) => {
106
- const { handleWidthChange} = this . props ;
107
- if ( handleWidthChange && this . labelElement_ . current ) {
108
- handleWidthChange ( this . labelElement_ . current . offsetWidth ) ;
115
+ if ( this . props . handleWidthChange && this . labelElement . current ) {
116
+ this . props . handleWidthChange ( this . labelElement . current . offsetWidth ) ;
109
117
}
110
118
} ;
111
119
@@ -126,7 +134,7 @@ export default class FloatingLabel extends React.Component<
126
134
return (
127
135
< label
128
136
className = { this . classes }
129
- ref = { this . labelElement_ }
137
+ ref = { this . labelElement }
130
138
onAnimationEnd = { this . onShakeEnd }
131
139
{ ...otherProps }
132
140
>
0 commit comments