Skip to content

Commit

Permalink
fix(text-input): form validation on submit
Browse files Browse the repository at this point in the history
  • Loading branch information
bennypowers committed Nov 14, 2023
1 parent 29076f6 commit 3ec82a6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
24 changes: 24 additions & 0 deletions elements/pf-text-input/demo/form-submission.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<form id="pf-form">
<label for="input">Input</label>
<pf-text-input id="input" required></pf-text-input>
<p>Form status: <output name="status">unsubmitted</output></p>
</form>

<style>
label {
display: block;
}
</style>

<script type="module">
import '@patternfly/elements/pf-text-input/pf-text-input.js';

document
.querySelector('#pf-form')
.addEventListener('submit', function(event) {
event.preventDefault();
this.elements.status.textContent = 'Submitted';
});
</script>

<link rel="stylesheet" href="demo.css">
6 changes: 4 additions & 2 deletions elements/pf-text-input/pf-text-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ export class PfTextInput extends LitElement {
<input id="input"
.placeholder="${this.placeholder ?? ''}"
.value="${this.value}"
.pattern="${this.pattern as string}"
pattern="${ifDefined(this.pattern)}"
@input="${this.#onInput}"
@keydown="${this.#onKeydown}"
@blur="${this.#onBlur}"
Expand Down Expand Up @@ -265,7 +265,9 @@ export class PfTextInput extends LitElement {
#onKeydown(event: Event) {
switch ((event as KeyboardEvent).key) {
case 'Enter':
this.#internals.form?.requestSubmit(null);
if (this.reportValidity()) {
this.#internals.form?.requestSubmit(null);
}
}
}

Expand Down

0 comments on commit 3ec82a6

Please sign in to comment.