import React from "react"; import _ from "lodash"; import BaseControl, { ControlProps } from "./BaseControl"; import { ControlWrapper, StyledInputGroup, StyledValidationError, } from "./StyledControls"; import { InputType } from "../../widgets/InputWidget"; import { ControlType } from "../../constants/PropertyControlConstants"; import { isDynamicValue } from "../../utils/DynamicBindingUtils"; import { ERROR_CODES } from "../../constants/validationErrorCodes"; type InputTextControlType = InputType | "OBJECT" | "ARRAY" | "BOOLEAN"; class InputTextControl extends BaseControl { render() { return ( {this.props.propertyError && ( {this.props.propertyError} )} ); } isNumberType(inputType: InputTextControlType): boolean { switch (inputType) { case "CURRENCY": case "INTEGER": case "NUMBER": case "PHONE_NUMBER": return true; default: return false; } } isStringType(inputType: InputTextControlType): boolean { switch (inputType) { case "TEXT": case "EMAIL": case "PASSWORD": case "SEARCH": return true; default: return false; } } onTextChange = (event: React.ChangeEvent) => { const { inputType } = this.props; let value: string | number = event.target.value; if (this.isNumberType(inputType)) { value = _.toNumber(value); } // if (this.validateInput(value)) { this.updateProperty(this.props.propertyName, value); // } }; getControlType(): ControlType { return "INPUT_TEXT"; } validateInput(inputValue: any): boolean { const { getDynamicValue, inputType, setPropertyValidation, propertyName, } = this.props; let value = inputValue; if (isDynamicValue(inputValue)) { value = getDynamicValue(inputValue); } if (this.isNumberType(inputType) && !_.isNumber(value)) { setPropertyValidation(propertyName, ERROR_CODES.TYPE_ERROR); return false; } if (this.isStringType(inputType) && !_.isString(value)) { setPropertyValidation(propertyName, ERROR_CODES.TYPE_ERROR); return false; } if (inputType === "ARRAY" && !Array.isArray(value)) { setPropertyValidation(propertyName, ERROR_CODES.TYPE_ERROR); return false; } if (inputType === "OBJECT" && !_.isObject(value)) { setPropertyValidation(propertyName, ERROR_CODES.TYPE_ERROR); return false; } setPropertyValidation(propertyName, ERROR_CODES.NO_ERROR); return true; } } export interface InputControlProps extends ControlProps { placeholderText: string; inputType: InputTextControlType; isDisabled?: boolean; } export default InputTextControl;