@esportsplus/ui 0.35.8 → 0.35.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,15 +1,9 @@
|
|
|
1
1
|
import response, { Response } from '@esportsplus/action';
|
|
2
|
-
import { html, Attributes, Element } from '@esportsplus/template';
|
|
2
|
+
import { html, Attributes, Element, Renderable } from '@esportsplus/template';
|
|
3
3
|
import { omit } from '@esportsplus/utilities';
|
|
4
|
-
import template from '~/components/template';
|
|
5
4
|
import input from './input';
|
|
6
5
|
|
|
7
6
|
|
|
8
|
-
type A = {
|
|
9
|
-
action: (data: { input: Record<string, any>, response: typeof response }) => (Promise<Errors> | Errors),
|
|
10
|
-
state?: { processing: boolean }
|
|
11
|
-
} & Attributes;
|
|
12
|
-
|
|
13
7
|
type Errors = { errors: Response<unknown>['errors'] };
|
|
14
8
|
|
|
15
9
|
|
|
@@ -46,59 +40,63 @@ function parse(input: ReturnType<FormData['entries']>) {
|
|
|
46
40
|
};
|
|
47
41
|
|
|
48
42
|
|
|
49
|
-
export default
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
43
|
+
export default <T extends Record<string, any>>(
|
|
44
|
+
attributes: {
|
|
45
|
+
action: (data: { input: T, response: typeof response }) => (Promise<Errors> | Errors),
|
|
46
|
+
state?: { processing: boolean }
|
|
47
|
+
} & Attributes,
|
|
48
|
+
content: Renderable<any>
|
|
49
|
+
) => {
|
|
50
|
+
let { action, state } = attributes;
|
|
51
|
+
|
|
52
|
+
return html`
|
|
53
|
+
<form
|
|
54
|
+
${omit(attributes, OMIT)}
|
|
55
|
+
${{
|
|
56
|
+
onclick: function(event) {
|
|
57
|
+
let trigger = event.target as HTMLButtonElement;
|
|
58
|
+
|
|
59
|
+
if (trigger?.type !== 'submit') {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
66
62
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
);
|
|
70
|
-
},
|
|
71
|
-
onsubmit: async function(event) {
|
|
72
|
-
event.preventDefault();
|
|
63
|
+
// On initial page load both events will be dispatched without preventDefault
|
|
64
|
+
event.preventDefault();
|
|
73
65
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
66
|
+
this.dispatchEvent(
|
|
67
|
+
new SubmitEvent('submit', { cancelable: true, bubbles:true, submitter: trigger })
|
|
68
|
+
);
|
|
69
|
+
},
|
|
70
|
+
onsubmit: async function(event) {
|
|
71
|
+
event.preventDefault();
|
|
77
72
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
});
|
|
73
|
+
if (state) {
|
|
74
|
+
state.processing = true;
|
|
75
|
+
}
|
|
82
76
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
77
|
+
let { errors } = await action({
|
|
78
|
+
input: parse( new FormData( this as any as HTMLFormElement ).entries() ) as T,
|
|
79
|
+
response
|
|
80
|
+
});
|
|
86
81
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
82
|
+
for (let i = 0, n = errors.length; i < n; i++) {
|
|
83
|
+
let { message, path } = errors[i],
|
|
84
|
+
reactive = input.get( (this as any as HTMLFormElement)[path!] as Element | undefined );
|
|
90
85
|
|
|
91
|
-
|
|
86
|
+
if (!reactive) {
|
|
87
|
+
continue;
|
|
92
88
|
}
|
|
93
89
|
|
|
94
|
-
|
|
95
|
-
state.processing = false;
|
|
96
|
-
}
|
|
90
|
+
reactive.error = `${message[0].toUpperCase()}${message.substring(1)}`;
|
|
97
91
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
92
|
+
|
|
93
|
+
if (state) {
|
|
94
|
+
state.processing = false;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}}
|
|
98
|
+
>
|
|
99
|
+
${content}
|
|
100
|
+
</form>
|
|
101
|
+
`;
|
|
102
|
+
};
|