import * as React from "react" ; import { MDCTextfieldFoundation } from "@material/textfield/dist/mdc.textfield" ; import { OrderedSet , Set } from "immutable" ; import * as PropTypes from "prop-types" ; import { ClassNameMetaBase , DefaultComponentBase , } from "@react-mdc/base" ; import { ContainerAdapter , FoundationAdapter } from "./adapter" ; import { BASE_CLASS_NAME , } from "./constants" ; export const CLASS_NAME = BASE_CLASS_NAME ; export const propertyClassNames = { PREFIX : CLASS_NAME , MULTILINE : `${ CLASS_NAME }--multiline` , FULLWIDTH : `${ CLASS_NAME }--fullwidth` , } ; export type ChildProps = { className ? : string ; } ; export type MetaProps = { disabled ? : boolean ; multiline ? : boolean ; fullwidth ? : boolean ; className ? : string ; } ; export type State = { foundationClasses : Set < string > ; } ; export type ChildContext = { adapter : FoundationAdapter ; } ; /**
 * Textfield input container component
 */ export class Meta extends ClassNameMetaBase < ChildProps , MetaProps , State > { public static childContextTypes = { adapter : PropTypes . instanceOf ( FoundationAdapter ) , } ; public state : State = { foundationClasses : OrderedSet < string > ( ) , } ; private adapter : FoundationAdapter ; private foundation : MDCTextfieldFoundation ; constructor ( props ) { super ( props ) ; this . adapter = new FoundationAdapter ( ) ; this . foundation = new MDCTextfieldFoundation ( this . adapter . toObject ( ) ) ; } public componentDidMount ( ) { this . adapter . setContainerAdapter ( new ContainerAdapterImpl ( this ) ) ; this . foundation . init ( ) ; } public componentWillUnmount ( ) { this . foundation . destroy ( ) ; this . adapter . setContainerAdapter ( new ContainerAdapter ( ) ) ; } public getChildContext ( ) { return { adapter : this . adapter , } ; } protected renderBaseClassName ( ) { return CLASS_NAME ; } protected renderClassValues ( ) { return [ { [ propertyClassNames . MULTILINE ] : this . props . multiline , [ propertyClassNames . FULLWIDTH ] : this . props . fullwidth , } , this . state . foundationClasses . toJS ( ) ] ; } } class ContainerAdapterImpl extends ContainerAdapter { private element : Meta ; constructor ( element ) { super ( ) ; this . element = element ; } public addClass ( className ) { this . element . setState ( ( state ) => ( { foundationClasses : state . foundationClasses . add ( className ) , } ) ) ; } public removeClass ( className ) { this . element . setState ( ( state ) => ( { foundationClasses : state . foundationClasses . remove ( className ) , } ) ) ; } } export default class Container extends DefaultComponentBase < React . HTMLProps < HTMLDivElement > , MetaProps , { } > { public static Meta = Meta ; protected getMetaComponent ( ) { return Meta ; } protected getMetaPropNames ( ) { return [ "disabled" , "multiline" , "fullwidth" , ] ; } protected getChildComponent ( ) { return "div" ; } }