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

feat: add calciteInvalid event #9211

Merged
merged 5 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,19 @@ export function formAssociated(

const submitButton = await page.find("#submitButton");
const spyEvent = await page.spyOnEvent(getClearValidationEventName(tag));
const spyInvalidEvent = await page.spyOnEvent("calciteInvalid");

const requiredValidationMessage =
options?.inputType === "radio" ? "Please select one of these options." : "Please fill out this field.";

await assertPreventsFormSubmission(page, component, submitButton, requiredValidationMessage);
expect(spyInvalidEvent).toHaveReceivedEventTimes(1);

await assertClearsValidationOnValueChange(page, component, options, spyEvent, tag);
expect(spyInvalidEvent).toHaveReceivedEventTimes(1);

await assertUserMessageNotOverridden(page, component, submitButton);
expect(spyInvalidEvent).toHaveReceivedEventTimes(2);
}

function ensureName(html: string, componentTag: string): string {
Expand Down
3 changes: 3 additions & 0 deletions packages/calcite-components/src/utils/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,9 @@ function invalidHandler(event: Event) {
// prevent the browser from showing the native validation popover
event?.preventDefault();

// dispatch a "calciteInvalid" so users can set custom validation messages
formComponent.dispatchEvent(new CustomEvent("calciteInvalid", { bubbles: true, composed: true }));

displayValidationMessage(formComponent, {
message: hiddenInput?.validationMessage,
icon: true,
Expand Down