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 from "widgets/ContainerWidget";
|
|
|
|
|
import { ContainerComponentProps } from "components/designSystems/appsmith/ContainerComponent";
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default FormWidget;
|