2222
2323import * as React from 'react' ;
2424import 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 ' ;
2727
2828export interface FloatingLabelProps extends React . LabelHTMLAttributes < HTMLLabelElement > {
2929 className ?: string ;
@@ -39,8 +39,8 @@ export default class FloatingLabel extends React.Component<
3939 FloatingLabelProps ,
4040 FloatingLabelState
4141 > {
42- foundation_ ? : MDCFloatingLabelFoundation ;
43- labelElement_ : React . RefObject < HTMLLabelElement > = React . createRef ( ) ;
42+ foundation ! : MDCFloatingLabelFoundation ;
43+ labelElement : React . RefObject < HTMLLabelElement > = React . createRef ( ) ;
4444
4545 static defaultProps : Partial < FloatingLabelProps > = {
4646 className : '' ,
@@ -55,26 +55,26 @@ export default class FloatingLabel extends React.Component<
5555 this . initializeFoundation ( ) ;
5656 this . handleWidthChange ( ) ;
5757 if ( this . props . float ) {
58- this . foundation_ . float ( true ) ;
58+ this . foundation . float ( true ) ;
5959 }
6060 }
6161
6262 componentWillUnmount ( ) {
63- this . foundation_ . destroy ( ) ;
63+ this . foundation . destroy ( ) ;
6464 }
6565
6666 componentDidUpdate ( prevProps : FloatingLabelProps ) {
6767 if ( this . props . children !== prevProps . children ) {
6868 this . handleWidthChange ( ) ;
6969 }
7070 if ( this . props . float !== prevProps . float ) {
71- this . foundation_ . float ( this . props . float ) ;
71+ this . foundation . float ( this . props . float ! ) ;
7272 }
7373 }
7474
7575 initializeFoundation = ( ) => {
76- this . foundation_ = new MDCFloatingLabelFoundation ( this . adapter ) ;
77- this . foundation_ . init ( ) ;
76+ this . foundation = new MDCFloatingLabelFoundation ( this . adapter ) ;
77+ this . foundation . init ( ) ;
7878 } ;
7979
8080 get classes ( ) {
@@ -83,17 +83,26 @@ export default class FloatingLabel extends React.Component<
8383 return classnames ( 'mdc-floating-label' , Array . from ( classList ) , className ) ;
8484 }
8585
86- get adapter ( ) {
86+ get adapter ( ) : MDCFloatingLabelAdapter {
8787 return {
8888 addClass : ( className : string ) =>
8989 this . setState ( { classList : this . state . classList . add ( className ) } ) ,
9090 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 ,
91100 } ;
92101 }
93102
94103 // must be called via ref
95104 shake = ( ) => {
96- this . foundation_ . shake ( true ) ;
105+ this . foundation . shake ( true ) ;
97106 } ;
98107
99108 removeClassFromClassList = ( className : string ) => {
@@ -103,9 +112,8 @@ export default class FloatingLabel extends React.Component<
103112 } ;
104113
105114 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 ) ;
109117 }
110118 } ;
111119
@@ -126,7 +134,7 @@ export default class FloatingLabel extends React.Component<
126134 return (
127135 < label
128136 className = { this . classes }
129- ref = { this . labelElement_ }
137+ ref = { this . labelElement }
130138 onAnimationEnd = { this . onShakeEnd }
131139 { ...otherProps }
132140 >
0 commit comments