-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.html
87 lines (81 loc) · 3.2 KB
/
example.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<html lang="en">
<head>
<title>Mithril utilites testing</title>
</head>
<body>
<main></main>
<script type="importmap">
{
"imports": {
"mithril": "https://cdn.skypack.dev/mithril?min",
"mithril/stream": "https://cdn.skypack.dev/mithril/stream?min",
"./dist/Component": "./dist/Component.js",
"./dist/Form": "./dist/Form.js",
"./dist/Request": "./dist/Request.js",
"./dist/helpers": "./dist/helpers.js",
"classnames": "https://cdn.skypack.dev/classnames?min",
"collect.js": "https://cdn.skypack.dev/collect.js?min",
"typescript-cookie": "./node_modules/typescript-cookie/dist/typescript-cookie.mjs"
}
}
</script>
<!--suppress NpmUsedModulesInstalled -->
<script type="module">
import m from 'mithril';
import Stream from 'mithril/stream';
import Component from "./dist/Component";
import Form from "./dist/Form";
import Request from "./dist/Request";
// Component is a wrapper for mithril components
class Page extends Component {
/** @type import('dist/Form').FormAttributes['state'] */
formstate = {
name: Stream(''),
email: Stream(''),
terms: Stream(false),
};
view(vnode) {
return m('div', [
m('h1', 'Mithril utilities testing'),
m('p', 'This is a test page for mithril-utilities.',
' It is not intended to be used in production.'),
m('h2', 'Form example'),
m(Form, {
state: this.formstate,
/**
* @typedef @import('dist/Form').FormSubmitEvent FormSubmitEvent
* @param {FormSubmitEvent} event
*/
onsubmit: (event) => {
console.log('Form submitted');
console.log('Data: ', Object.fromEntries([...event.data.entries()]));
console.log('State: ', Object.fromEntries(Object.entries(this.formstate).map(([key, value]) => [key, value()])));
alert('Check the console');
}
}, [
m('input', {type: 'text', name: 'name', placeholder: 'Enter your name'}),
m('br'),
m('input', {type: 'text', name: 'email', placeholder: 'Enter your email'}),
m('br'),
m('label', undefined, [
m('input', {type: 'checkbox', name: 'terms', 'preferred-value-prop': 'checked'}),
m('span', 'I agree to the terms and conditions')
]),
m('br'),
m('button', {type: 'submit'}, 'Submit')
]),
m('h2', 'Request example'),
m('button', {onclick: () => this.request()}, 'Request'),
]);
}
request() {
Request.get('https://jsonplaceholder.typicode.com/todos/1').then((data) => {
console.log('Request data: ', data);
alert('Check the console');
});
}
}
m.mount(document.body.querySelector('main'), Page);
</script>
</body>
</html>