Skip to content

Commit 287ecab

Browse files
authored
Merge pull request #27 from Travelopia/feature/fix-form-fields
Fix Form Events
2 parents 1da5667 + 4e6d5c8 commit 287ecab

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@travelopia/web-components",
3-
"version": "0.5.10",
3+
"version": "0.5.11",
44
"description": "Accessible web components for the modern web",
55
"files": [
66
"dist"

src/form/tp-form-field.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ export class TPFormFieldElement extends HTMLElement {
8686
return true;
8787
}
8888

89+
// Check if the field is not visible.
90+
if ( this.offsetWidth <= 0 || this.offsetHeight <= 0 ) {
91+
return true;
92+
}
93+
8994
// Prepare error and valid status.
9095
let valid: boolean = true;
9196
let error: string = '';
@@ -137,14 +142,14 @@ export class TPFormFieldElement extends HTMLElement {
137142
this.appendChild( errorElement );
138143
}
139144

140-
this.dispatchEvent( new CustomEvent( 'validation-error', { bubbles: true } ) );
145+
this.dispatchEvent( new CustomEvent( 'validation-error' ) );
141146
}
142147

143148
/**
144149
* Remove the error message.
145150
*/
146151
removeErrorMessage(): void {
147152
this.querySelector( 'tp-form-error' )?.remove();
148-
this.dispatchEvent( new CustomEvent( 'validation-success', { bubbles: true } ) );
153+
this.dispatchEvent( new CustomEvent( 'validation-success' ) );
149154
}
150155
}

src/form/tp-form.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export class TPFormElement extends HTMLElement {
3737
const formValid: boolean = this.validate();
3838
if ( ! formValid || 'yes' === this.getAttribute( 'prevent-submit' ) ) {
3939
e.preventDefault();
40+
e.stopImmediatePropagation();
4041
}
4142

4243
const submit: TPFormSubmitElement | null = this.querySelector( 'tp-form-submit' );
@@ -47,6 +48,10 @@ export class TPFormElement extends HTMLElement {
4748
submit.removeAttribute( 'submitting' );
4849
}
4950
}
51+
52+
if ( formValid ) {
53+
this.dispatchEvent( new CustomEvent( 'submit-validation-success', { bubbles: true } ) );
54+
}
5055
}
5156

5257
/**

0 commit comments

Comments
 (0)