|
@@ -0,0 +1,160 @@
|
|
|
+import React, { Component } from 'react';
|
|
|
+import logo from './logo.svg';
|
|
|
+import './App.css';
|
|
|
+
|
|
|
+import { connect, Provider} from 'react-redux';
|
|
|
+import { createStore, combineReducers } from 'redux';
|
|
|
+import { stat } from 'fs';
|
|
|
+
|
|
|
+
|
|
|
+var dataGrid = []
|
|
|
+function data () {
|
|
|
+let dataGrid = [[{value: 5, formula: 5}, {value: 2, formula: 2}, {value: 3, formula: 3}],
|
|
|
+ [{value: 4, formula: 4}, {value: 6, formula: 6}, {value: 7, formula: 7}],
|
|
|
+ [{value: 8, formula: 8}, {value: 9, formula: 9}, {value: 1, formula: 1}], ]
|
|
|
+ return dataGrid
|
|
|
+}
|
|
|
+
|
|
|
+dataGrid = data ()
|
|
|
+
|
|
|
+class CellComponent extends Component {
|
|
|
+ onClick = () => {
|
|
|
+ this.props.onClick(this.props.rows, this.props.columns);
|
|
|
+ console.log (this.props)
|
|
|
+ };
|
|
|
+ render() {
|
|
|
+ return(
|
|
|
+ <td onClick = {this.onClick}> {this.props.cellData.value} </td>
|
|
|
+ )
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+class TestFromInput extends Component {
|
|
|
+ constructor(props) {
|
|
|
+ super(props);
|
|
|
+ this.state = {
|
|
|
+ cell_input: true,
|
|
|
+ row: 0,
|
|
|
+ column: 0
|
|
|
+ };
|
|
|
+ this.inputCell = this.inputCell.bind(this.value);
|
|
|
+ this.onChange = this.onChange.bind(this);
|
|
|
+ }
|
|
|
+
|
|
|
+ cellClick = (i,j) => {
|
|
|
+ console.log (j,i)
|
|
|
+ this.input.value = this.props.dataGrid [i][j].formula
|
|
|
+ this.setState(() => ({
|
|
|
+ row: i,
|
|
|
+ column: j
|
|
|
+ }))
|
|
|
+ store.dispatch ({type: 'CHANGE_INPUT', dataFormula: this.props.dataGrid [i][j].formula})
|
|
|
+ };
|
|
|
+
|
|
|
+ inputCell() {
|
|
|
+ this.setState(
|
|
|
+ (prevState, props) => ({
|
|
|
+ cell_input: this.input.value
|
|
|
+ })
|
|
|
+ );
|
|
|
+ }
|
|
|
+ onChange(i, j){
|
|
|
+ console.log ('inputCell', this.input.value)
|
|
|
+ this.props.dataGrid [this.state.row][this.state.column].formula = this.input.value
|
|
|
+
|
|
|
+ store.dispatch ({type: 'CHANGE_CELL', dataInput: this.input.value})
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ render (){
|
|
|
+ console.log ('render', this.props)
|
|
|
+ var trs= new Array();
|
|
|
+ for (var i = 0; i < this.props.rows; i++) {
|
|
|
+ var tds = new Array();
|
|
|
+ for (var j = 0; j < this.props.columns; j++) {
|
|
|
+ var oneCell = {}
|
|
|
+ oneCell['adress']= String.fromCharCode(65+i) + (+(+ j))
|
|
|
+ oneCell['formula'] = " "
|
|
|
+ oneCell[String.fromCharCode(65+i) + (+(+ j))] = {i} + {j}
|
|
|
+ tds.push (<CellComponent
|
|
|
+ adress = {oneCell.adress}
|
|
|
+ columns = {j} rows = {i}
|
|
|
+ onClick = {this.cellClick.bind(this, j, i)}
|
|
|
+ onChange = {this.inputCell}
|
|
|
+ ref={ c => this.td = c}
|
|
|
+ cellData = {this.props.data[j][i]}
|
|
|
+ > </CellComponent>)
|
|
|
+ }
|
|
|
+ trs.push(<tr>{tds}</tr>)
|
|
|
+ }
|
|
|
+
|
|
|
+ return (
|
|
|
+ <div>
|
|
|
+ <input
|
|
|
+ className = "dataInput"
|
|
|
+ onChange={this.onChange}
|
|
|
+ ref={ c => this.input = c}/>
|
|
|
+ <table>
|
|
|
+ <tbody>
|
|
|
+ {trs}
|
|
|
+ </tbody>
|
|
|
+ </table>
|
|
|
+ </div>
|
|
|
+ )
|
|
|
+ };
|
|
|
+};
|
|
|
+
|
|
|
+function tableReducer(state =[], action){
|
|
|
+ if (action.type === 'CHANGE_INPUT'){
|
|
|
+ return [...state, action.dataFormula]
|
|
|
+ }
|
|
|
+ if (action.type === 'CHANGE_CELL'){
|
|
|
+ return [...state, action.dataInput]
|
|
|
+ }
|
|
|
+ return state;
|
|
|
+}
|
|
|
+
|
|
|
+const store = createStore (tableReducer)
|
|
|
+store.subscribe(() => {
|
|
|
+ console.log('subcribe', store.getState())
|
|
|
+})
|
|
|
+
|
|
|
+const mapStateToProps = function (store) {
|
|
|
+ return {
|
|
|
+ tableReducer: store,
|
|
|
+
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+data = connect(mapStateToProps)(data)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+class AppInput extends Component {
|
|
|
+ render() {
|
|
|
+
|
|
|
+ return (
|
|
|
+ <Provider store={store}>
|
|
|
+
|
|
|
+ <div className="App">
|
|
|
+
|
|
|
+ <TestFromInput columns = {3} rows = {3} data = {dataGrid}/>
|
|
|
+ </div>
|
|
|
+ </Provider>
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ export default AppInput;
|
|
|
+
|