Skip to content

Commit 3d7ce5a

Browse files
authored
feat(text-input): form submission (#2647)
1 parent dc7b07e commit 3d7ce5a

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

.changeset/input-submit.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
"@patternfly/elements": patch
3+
---
4+
`<pf-text-input>`: pressing `Enter` will request to submit the form
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<form id="pf-form">
2+
<label for="input">Input</label>
3+
<pf-text-input id="input" required></pf-text-input>
4+
<p>Form status: <output name="status">unsubmitted</output></p>
5+
</form>
6+
7+
<style>
8+
label {
9+
display: block;
10+
}
11+
</style>
12+
13+
<script type="module">
14+
import '@patternfly/elements/pf-text-input/pf-text-input.js';
15+
16+
document
17+
.querySelector('#pf-form')
18+
.addEventListener('submit', function(event) {
19+
event.preventDefault();
20+
this.elements.status.textContent = 'Submitted';
21+
});
22+
</script>
23+
24+
<link rel="stylesheet" href="demo.css">

elements/pf-text-input/pf-text-input.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ export class PfTextInput extends LitElement {
156156
/** Trim text on left */
157157
@property({ type: Boolean, reflect: true, attribute: 'left-truncated' }) leftTruncated = false;
158158

159-
/** Value to indicate if the input is modified to show that validation state.
159+
/**
160+
* Value to indicate if the input is modified to show that validation state.
160161
* If set to success, input will be modified to indicate valid state.
161162
* If set to warning, input will be modified to indicate warning state.
162163
* Invalid inputs will display an error state
@@ -233,8 +234,9 @@ export class PfTextInput extends LitElement {
233234
<input id="input"
234235
.placeholder="${this.placeholder ?? ''}"
235236
.value="${this.value}"
236-
.pattern="${this.pattern as string}"
237+
pattern="${ifDefined(this.pattern)}"
237238
@input="${this.#onInput}"
239+
@keydown="${this.#onKeydown}"
238240
@blur="${this.#onBlur}"
239241
?disabled="${this.matches(':disabled') || this.disabled}"
240242
?readonly="${this.readonly}"
@@ -260,6 +262,15 @@ export class PfTextInput extends LitElement {
260262
this.#touched = true;
261263
}
262264

265+
#onKeydown(event: Event) {
266+
switch ((event as KeyboardEvent).key) {
267+
case 'Enter':
268+
if (this.reportValidity()) {
269+
this.#internals.form?.requestSubmit(null);
270+
}
271+
}
272+
}
273+
263274
#onBlur() {
264275
if (this.validateOn === 'blur') {
265276
this.checkValidity();

0 commit comments

Comments
 (0)