import {MDCTextField} from '@material/textfield'; import {Component, h} from 'preact'; import MaterialComponent, { MaterialComponentProps } from '../Base/MaterialComponent'; import {SoftMerge} from '../Base/types'; import Icon from '../Icon'; export interface IHelperTextProps { persistent?: boolean; 'validation-msg'?: boolean; } export interface IHelperTextState {} export class HelperText extends MaterialComponent< IHelperTextProps, IHelperTextState > { protected componentName = 'text-field-helper-text'; protected mdcProps = ['persistent', 'validation-msg']; protected materialDom(props) { return ( ); } } export interface ILabelProps {} export interface ILabelState {} export class Label extends MaterialComponent { protected componentName = 'floating-label'; protected mdcProps = []; protected materialDom(props) { return ; } } export interface ITextFieldInputProps { fullwidth?: boolean; textarea?: boolean; dense?: boolean; box?: boolean; disabled?: boolean; outlined?: boolean; cssLabel?: string; leadingIcon?: string; trailingIcon?: string; outerStyle?: {[key: string]: string}; onInit?: (c: MDCTextField) => any | void; valid?: boolean; value?: string; } export interface ITextFieldInputState { jsComponentAttached: boolean; } export class TextFieldInput extends MaterialComponent< ITextFieldInputProps, ITextFieldInputState > { public static readonly defaultProps = { valid: true }; public state = { jsComponentAttached: false }; public MDComponent?: MDCTextField; protected componentName = 'text-field'; protected mdcProps = [ 'fullwidth', 'textarea', 'dense', 'disabled', 'box', 'outlined' ]; protected mdcNotifyProps = ['valid', 'disabled']; public componentDidMount() { super.componentDidMount(); this.setState( { jsComponentAttached: true }, () => { if (this.control) { this.MDComponent = new MDCTextField(this.control); if (this.props.onInit) { this.props.onInit(this.MDComponent); } if (this.props.value) { this.MDComponent.value = this.props.value; } } this.afterComponentDidMount(); } ); } public componentWillReceiveProps(nextProps: ITextFieldInputProps) { super.componentWillReceiveProps(nextProps); if ( this.MDComponent && nextProps.value && this.props.value !== nextProps.value && this.MDComponent.value !== nextProps.value ) { this.MDComponent.value = nextProps.value; } } public componentWillUnmount() { super.componentWillUnmount(); if (this.MDComponent) { this.MDComponent.destroy(); } } public getValue() { return this.MDComponent ? this.MDComponent.value : null; } protected materialDom(allprops) { let {className, outerStyle, outlined, ...props} = allprops; className = className || ''; if ('leadingIcon' in props) { className += ' mdc-text-field--box mdc-text-field--with-leading-icon'; } if ('trailingIcon' in props) { className += ' mdc-text-field--box mdc-text-field--with-trailing-icon'; } if ('value' in props && this.state.jsComponentAttached) { className = [className, 'mdc-text-field--upgraded'].join(' '); } if (props.label && props.fullwidth) { console.log( 'Passing a "label" prop is not supported when using a "fullwidth" text field.' ); } // noinspection RequiredAttributes return (
{props.leadingIcon ? ( {props.leadingIcon} ) : null} {props.textarea ? (