import * as React from "react"; import {ReactElement} from "react"; const cssStyle = require("./InputField.scss"); export interface BoxedInputFieldProperties { prefix?: string; placeholder?: string; disabled?: boolean; editable?: boolean; defaultValue?: string; rightIcon?: () => ReactElement; leftIcon?: () => ReactElement; inputBox?: () => ReactElement; /* if set the onChange and onInput will not work anymore! */ isInvalid?: boolean; className?: string; size?: "normal" | "large" | "small"; onFocus?: () => void; onBlur?: () => void; onChange?: (newValue: string) => void; onInput?: (newValue: string) => void; } export interface BoxedInputFieldState { disabled?: boolean; defaultValue?: string; isInvalid?: boolean; value?: string; } export class BoxedInputField extends React.Component { private refInput = React.createRef(); private inputEdited = false; constructor(props) { super(props); this.state = {}; } render() { return (
this.onInputBlur()} > {this.props.leftIcon ? this.props.leftIcon() : ""} {this.props.prefix ? {this.props.prefix} : undefined} {this.props.inputBox ? {this.props.inputBox()} : this.props.onInput(event.currentTarget.value))} onKeyDown={e => this.onKeyDown(e)} />} {this.props.rightIcon ? this.props.rightIcon() : ""}
) } focusInput() { this.refInput.current?.focus(); } private onKeyDown(event: React.KeyboardEvent) { this.inputEdited = true; if(event.key === "Enter") this.refInput.current?.blur(); } private onInputBlur() { if(this.props.onChange && this.inputEdited) { this.inputEdited = false; this.props.onChange(this.refInput.current.value); } if(this.props.onBlur) this.props.onBlur(); } }