Skip to content

Commit

Permalink
fix: Update ButtonGroupWidget to correctly handle button disabling ba…
Browse files Browse the repository at this point in the history
…sed on form validity

- Adjusted logic in ButtonGroupComponent to disable buttons when `disabledWhenInvalid` is true and the form is invalid.
- Passed `isFormValid` prop from ButtonGroupWidget to ButtonGroupComponent.
- Updated tests to ensure buttons are enabled/disabled correctly based on the `isFormValid` state.

Co-Authored-By: rahul.barwal@appsmith.com
  • Loading branch information
rahulbarwal committed Jan 15, 2025
1 parent e2b0485 commit e71e6e0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ class ButtonGroupComponent extends React.Component<
isDisabled ||
!!loadedBtnId ||
isLoading ||
(button.disabledWhenInvalid && !isFormValid);
(button.disabledWhenInvalid && isFormValid === false);

if (button.buttonType === "MENU" && !isButtonDisabled) {
const { menuItems } = button;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React from "react";
import ButtonGroupWidget from "../index";
import { RenderModes } from "constants/WidgetConstants";
import type { ButtonGroupWidgetProps } from "../index";
import { klona } from "klona";

describe("ButtonGroupWidget disabledWhenInvalid", () => {
const defaultProps: ButtonGroupWidgetProps = {
Expand Down Expand Up @@ -50,55 +51,51 @@ describe("ButtonGroupWidget disabledWhenInvalid", () => {
};

it("disables buttons when disabledWhenInvalid is true and form is invalid", () => {
const props = {
...defaultProps,
isFormValid: false,
};
const props = klona(defaultProps);

props.isFormValid = false;

const { container } = render(<ButtonGroupWidget {...props} />);
const buttons = container.querySelectorAll("button");

buttons.forEach((button) => {
expect(button).toHaveAttribute("disabled");
expect(button.hasAttribute("disabled")).toBe(true);
});
});

it("enables buttons when disabledWhenInvalid is true but form is valid", () => {
const props = {
...defaultProps,
isFormValid: true,
};
const props = klona(defaultProps);

props.isFormValid = true;

const { container } = render(<ButtonGroupWidget {...props} />);
const buttons = container.querySelectorAll("button");

buttons.forEach((button) => {
expect(button).not.toHaveAttribute("disabled");
expect(button.hasAttribute("disabled")).toBe(false);
});
});

it("enables buttons when disabledWhenInvalid is false regardless of form validity", () => {
const props = {
...defaultProps,
isFormValid: false,
groupButtons: {
...defaultProps.groupButtons,
groupButton1: {
...defaultProps.groupButtons.groupButton1,
disabledWhenInvalid: false,
},
groupButton2: {
...defaultProps.groupButtons.groupButton2,
disabledWhenInvalid: false,
},
const props = klona(defaultProps);

props.groupButtons = {
...defaultProps.groupButtons,
groupButton1: {
...defaultProps.groupButtons.groupButton1,
disabledWhenInvalid: false,
},
groupButton2: {
...defaultProps.groupButtons.groupButton2,
disabledWhenInvalid: false,
},
};

const { container } = render(<ButtonGroupWidget {...props} />);
const buttons = container.querySelectorAll("button");

buttons.forEach((button) => {
expect(button).not.toHaveAttribute("disabled");
expect(button.hasAttribute("disabled")).toBe(false);
});
});
});
1 change: 1 addition & 0 deletions app/client/src/widgets/ButtonGroupWidget/widget/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,7 @@ class ButtonGroupWidget extends BaseWidget<
buttonVariant={this.props.buttonVariant}
groupButtons={this.props.groupButtons}
isDisabled={this.props.isDisabled}
isFormValid={this.props.isFormValid}
minHeight={this.isAutoLayoutMode ? this.props.minHeight : undefined}
minPopoverWidth={minPopoverWidth}
orientation={this.props.orientation}
Expand Down

0 comments on commit e71e6e0

Please sign in to comment.