Skip to content

Commit 6d44f7e

Browse files
committed
feat(form): submit-on-enter attribute
1 parent 0bc8799 commit 6d44f7e

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

src/form/ux-form.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ import { processDesignAttributes } from '../designs/design-attributes';
1111

1212
export class UxForm implements Themable {
1313
@bindable public theme = null;
14-
@bindable public submit: any;
14+
@bindable public submitOnEnter: any;
1515

1616
public view: View;
17+
private bindSubmitToEnter: boolean = false;
1718

1819
constructor(private element: Element, public resources: ViewResources, private styleEngine: StyleEngine) { }
1920

@@ -25,17 +26,39 @@ export class UxForm implements Themable {
2526
if (this.theme) {
2627
this.styleEngine.applyTheme(this, this.theme);
2728
}
29+
30+
if (this.submitOnEnter !== undefined) {
31+
this.bindSubmitToEnter = true;
32+
}
33+
}
34+
35+
public attached() {
36+
if (this.bindSubmitToEnter) {
37+
this.element.addEventListener('keyup', (e: KeyboardEvent) => {
38+
if (e.keyCode === 13) {
39+
this.submitForm();
40+
}
41+
});
42+
}
43+
}
44+
45+
public detached() {
46+
if (this.bindSubmitToEnter) {
47+
this.element.removeEventListener('keyup', (e: KeyboardEvent) => {
48+
if (e.keyCode === 13) {
49+
this.submitForm();
50+
}
51+
});
52+
}
2853
}
2954

3055
public themeChanged(newValue: any) {
3156
this.styleEngine.applyTheme(this, newValue);
3257
}
3358

3459
public submitForm() {
35-
if (this.submit) {
36-
const submitEvent = DOM.createCustomEvent('submit', { bubbles: true, target: this.element });
60+
const submitEvent = DOM.createCustomEvent('submit', { bubbles: true, target: this.element });
3761

38-
this.element.dispatchEvent(submitEvent);
39-
}
62+
this.element.dispatchEvent(submitEvent);
4063
}
4164
}

0 commit comments

Comments
 (0)