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

118 lines
3.5 KiB
TypeScript
Raw Normal View History

2020-03-06 09:45:21 +00:00
import React from "react";
import _ from "lodash";
import { WidgetProps } from "./BaseWidget";
import { WidgetType } from "constants/WidgetConstants";
import ContainerWidget, { ContainerWidgetProps } from "widgets/ContainerWidget";
2020-03-06 09:45:21 +00:00
import { ContainerComponentProps } from "components/designSystems/appsmith/ContainerComponent";
import * as Sentry from "@sentry/react";
2020-10-06 09:01:51 +00:00
import withMeta from "./MetaHOC";
import { VALIDATION_TYPES } from "constants/WidgetValidation";
2020-03-06 09:45:21 +00:00
class FormWidget extends ContainerWidget {
static getPropertyPaneConfig() {
return [
{
sectionName: "General",
children: [
{
propertyName: "backgroundColor",
label: "Background Color",
helpText: "Use a html color name, HEX, RGB or RGBA value",
placeholderText: "#FFFFFF / Gray / rgb(255, 99, 71)",
controlType: "INPUT_TEXT",
isBindProperty: true,
isTriggerProperty: false,
validation: VALIDATION_TYPES.TEXT,
},
{
helpText: "Controls the visibility of the widget",
propertyName: "isVisible",
label: "Visible",
controlType: "SWITCH",
isJSConvertible: true,
isBindProperty: true,
isTriggerProperty: false,
validation: VALIDATION_TYPES.BOOLEAN,
},
{
propertyName: "shouldScrollContents",
label: "Scroll Contents",
controlType: "SWITCH",
isBindProperty: false,
isTriggerProperty: false,
},
],
},
];
}
2020-03-06 09:45:21 +00:00
checkInvalidChildren = (children: WidgetProps[]): boolean => {
2020-12-24 04:32:25 +00:00
return _.some(children, (child) => {
if ("children" in child) {
return this.checkInvalidChildren(child.children);
}
if ("isValid" in child) {
return !child.isValid;
}
2020-03-06 09:45:21 +00:00
return false;
});
};
handleResetInputs = () => {
super.resetChildrenMetaProperty(this.props.widgetId);
};
componentDidMount() {
super.componentDidMount();
this.updateFormData();
}
componentDidUpdate(prevProps: ContainerWidgetProps<any>) {
super.componentDidUpdate(prevProps);
this.updateFormData();
}
updateFormData() {
if (this.props.children) {
const formData = this.getFormData(this.props.children[0] as WidgetProps);
2020-11-10 07:53:18 +00:00
if (!_.isEqual(formData, this.props.data)) {
2020-10-06 09:01:51 +00:00
this.props.updateWidgetMetaProperty("data", formData);
}
}
}
getFormData(formWidget: ContainerWidgetProps<WidgetProps>) {
const formData: any = {};
if (formWidget.children)
2020-12-24 04:32:25 +00:00
formWidget.children.forEach((widgetData) => {
2020-06-09 13:10:42 +00:00
if (widgetData.value) {
formData[widgetData.widgetName] = widgetData.value;
}
});
return formData;
}
2020-03-06 09:45:21 +00:00
renderChildWidget(childWidgetData: WidgetProps): React.ReactNode {
2020-03-31 07:42:25 +00:00
if (childWidgetData.children) {
const isInvalid = this.checkInvalidChildren(childWidgetData.children);
childWidgetData.children.forEach((grandChild: WidgetProps) => {
if (isInvalid) grandChild.isFormValid = false;
// Add submit and reset handlers
grandChild.onReset = this.handleResetInputs;
});
2020-03-06 09:45:21 +00:00
}
return super.renderChildWidget(childWidgetData);
}
getWidgetType(): WidgetType {
return "FORM_WIDGET";
}
}
export interface FormWidgetProps extends ContainerComponentProps {
name: string;
data: Record<string, unknown>;
2020-03-06 09:45:21 +00:00
}
export default FormWidget;
2020-10-06 09:01:51 +00:00
export const ProfiledFormWidget = Sentry.withProfiler(withMeta(FormWidget));