PromucFlow_constructor/app/client/src/components/propertyControls/InputTextControl.tsx

114 lines
3.1 KiB
TypeScript
Raw Normal View History

import React from "react";
2019-11-14 09:28:51 +00:00
import _ from "lodash";
import BaseControl, { ControlProps } from "./BaseControl";
2019-11-14 09:28:51 +00:00
import {
ControlWrapper,
StyledInputGroup,
StyledValidationError,
} from "./StyledControls";
import { InputType } from "../../widgets/InputWidget";
2019-11-05 05:09:50 +00:00
import { ControlType } from "../../constants/PropertyControlConstants";
2019-11-14 09:28:51 +00:00
import { isDynamicValue } from "../../utils/DynamicBindingUtils";
import { ERROR_CODES } from "../../constants/validationErrorCodes";
type InputTextControlType = InputType | "OBJECT" | "ARRAY" | "BOOLEAN";
class InputTextControl extends BaseControl<InputControlProps> {
render() {
return (
<ControlWrapper>
<label>{this.props.label}</label>
<StyledInputGroup
2019-10-31 05:28:11 +00:00
type={this.isNumberType(this.props.inputType) ? "number" : "text"}
onChange={this.onTextChange}
2019-10-31 05:28:11 +00:00
placeholder={this.props.placeholderText}
defaultValue={this.props.propertyValue}
/>
2019-11-14 09:28:51 +00:00
{this.props.propertyError && (
<StyledValidationError>
{this.props.propertyError}
</StyledValidationError>
)}
</ControlWrapper>
);
}
2019-11-14 09:28:51 +00:00
isNumberType(inputType: InputTextControlType): boolean {
2019-10-31 05:28:11 +00:00
switch (inputType) {
case "CURRENCY":
case "INTEGER":
case "NUMBER":
case "PHONE_NUMBER":
return true;
default:
return false;
}
}
2019-11-14 09:28:51 +00:00
isStringType(inputType: InputTextControlType): boolean {
switch (inputType) {
case "TEXT":
case "EMAIL":
case "PASSWORD":
case "SEARCH":
return true;
default:
return false;
}
}
onTextChange = (event: React.ChangeEvent<HTMLInputElement>) => {
2019-11-14 13:13:35 +00:00
const { inputType } = this.props;
2019-11-14 09:28:51 +00:00
let value: string | number = event.target.value;
2019-11-14 13:13:35 +00:00
if (this.isNumberType(inputType)) {
2019-11-14 09:28:51 +00:00
value = _.toNumber(value);
}
2019-11-14 14:26:23 +00:00
// if (this.validateInput(value)) {
this.updateProperty(this.props.propertyName, value);
// }
};
getControlType(): ControlType {
return "INPUT_TEXT";
}
2019-11-14 09:28:51 +00:00
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;
2019-11-14 09:28:51 +00:00
inputType: InputTextControlType;
isDisabled?: boolean;
}
export default InputTextControl;