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

81 lines
2.3 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";
2020-03-06 09:45:21 +00:00
class FormWidget extends ContainerWidget {
checkInvalidChildren = (children: WidgetProps[]): boolean => {
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]);
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)
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));