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

101 lines
2.5 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";
import InputComponent from "components/designSystems/blueprint/InputComponent";
import { ActionPayload } from "constants/ActionConstants";
2019-09-12 08:11:25 +00:00
class InputWidget extends BaseWidget<InputWidgetProps, WidgetState> {
2019-10-31 05:28:11 +00:00
regex = new RegExp("");
componentDidMount() {
super.componentDidMount();
if (this.props.regex) {
try {
this.regex = new RegExp(this.props.regex);
} catch (e) {
console.log("invalid regex");
}
}
}
componentDidUpdate(prevProps: InputWidgetProps) {
super.componentDidUpdate(prevProps);
if (this.props.regex !== prevProps.regex && this.props.regex) {
try {
this.regex = new RegExp(this.props.regex);
} catch (e) {
console.log("invalid regex");
}
}
}
onValueChange = (value: string) => {
this.context.updateWidgetProperty(this.props.widgetId, "text", value);
2019-11-06 12:12:41 +00:00
super.executeAction(this.props.onTextChanged);
2019-10-31 05:28:11 +00:00
};
2019-09-12 08:11:25 +00:00
getPageView() {
const errorMessage =
this.props.regex &&
this.props.text &&
this.props.text.length > 0 &&
!this.regex.test(this.props.text)
? this.props.errorMessage
: undefined;
2019-10-30 10:23:20 +00:00
return (
<InputComponent
2019-10-31 05:28:11 +00:00
onValueChange={this.onValueChange}
2019-10-30 10:23:20 +00:00
widgetId={this.props.widgetId}
errorMessage={errorMessage}
2019-10-30 10:23:20 +00:00
inputType={this.props.inputType}
disabled={this.props.isDisabled}
2019-10-31 05:28:11 +00:00
maxChars={this.props.maxChars}
label={this.props.label}
2019-10-30 10:23:20 +00:00
defaultValue={this.props.defaultText}
maxNum={this.props.maxNum}
minNum={this.props.minNum}
placeholder={this.props.placeholderText}
stepSize={1}
/>
);
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;
2019-11-12 07:57:12 +00:00
onTextChanged?: ActionPayload[];
2019-09-12 08:11:25 +00:00
label: string;
inputValidators: InputValidator[];
focusIndex?: number;
isAutoFocusEnabled?: boolean;
2019-09-12 08:11:25 +00:00
}
export default InputWidget;