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

97 lines
2.6 KiB
TypeScript
Raw Normal View History

import React, { lazy, Suspense } from "react";
2020-03-20 11:17:30 +00:00
import BaseWidget, { WidgetProps, WidgetState } from "./BaseWidget";
import { WidgetType } from "constants/WidgetConstants";
import { EventType } from "constants/ActionConstants";
import { WidgetPropertyValidationType } from "utils/ValidationFactory";
import { VALIDATION_TYPES } from "constants/WidgetValidation";
import { TriggerPropertiesMap } from "utils/WidgetFactory";
const RichtextEditorComponent = lazy(() =>
import("components/designSystems/appsmith/RichTextEditorComponent"),
);
2020-03-20 11:17:30 +00:00
class RichTextEditorWidget extends BaseWidget<
RichTextEditorWidgetProps,
WidgetState
> {
static getPropertyValidationMap(): WidgetPropertyValidationType {
return {
placeholder: VALIDATION_TYPES.TEXT,
defaultValue: VALIDATION_TYPES.TEXT,
isDisabled: VALIDATION_TYPES.BOOLEAN,
isVisible: VALIDATION_TYPES.BOOLEAN,
2020-03-31 10:40:52 +00:00
onTextChange: VALIDATION_TYPES.ACTION_SELECTOR,
2020-03-20 11:17:30 +00:00
};
}
static getTriggerPropertyMap(): TriggerPropertiesMap {
return {
onTextChange: true,
};
}
componentDidMount() {
super.componentDidMount();
2020-04-15 11:42:11 +00:00
if (this.props.defaultText) {
this.updateWidgetMetaProperty("text", this.props.defaultText);
2020-03-20 11:17:30 +00:00
}
}
componentDidUpdate(prevProps: RichTextEditorWidgetProps) {
super.componentDidUpdate(prevProps);
2020-04-15 11:42:11 +00:00
if (this.props.defaultText) {
if (this.props.defaultText !== prevProps.defaultText) {
this.updateWidgetMetaProperty("text", this.props.defaultText);
2020-03-20 11:17:30 +00:00
}
}
}
2020-04-15 11:42:11 +00:00
onValueChange = (text: string) => {
this.updateWidgetMetaProperty("text", text);
2020-03-20 11:17:30 +00:00
if (this.props.onTextChange) {
super.executeAction({
dynamicString: this.props.onTextChange,
event: {
type: EventType.ON_TEXT_CHANGE,
},
});
}
};
getPageView() {
return (
<Suspense fallback={null}>
<RichtextEditorComponent
onValueChange={this.onValueChange}
defaultValue={this.props.text}
widgetId={this.props.widgetId}
placeholder={this.props.placeholder}
key={this.props.widgetId}
isDisabled={this.props.isDisabled}
isVisible={this.props.isVisible}
/>
</Suspense>
2020-03-20 11:17:30 +00:00
);
}
getWidgetType(): WidgetType {
return "RICH_TEXT_EDITOR_WIDGET";
}
}
export interface InputValidator {
validationRegex: string;
errorMessage: string;
}
export interface RichTextEditorWidgetProps extends WidgetProps {
2020-04-15 11:42:11 +00:00
defaultText?: string;
2020-03-20 11:17:30 +00:00
text?: string;
placeholder?: string;
onTextChange?: string;
isDisabled?: boolean;
isVisible?: boolean;
}
export default RichTextEditorWidget;