Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear field error state if the field is cleared #2424

Merged
merged 12 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/fast-readers-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@khanacademy/wonder-blocks-search-field": patch
"@khanacademy/wonder-blocks-form": patch
---

TextField, TextArea, SearchField: Clear error state when field is cleared and it is not required
5 changes: 5 additions & 0 deletions .changeset/twenty-ducks-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@khanacademy/wonder-blocks-dropdown": patch
---

MultiSelect: Clear error state when "Select none" or "Select all" shortcuts are used
3 changes: 3 additions & 0 deletions __docs__/wonder-blocks-dropdown/multi-select.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,9 @@ export const ErrorFromValidation: StoryComponentType = {
</View>
);
},
args: {
shortcuts: true,
},
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2583,6 +2583,106 @@ describe("MultiSelect", () => {
"true",
);
});

it("should call onValidate with null if values are cleared using the 'select none' shortcut", async () => {
// Arrange
const errorMessage = "Error message";
const onValidate = jest.fn();
const {userEvent} = doRender(
<ControlledMultiSelect
validate={(values) =>
values.includes("1") ? errorMessage : undefined
}
selectedValues={["1"]}
onValidate={onValidate}
shortcuts={true}
/>,
);
await userEvent.click(await screen.findByRole("button")); // Open the dropdown
onValidate.mockClear(); // Clear any calls

// Act
await userEvent.click(await screen.findByText("Select none")); // Select none

// Assert
expect(onValidate).toHaveBeenCalledExactlyOnceWith(null);
});

it("should not be in an error state if values are cleared using the 'select none' shortcut", async () => {
// Arrange
const errorMessage = "Error message";
const {userEvent} = doRender(
<ControlledMultiSelect
validate={(values) =>
values.includes("1") ? errorMessage : undefined
}
selectedValues={["1"]}
shortcuts={true}
/>,
);
await userEvent.click(await screen.findByRole("button")); // Open the dropdown

// Act
await userEvent.click(await screen.findByText("Select none")); // Select none

// Assert
expect(await screen.findByRole("button")).toHaveAttribute(
"aria-invalid",
"false",
);
});

it("should call onValidate with null if values are changed using the 'select all' shortcut", async () => {
// Arrange
const errorMessage = "Error message";
const onValidate = jest.fn();
const {userEvent} = doRender(
<ControlledMultiSelect
validate={(values) =>
values.includes("1") ? errorMessage : undefined
}
selectedValues={["1"]}
onValidate={onValidate}
shortcuts={true}
/>,
);
await userEvent.click(await screen.findByRole("button")); // Open the dropdown
onValidate.mockClear(); // Clear any calls

// Act
await userEvent.click(
await screen.findByText("Select all (2)"),
); // Select all

// Assert
expect(onValidate).toHaveBeenCalledExactlyOnceWith(null);
});

it("should not be in an error state if values are changed using the 'select all' shortcut", async () => {
// Arrange
const errorMessage = "Error message";
const {userEvent} = doRender(
<ControlledMultiSelect
validate={(values) =>
values.includes("1") ? errorMessage : undefined
}
selectedValues={["1"]}
shortcuts={true}
/>,
);
await userEvent.click(await screen.findByRole("button")); // Open the dropdown

// Act
await userEvent.click(
await screen.findByText("Select all (2)"),
); // Select all

// Assert
expect(await screen.findByRole("button")).toHaveAttribute(
"aria-invalid",
"false",
);
});
});

describe("required", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,10 +357,12 @@ const MultiSelect = (props: Props) => {
.filter((option) => !!option && !option.props.disabled)
.map((option) => option.props.value);
onChange(selected);
onSelectedValuesChangeValidation();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the values change when the Select none / Select all options are picked, we call the onSelectedValuesChangeValidation handler

};

const handleSelectNone = () => {
onChange([]);
onSelectedValuesChangeValidation();
};

const getMenuText = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ describe("Required LabeledTextField", () => {
const errorMessage = "Empty string!";

const validate = (value: string): string | null | undefined => {
if (value === "") {
if (value === "initials") {
return errorMessage;
}
};
Expand All @@ -729,10 +729,10 @@ describe("Required LabeledTextField", () => {
"test-labeled-text-field-field",
);
textField.focus();
await userEvent.clear(textField);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we now clear the error state when the field is cleared, this test case can be updated with an example where we update the input value instead of clear it

await userEvent.type(textField, "s");

// Act
textField.blur();
await textField.blur();

// Assert
expect(await screen.findByRole("alert")).toHaveTextContent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1806,5 +1806,82 @@ describe("TextArea", () => {
});
});
});

describe("Clearing the field value", () => {
describe.each([true, false])(
"when instantValidation is %s",
(instantValidation) => {
it("should clear the error state", async () => {
// Arrange
render(
<ControlledTextArea
value="test"
validate={() => "error message"}
instantValidation={instantValidation}
/>,
);
const field = screen.getByRole("textbox");

// Act
await userEvent.clear(field);

// Assert
expect(field).toHaveAttribute("aria-invalid", "false");
});

it("should call onValidate with null", async () => {
// Arrange
const onValidate = jest.fn();
render(
<ControlledTextArea
value="test"
validate={() => "error"}
onValidate={onValidate}
instantValidation={instantValidation}
/>,
);
const field = screen.getByRole("textbox");
onValidate.mockReset(); // Reset mock before clearing the field

// Act
await userEvent.clear(field);

// Assert
expect(onValidate).toHaveBeenLastCalledWith(null);
});
},
);

it("should not clear the error state if the field is required", async () => {
// Arrange
render(<ControlledTextArea value="test" required="Required" />);
const field = screen.getByRole("textbox");

// Act
await userEvent.clear(field);

// Assert
expect(field).toHaveAttribute("aria-invalid", "true");
});

it("should not clear the error state if the field is required and instantValidation=false after the field is blurred", async () => {
// Arrange
render(
<ControlledTextArea
instantValidation={false}
value="test"
required="Required"
/>,
);
const field = screen.getByRole("textbox");
await userEvent.clear(field);

// Act
await userEvent.tab();

// Assert
expect(field).toHaveAttribute("aria-invalid", "true");
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -1647,5 +1647,84 @@ describe("TextField", () => {
});
});
});

describe("Clearing the field value", () => {
describe.each([true, false])(
"when instantValidation is %s",
(instantValidation) => {
it("should clear the error state", async () => {
// Arrange
render(
<ControlledTextField
value="test"
validate={() => "error message"}
instantValidation={instantValidation}
/>,
);
const field = screen.getByRole("textbox");

// Act
await userEvent.clear(field);

// Assert
expect(field).toHaveAttribute("aria-invalid", "false");
});

it("should call onValidate with null", async () => {
// Arrange
const onValidate = jest.fn();
render(
<ControlledTextField
value="test"
validate={() => "error message"}
onValidate={onValidate}
instantValidation={instantValidation}
/>,
);
const field = screen.getByRole("textbox");
onValidate.mockReset(); // Reset mock before clearing the field

// Act
await userEvent.clear(field);

// Assert
expect(onValidate).toHaveBeenLastCalledWith(null);
});
},
);

it("should not clear the error state if the field is required", async () => {
// Arrange
render(
<ControlledTextField value="test" required="Required" />,
);
const field = screen.getByRole("textbox");

// Act
await userEvent.clear(field);

// Assert
expect(field).toHaveAttribute("aria-invalid", "true");
});

it("should not clear the error state if the field is required and instantValidation=false after the field is blurred", async () => {
// Arrange
render(
<ControlledTextField
instantValidation={false}
value="test"
required="Required"
/>,
);
const field = screen.getByRole("textbox");
await userEvent.clear(field);

// Act
await userEvent.tab();

// Assert
expect(field).toHaveAttribute("aria-invalid", "true");
});
});
});
});
Loading
Loading