Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .changeset/input-submit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
"@patternfly/elements": patch
---
`<pf-text-input>`: pressing `Enter` will request to submit the form
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">
15 changes: 13 additions & 2 deletions elements/pf-text-input/pf-text-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ export class PfTextInput extends LitElement {
/** Trim text on left */
@property({ type: Boolean, reflect: true, attribute: 'left-truncated' }) leftTruncated = false;

/** Value to indicate if the input is modified to show that validation state.
/**
* Value to indicate if the input is modified to show that validation state.
* If set to success, input will be modified to indicate valid state.
* If set to warning, input will be modified to indicate warning state.
* Invalid inputs will display an error state
Expand Down Expand Up @@ -233,8 +234,9 @@ 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}"
?disabled="${this.matches(':disabled') || this.disabled}"
?readonly="${this.readonly}"
Expand All @@ -260,6 +262,15 @@ export class PfTextInput extends LitElement {
this.#touched = true;
}

#onKeydown(event: Event) {
switch ((event as KeyboardEvent).key) {
case 'Enter':
if (this.reportValidity()) {
this.#internals.form?.requestSubmit(null);
}
}
}

#onBlur() {
if (this.validateOn === 'blur') {
this.checkValidity();
Expand Down