Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0bc8799

Browse files
committed
feat(form): submit
add submit function on the form, add ux-submit directive to allow elements to trigger a forms submit.
1 parent 7aaae37 commit 0bc8799

File tree

5 files changed

+54
-13
lines changed

5 files changed

+54
-13
lines changed

src/form/ux-field.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ export class UxField implements Themable {
3333
}
3434
}
3535

36-
// public attached() { }
37-
38-
// public detached() { }
39-
4036
public themeChanged(newValue: any) {
4137
this.styleEngine.applyTheme(this, newValue);
4238
}

src/form/ux-form-theme.css

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ styles.form .form-row {
1010
}
1111

1212
styles.form .form-row>* {
13-
margin: 0 8px;
13+
margin-left: 8px;
14+
margin-right: 8px;
1415
}
1516

1617
styles.form .form-row>*:last-child {

src/form/ux-form.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
import { customElement, bindable, ViewResources, View, processAttributes } from 'aurelia-templating';
2+
import { DOM } from 'aurelia-pal';
23
import { inject } from 'aurelia-dependency-injection';
34
import { StyleEngine } from '../styles/style-engine';
45
import { Themable } from '../styles/themable';
56
import { processDesignAttributes } from '../designs/design-attributes';
67

7-
@inject(ViewResources, StyleEngine)
8+
@inject(Element, ViewResources, StyleEngine)
89
@customElement('ux-form')
910
@processAttributes(processDesignAttributes)
1011

1112
export class UxForm implements Themable {
1213
@bindable public theme = null;
14+
@bindable public submit: any;
1315

1416
public view: View;
1517

16-
constructor(public resources: ViewResources, private styleEngine: StyleEngine) { }
18+
constructor(private element: Element, public resources: ViewResources, private styleEngine: StyleEngine) { }
1719

1820
public created(_: any, myView: View) {
1921
this.view = myView;
@@ -25,12 +27,15 @@ export class UxForm implements Themable {
2527
}
2628
}
2729

28-
// public attached() { }
29-
30-
// public detached() { }
31-
32-
3330
public themeChanged(newValue: any) {
3431
this.styleEngine.applyTheme(this, newValue);
3532
}
33+
34+
public submitForm() {
35+
if (this.submit) {
36+
const submitEvent = DOM.createCustomEvent('submit', { bubbles: true, target: this.element });
37+
38+
this.element.dispatchEvent(submitEvent);
39+
}
40+
}
3641
}

src/form/ux-submit-attribute.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { inject } from 'aurelia-dependency-injection';
2+
import { DOM } from 'aurelia-pal';
3+
4+
@inject(Element)
5+
export class UxSubmitCustomAttribute {
6+
public submitEvent: CustomEvent;
7+
private canSubmit: boolean = false;
8+
9+
constructor(public element: Element) { }
10+
11+
public attached() {
12+
let currentParent = this.element.parentElement;
13+
14+
while (currentParent != null) {
15+
if (currentParent.tagName === 'UX-FORM') {
16+
17+
this.canSubmit = true;
18+
this.submitEvent = DOM.createCustomEvent('submit', { bubbles: true });
19+
20+
this.element.addEventListener('mouseup', () => {
21+
this.element.dispatchEvent(this.submitEvent);
22+
});
23+
24+
break;
25+
}
26+
27+
currentParent = currentParent.parentElement;
28+
}
29+
}
30+
31+
public detached() {
32+
if (this.canSubmit) {
33+
this.element.removeEventListener('mouseup', () => {
34+
this.element.dispatchEvent(this.submitEvent);
35+
});
36+
}
37+
}
38+
}

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ export function configure(config: FrameworkConfiguration, callback?: (config: Au
2323
'./input-info/ux-input-info',
2424
'./textarea/ux-textarea',
2525
'./form/ux-form',
26-
'./form/ux-field'
26+
'./form/ux-field',
27+
'./form/ux-submit-attribute'
2728
]);
2829

2930
const ux = config.container.get(AureliaUX) as AureliaUX;

0 commit comments

Comments
 (0)