-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (47 loc) · 1.07 KB
/
index.js
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
const input = (attributes, events) => {
let element = document.createElement('input')
for (let prop in attributes) {
element.setAttribute(prop, attributes[prop])
}
if (attributes.type === 'input') {
element.addEventListener('input', (e) => {
events.onChange(element.value)
})
}
if (attributes.type === 'checkbox') {
element.addEventListener('change', () => {
events.onChange(element.checked)
})
}
document.body.appendChild(element)
return element
}
let state = {
input: ''
}
const setInput = (payload) => {
return {
type: 'SET_INPUT',
payload
}
}
const toggleCheckbox = (isChecked) => {
return {
type: 'TOGGLE_CHECKBOX',
isChecked
}
}
const dispatch = (action) => {
state = update(state, action)
console.log(action)
console.log(state)
}
const update = (state, action) => {
switch (action.type) {
case 'SET_INPUT':
return { ...state, input: action.payload }
default:
return state
}
}
input({ type: 'input', value: state.input }, { onChange: (val) => dispatch(setInput(val)) })