import React from 'react'; import classNames from 'classnames'; import { SessionIconButton, SessionIconSize, SessionIconType } from './icon'; interface Props { label: string; error?: string; type: string; value?: string; placeholder: string; maxLength?: number; enableShowHide?: boolean; onValueChanged?: any; onEnterPressed?: any; } interface State { inputValue: string; forceShow: boolean; } export class SessionInput extends React.PureComponent { constructor(props: any) { super(props); this.updateInputValue = this.updateInputValue.bind(this); this.renderShowHideButton = this.renderShowHideButton.bind(this); this.state = { inputValue: '', forceShow: false, }; } public render() { const { placeholder, type, value, maxLength, enableShowHide, error } = this.props; const { forceShow } = this.state; const correctType = forceShow ? 'text' : type; return (
{error ? this.renderError() : this.renderLabel()} { this.updateInputValue(e); }} className={classNames( enableShowHide ? 'session-input-floating-label-show-hide' : '' )} onKeyPress={event => { event.persist(); if (event.key === 'Enter' && this.props.onEnterPressed) { this.props.onEnterPressed(); } }} /> {enableShowHide && this.renderShowHideButton()}
); } private renderLabel() { const { inputValue } = this.state; const { label } = this.props; return ( ); } private renderError() { const { error } = this.props; return ( ); } private renderShowHideButton() { return ( { this.setState({ forceShow: !this.state.forceShow, }); }} /> ); } private updateInputValue(e: any) { e.preventDefault(); this.setState({ inputValue: e.target.value, }); if (this.props.onValueChanged) { this.props.onValueChanged(e.target.value); } } }