PromucFlow_constructor/app/client/src/widgets/InputWidget.tsx

177 lines
5.1 KiB
TypeScript
Raw Normal View History

2019-09-12 08:11:25 +00:00
import React from "react";
import BaseWidget, { WidgetProps, WidgetState } from "./BaseWidget";
import { WidgetType } from "constants/WidgetConstants";
2020-03-06 09:45:21 +00:00
import InputComponent, {
InputComponentProps,
} from "components/designSystems/blueprint/InputComponent";
2020-02-18 10:41:52 +00:00
import { EventType } from "constants/ActionConstants";
2019-11-22 13:12:39 +00:00
import { WidgetPropertyValidationType } from "utils/ValidationFactory";
import { VALIDATION_TYPES } from "constants/WidgetValidation";
2020-03-06 09:45:21 +00:00
import { FIELD_REQUIRED_ERROR } from "constants/messages";
import {
DerivedPropertiesMap,
TriggerPropertiesMap,
} from "utils/WidgetFactory";
2019-09-12 08:11:25 +00:00
class InputWidget extends BaseWidget<InputWidgetProps, WidgetState> {
2019-11-22 13:12:39 +00:00
static getPropertyValidationMap(): WidgetPropertyValidationType {
return {
inputType: VALIDATION_TYPES.TEXT,
defaultText: VALIDATION_TYPES.TEXT,
isDisabled: VALIDATION_TYPES.BOOLEAN,
text: VALIDATION_TYPES.TEXT,
regex: VALIDATION_TYPES.TEXT,
errorMessage: VALIDATION_TYPES.TEXT,
placeholderText: VALIDATION_TYPES.TEXT,
maxChars: VALIDATION_TYPES.NUMBER,
minNum: VALIDATION_TYPES.NUMBER,
maxNum: VALIDATION_TYPES.NUMBER,
label: VALIDATION_TYPES.TEXT,
inputValidators: VALIDATION_TYPES.ARRAY,
focusIndex: VALIDATION_TYPES.NUMBER,
isAutoFocusEnabled: VALIDATION_TYPES.BOOLEAN,
2020-03-06 09:45:21 +00:00
isRequired: VALIDATION_TYPES.BOOLEAN,
2019-11-22 13:12:39 +00:00
};
}
2020-02-18 10:41:52 +00:00
static getTriggerPropertyMap(): TriggerPropertiesMap {
return {
onTextChanged: true,
};
}
2019-10-31 05:28:11 +00:00
2020-03-06 09:45:21 +00:00
static getDerivedPropertiesMap(): DerivedPropertiesMap {
return {
isValid: `{{
function() {
2020-03-06 14:29:10 +00:00
if(this.isRequired) {
if(!this.text || this.text.length === 0) {
return false
2020-03-06 09:45:21 +00:00
}
}
2020-03-06 14:29:10 +00:00
if(this.regex) {
return new RegExp(this.regex).test(this.text);
}
2020-03-06 09:45:21 +00:00
return true
}()
}}`,
};
2019-10-31 05:28:11 +00:00
}
2020-03-06 09:45:21 +00:00
componentDidMount() {
super.componentDidMount();
if (this.props.defaultText) {
this.updateWidgetMetaProperty("text", this.props.defaultText);
2019-10-31 05:28:11 +00:00
}
}
2020-03-13 07:24:03 +00:00
componentDidUpdate(prevProps: InputWidgetProps) {
super.componentDidUpdate(prevProps);
if (this.props.defaultText) {
if (
(this.props.text !== prevProps.text && this.props.text === undefined) ||
this.props.defaultText !== prevProps.defaultText
) {
this.updateWidgetMetaProperty("text", this.props.defaultText);
}
}
}
2019-10-31 05:28:11 +00:00
onValueChange = (value: string) => {
2020-03-06 09:45:21 +00:00
this.updateWidgetMetaProperty("text", value);
if (!this.props.isDirty) {
this.updateWidgetMetaProperty("isDirty", true);
}
2020-02-18 10:41:52 +00:00
if (this.props.onTextChanged) {
super.executeAction({
dynamicString: this.props.onTextChanged,
event: {
type: EventType.ON_TEXT_CHANGE,
},
});
}
2019-10-31 05:28:11 +00:00
};
2020-03-06 09:45:21 +00:00
handleFocusChange = (focusState: boolean) => {
this.updateWidgetMetaProperty("isFocused", focusState);
};
2019-09-12 08:11:25 +00:00
getPageView() {
2020-03-06 09:45:21 +00:00
const value = this.props.text || "";
2020-03-06 14:29:10 +00:00
const isInvalid =
"isValid" in this.props && !this.props.isValid && !!this.props.isDirty;
2020-03-06 09:45:21 +00:00
const conditionalProps: Partial<InputComponentProps> = {};
conditionalProps.errorMessage = this.props.errorMessage;
if (this.props.isRequired && value.length === 0) {
conditionalProps.errorMessage = FIELD_REQUIRED_ERROR;
}
if (this.props.maxChars) conditionalProps.maxChars = this.props.maxChars;
if (this.props.maxNum) conditionalProps.maxNum = this.props.maxNum;
if (this.props.minNum) conditionalProps.minNum = this.props.minNum;
if (this.props.isRequired) conditionalProps.label = `${this.props.label} *`;
2019-10-30 10:23:20 +00:00
return (
<InputComponent
2020-03-06 09:45:21 +00:00
value={value}
isInvalid={isInvalid}
2019-10-31 05:28:11 +00:00
onValueChange={this.onValueChange}
2019-10-30 10:23:20 +00:00
widgetId={this.props.widgetId}
inputType={this.props.inputType}
disabled={this.props.isDisabled}
2019-10-31 05:28:11 +00:00
label={this.props.label}
2019-10-30 10:23:20 +00:00
defaultValue={this.props.defaultText}
placeholder={this.props.placeholderText}
2019-12-03 04:41:10 +00:00
isLoading={this.props.isLoading}
2020-02-13 09:32:24 +00:00
multiline={
2020-02-06 07:01:25 +00:00
this.props.bottomRow - this.props.topRow > 1 &&
this.props.inputType === "TEXT"
}
2019-10-30 10:23:20 +00:00
stepSize={1}
2020-03-06 09:45:21 +00:00
onFocusChange={this.handleFocusChange}
showError={!!this.props.isFocused}
{...conditionalProps}
2019-10-30 10:23:20 +00:00
/>
);
2019-09-12 08:11:25 +00:00
}
getWidgetType(): WidgetType {
return "INPUT_WIDGET";
}
}
export type InputType =
| "TEXT"
| "NUMBER"
| "INTEGER"
| "PHONE_NUMBER"
| "EMAIL"
| "PASSWORD"
| "CURRENCY"
| "SEARCH";
2019-09-12 08:11:25 +00:00
export interface InputValidator {
validationRegex: string;
errorMessage: string;
}
export interface InputWidgetProps extends WidgetProps {
2019-09-12 08:11:25 +00:00
inputType: InputType;
defaultText?: string;
2019-10-30 10:23:20 +00:00
isDisabled?: boolean;
2019-10-31 05:28:11 +00:00
text?: string;
regex?: string;
errorMessage?: string;
placeholderText?: string;
maxChars?: number;
2019-10-30 10:23:20 +00:00
minNum?: number;
maxNum?: number;
2020-02-18 10:41:52 +00:00
onTextChanged?: string;
2019-09-12 08:11:25 +00:00
label: string;
inputValidators: InputValidator[];
2020-03-06 09:45:21 +00:00
isValid: boolean;
focusIndex?: number;
isAutoFocusEnabled?: boolean;
2020-03-06 09:45:21 +00:00
isRequired?: boolean;
isFocused?: boolean;
isDirty?: boolean;
2019-09-12 08:11:25 +00:00
}
export default InputWidget;