@@ -2,7 +2,10 @@ import React from "react";
22import BaseWidget , { WidgetProps , WidgetState } from "widgets/BaseWidget" ;
33import { WidgetType } from "constants/WidgetConstants" ;
44import ButtonComponent , { ButtonType } from "../component" ;
5- import { EventType } from "constants/AppsmithActionConstants/ActionConstants" ;
5+ import {
6+ EventType ,
7+ ExecutionResult ,
8+ } from "constants/AppsmithActionConstants/ActionConstants" ;
69import { ValidationTypes } from "constants/WidgetValidation" ;
710import { DerivedPropertiesMap } from "utils/WidgetFactory" ;
811import { Alignment } from "@blueprintjs/core" ;
@@ -15,6 +18,7 @@ import {
1518 ButtonPlacementTypes ,
1619 ButtonPlacement ,
1720} from "components/constants" ;
21+ import FormWidget from "widgets/FormWidget/widget" ;
1822
1923class ButtonWidget extends BaseWidget < ButtonWidgetProps , ButtonWidgetState > {
2024 onButtonClickBound : ( event : React . MouseEvent < HTMLElement > ) => void ;
@@ -121,6 +125,39 @@ class ButtonWidget extends BaseWidget<ButtonWidgetProps, ButtonWidgetState> {
121125 } ,
122126 ] ,
123127 } ,
128+ // TODO: refactor widgetParentProps implementation when we address #10659
129+ {
130+ sectionName : "Form options" ,
131+ hidden : (
132+ props : ButtonWidgetProps ,
133+ propertyPath : string ,
134+ widgetParentProps ?: WidgetProps ,
135+ ) => widgetParentProps ?. type !== FormWidget . getWidgetType ( ) ,
136+ children : [
137+ {
138+ helpText :
139+ "Disabled if the form is invalid, if this widget exists directly within a Form widget." ,
140+ propertyName : "disabledWhenInvalid" ,
141+ label : "Disabled Invalid Forms" ,
142+ controlType : "SWITCH" ,
143+ isJSConvertible : true ,
144+ isBindProperty : true ,
145+ isTriggerProperty : false ,
146+ validation : { type : ValidationTypes . BOOLEAN } ,
147+ } ,
148+ {
149+ helpText :
150+ "Resets the fields of the form, on click, if this widget exists directly within a Form widget." ,
151+ propertyName : "resetFormOnClick" ,
152+ label : "Reset Form on Success" ,
153+ controlType : "SWITCH" ,
154+ isJSConvertible : true ,
155+ isBindProperty : true ,
156+ isTriggerProperty : false ,
157+ validation : { type : ValidationTypes . BOOLEAN } ,
158+ } ,
159+ ] ,
160+ } ,
124161 {
125162 sectionName : "Events" ,
126163 children : [
@@ -321,6 +358,8 @@ class ButtonWidget extends BaseWidget<ButtonWidgetProps, ButtonWidgetState> {
321358 callback : this . handleActionComplete ,
322359 } ,
323360 } ) ;
361+ } else if ( this . props . resetFormOnClick && this . props . onReset ) {
362+ this . props . onReset ( ) ;
324363 }
325364 }
326365
@@ -341,13 +380,22 @@ class ButtonWidget extends BaseWidget<ButtonWidgetProps, ButtonWidgetState> {
341380 }
342381 } ;
343382
344- handleActionComplete = ( ) => {
383+ handleActionComplete = ( result : ExecutionResult ) => {
345384 this . setState ( {
346385 isLoading : false ,
347386 } ) ;
387+ if ( result . success ) {
388+ if ( this . props . resetFormOnClick && this . props . onReset )
389+ this . props . onReset ( ) ;
390+ }
348391 } ;
349392
350393 getPageView ( ) {
394+ const disabled =
395+ this . props . disabledWhenInvalid &&
396+ "isFormValid" in this . props &&
397+ ! this . props . isFormValid ;
398+ const isDisabled = this . props . isDisabled || disabled ;
351399 return (
352400 < ButtonComponent
353401 borderRadius = { this . props . borderRadius }
@@ -359,10 +407,10 @@ class ButtonWidget extends BaseWidget<ButtonWidgetProps, ButtonWidgetState> {
359407 handleRecaptchaV2Loading = { this . handleRecaptchaV2Loading }
360408 iconAlign = { this . props . iconAlign }
361409 iconName = { this . props . iconName }
362- isDisabled = { this . props . isDisabled }
410+ isDisabled = { isDisabled }
363411 isLoading = { this . props . isLoading || this . state . isLoading }
364412 key = { this . props . widgetId }
365- onClick = { ! this . props . isDisabled ? this . onButtonClickBound : undefined }
413+ onClick = { isDisabled ? undefined : this . onButtonClickBound }
366414 placement = { this . props . placement }
367415 recaptchaType = { this . props . recaptchaType }
368416 text = { this . props . text }
@@ -394,6 +442,8 @@ export interface ButtonWidgetProps extends WidgetProps {
394442 iconName ?: IconName ;
395443 iconAlign ?: Alignment ;
396444 placement ?: ButtonPlacement ;
445+ disabledWhenInvalid ?: boolean ;
446+ resetFormOnClick ?: boolean ;
397447}
398448
399449interface ButtonWidgetState extends WidgetState {
0 commit comments