2019-09-12 08:11:25 +00:00
|
|
|
import React from "react";
|
2019-09-13 10:45:49 +00:00
|
|
|
import BaseWidget, { WidgetProps, WidgetState } from "./BaseWidget";
|
2019-11-13 07:00:25 +00:00
|
|
|
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";
|
2020-03-16 07:59:07 +00:00
|
|
|
import {
|
|
|
|
|
WidgetPropertyValidationType,
|
|
|
|
|
BASE_WIDGET_VALIDATION,
|
|
|
|
|
} from "utils/ValidationFactory";
|
2019-11-22 13:12:39 +00:00
|
|
|
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";
|
2020-04-17 16:15:09 +00:00
|
|
|
import _ from "lodash";
|
2019-09-12 08:11:25 +00:00
|
|
|
|
2020-04-13 08:24:13 +00:00
|
|
|
class InputWidget extends BaseWidget<InputWidgetProps, InputWidgetState> {
|
2020-04-17 16:15:09 +00:00
|
|
|
debouncedHandleTextChanged = _.debounce(
|
|
|
|
|
this.handleTextChanged.bind(this),
|
|
|
|
|
200,
|
|
|
|
|
);
|
2020-04-13 08:24:13 +00:00
|
|
|
constructor(props: InputWidgetProps) {
|
|
|
|
|
super(props);
|
|
|
|
|
this.state = {
|
2020-04-17 16:15:09 +00:00
|
|
|
text: props.text,
|
2020-04-13 08:24:13 +00:00
|
|
|
};
|
|
|
|
|
}
|
2019-11-22 13:12:39 +00:00
|
|
|
static getPropertyValidationMap(): WidgetPropertyValidationType {
|
|
|
|
|
return {
|
2020-03-16 07:59:07 +00:00
|
|
|
...BASE_WIDGET_VALIDATION,
|
2019-11-22 13:12:39 +00:00
|
|
|
inputType: VALIDATION_TYPES.TEXT,
|
|
|
|
|
defaultText: VALIDATION_TYPES.TEXT,
|
|
|
|
|
isDisabled: VALIDATION_TYPES.BOOLEAN,
|
|
|
|
|
text: VALIDATION_TYPES.TEXT,
|
2020-04-03 02:42:45 +00:00
|
|
|
regex: VALIDATION_TYPES.REGEX,
|
2019-11-22 13:12:39 +00:00
|
|
|
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-04-02 07:53:03 +00:00
|
|
|
onTextChanged: VALIDATION_TYPES.ACTION_SELECTOR,
|
2020-03-06 09:45:21 +00:00
|
|
|
isRequired: VALIDATION_TYPES.BOOLEAN,
|
2020-03-16 07:59:07 +00:00
|
|
|
isValid: 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 {
|
2020-04-17 16:15:09 +00:00
|
|
|
isValid: `{{!!(this.isRequired ? this.text && this.text.length > 0 ? this.regex ? new RegExp(this.regex).test(this.text) : true : false : this.regex ? new RegExp(this.regex).test(this.text) : true)}}`,
|
2020-06-09 12:04:38 +00:00
|
|
|
value: `{{this.text}}`,
|
2020-03-06 09:45:21 +00:00
|
|
|
};
|
2019-10-31 05:28:11 +00:00
|
|
|
}
|
|
|
|
|
|
2020-04-17 16:15:09 +00:00
|
|
|
static getDefaultPropertiesMap(): Record<string, string> {
|
|
|
|
|
return {
|
|
|
|
|
text: "defaultText",
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static getMetaPropertiesMap(): Record<string, any> {
|
|
|
|
|
return {
|
|
|
|
|
text: undefined,
|
|
|
|
|
isFocused: false,
|
|
|
|
|
isDirty: false,
|
|
|
|
|
};
|
2019-10-31 05:28:11 +00:00
|
|
|
}
|
|
|
|
|
|
2020-03-13 07:24:03 +00:00
|
|
|
componentDidUpdate(prevProps: InputWidgetProps) {
|
|
|
|
|
super.componentDidUpdate(prevProps);
|
2020-03-16 15:42:39 +00:00
|
|
|
if (
|
2020-04-17 16:15:09 +00:00
|
|
|
prevProps.text !== this.props.text &&
|
|
|
|
|
this.props.defaultText === this.props.text
|
2020-03-16 15:42:39 +00:00
|
|
|
) {
|
2020-04-17 16:15:09 +00:00
|
|
|
const text = this.props.text;
|
|
|
|
|
this.setState({ text });
|
2020-03-13 07:24:03 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-31 05:28:11 +00:00
|
|
|
onValueChange = (value: string) => {
|
2020-04-13 08:24:13 +00:00
|
|
|
this.setState({ text: value }, () => {
|
|
|
|
|
this.updateWidgetMetaProperty("text", value);
|
|
|
|
|
});
|
2020-03-06 09:45:21 +00:00
|
|
|
if (!this.props.isDirty) {
|
|
|
|
|
this.updateWidgetMetaProperty("isDirty", true);
|
|
|
|
|
}
|
2020-04-17 16:15:09 +00:00
|
|
|
this.debouncedHandleTextChanged();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
handleTextChanged() {
|
2020-02-18 10:41:52 +00:00
|
|
|
if (this.props.onTextChanged) {
|
|
|
|
|
super.executeAction({
|
|
|
|
|
dynamicString: this.props.onTextChanged,
|
|
|
|
|
event: {
|
|
|
|
|
type: EventType.ON_TEXT_CHANGE,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-04-17 16:15:09 +00:00
|
|
|
}
|
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-04-13 08:24:13 +00:00
|
|
|
const value = this.state.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;
|
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";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-14 05:35:16 +00:00
|
|
|
export const InputTypes: { [key: string]: string } = {
|
|
|
|
|
TEXT: "TEXT",
|
|
|
|
|
NUMBER: "NUMBER",
|
|
|
|
|
INTEGER: "INTEGER",
|
|
|
|
|
PHONE_NUMBER: "PHONE_NUMBER",
|
|
|
|
|
EMAIL: "EMAIL",
|
|
|
|
|
PASSWORD: "PASSWORD",
|
|
|
|
|
CURRENCY: "CURRENCY",
|
|
|
|
|
SEARCH: "SEARCH",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type InputType = typeof InputTypes[keyof typeof InputTypes];
|
2019-09-12 08:11:25 +00:00
|
|
|
|
2019-09-19 11:29:24 +00:00
|
|
|
export interface InputValidator {
|
|
|
|
|
validationRegex: string;
|
|
|
|
|
errorMessage: string;
|
|
|
|
|
}
|
2019-09-13 10:45:49 +00:00
|
|
|
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;
|
2020-04-17 16:15:09 +00:00
|
|
|
text: string;
|
2019-10-31 05:28:11 +00:00
|
|
|
regex?: string;
|
|
|
|
|
errorMessage?: string;
|
2019-09-19 11:29:24 +00:00
|
|
|
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;
|
2019-09-19 11:29:24 +00:00
|
|
|
inputValidators: InputValidator[];
|
2020-03-06 09:45:21 +00:00
|
|
|
isValid: boolean;
|
2019-09-13 10:45:49 +00:00
|
|
|
focusIndex?: number;
|
2019-09-19 11:29:24 +00:00
|
|
|
isAutoFocusEnabled?: boolean;
|
2020-03-06 09:45:21 +00:00
|
|
|
isRequired?: boolean;
|
|
|
|
|
isFocused?: boolean;
|
|
|
|
|
isDirty?: boolean;
|
2019-09-12 08:11:25 +00:00
|
|
|
}
|
|
|
|
|
|
2020-04-13 08:24:13 +00:00
|
|
|
interface InputWidgetState extends WidgetState {
|
|
|
|
|
text: string;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-12 08:11:25 +00:00
|
|
|
export default InputWidget;
|