-
Notifications
You must be signed in to change notification settings - Fork 1
/
form.jsx
54 lines (51 loc) · 1.62 KB
/
form.jsx
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
import Form from './form.html';
import React from 'react';
import ReactDOM from 'react-dom';
import { getBodyElement, buildScriptWithFn } from './helpers';
import { connect } from 'react-redux';
import { store, updateTodoForm } from './store';
class FormComponent extends React.Component {
constructor() {
super();
window.store = store;
window.updateTodoForm = updateTodoForm;
}
logResult() {
const name = document.querySelector('#todo-name');
console.log('the name', name.value);
return false;
}
updateInputState() {
const input = document.querySelector('#todo-name');
window.store.dispatch(window.updateTodoForm(input.value));
return false;
}
createMarkup() {
const body = getBodyElement(Form);
this.script = buildScriptWithFn(this.logResult);
this.inputScript = buildScriptWithFn(this.updateInputState);
document.body.appendChild(this.script);
document.body.appendChild(this.inputScript);
const form = body.querySelector('#todo-form');
form.setAttribute('onsubmit', 'return logResult()');
const input = body.querySelector('#todo-name');
input.setAttribute('oninput', 'return updateInputState()');
const htmlString = body.innerHTML
return { __html: htmlString };
}
render() {
return (
<div dangerouslySetInnerHTML={this.createMarkup()} />
);
}
componentWillUnmount() {
delete window.store;
delete window.updateTodoForm;
this.script.remove();
this.inputScript.remove();
}
}
const FormComponentWithRedux = connect(state => ({
todoValue: state.todoFormValue
}))(FormComponent);
export default FormComponentWithRedux;