2020-03-06 09:45:21 +00:00
|
|
|
import React from "react";
|
|
|
|
|
import _ from "lodash";
|
|
|
|
|
import { WidgetProps } from "./BaseWidget";
|
|
|
|
|
import { WidgetType } from "constants/WidgetConstants";
|
2020-06-09 12:04:38 +00:00
|
|
|
import ContainerWidget, { ContainerWidgetProps } from "widgets/ContainerWidget";
|
2020-03-06 09:45:21 +00:00
|
|
|
import { ContainerComponentProps } from "components/designSystems/appsmith/ContainerComponent";
|
2020-06-09 12:04:38 +00:00
|
|
|
import shallowEqual from "shallowequal";
|
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;
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
handleResetInputs = () => {
|
|
|
|
|
super.resetChildrenMetaProperty(this.props.widgetId);
|
|
|
|
|
};
|
|
|
|
|
|
2020-06-09 12:04:38 +00:00
|
|
|
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]);
|
|
|
|
|
if (!shallowEqual(formData, this.props.data)) {
|
|
|
|
|
this.updateWidgetMetaProperty("data", formData);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getFormData(formWidget: ContainerWidgetProps<WidgetProps>) {
|
|
|
|
|
const formData: any = {};
|
|
|
|
|
if (formWidget.children)
|
2020-06-17 13:53:01 +00:00
|
|
|
formWidget.children.forEach(widgetData => {
|
2020-06-09 13:10:42 +00:00
|
|
|
if (widgetData.value) {
|
|
|
|
|
formData[widgetData.widgetName] = widgetData.value;
|
|
|
|
|
}
|
2020-06-09 12:04:38 +00:00
|
|
|
});
|
|
|
|
|
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;
|
2020-06-09 12:04:38 +00:00
|
|
|
data: object;
|
2020-03-06 09:45:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default FormWidget;
|