-
Notifications
You must be signed in to change notification settings - Fork 10
/
datagrid.component.jsx
209 lines (194 loc) · 5.85 KB
/
datagrid.component.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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { List, Map } from 'immutable';
import ImmutablePropTypes from 'react-immutable-proptypes';
import { Form, FormGroup, Button, Radio } from 'react-bootstrap';
import Perf from 'react-addons-perf';
import { Datagrid, DatagridActions } from '../../../src/index';
import { GRID, columns, data } from './datagrid.constants';
import './datagrid.component.scss';
// Needed grid actions are mapped here
// There's more in /src/datagrid/datagrid.actions.js
const mapDispatchToProps = {
cellShowMessage: DatagridActions.cellShowMessage,
cellShowMessages: DatagridActions.cellShowMessages,
setData: DatagridActions.setData,
saveSuccess: DatagridActions.saveSuccess,
removeSuccess: DatagridActions.removeSuccess,
saveFail: DatagridActions.saveFail,
removeFail: DatagridActions.removeFail,
};
// Grid state data can be mapped from redux store here
const mapStateToProps = state => ({
allData: state.datagrid.getIn([GRID.id, 'allData'], List()),
createData: state.datagrid.getIn([GRID.id, 'createData'], List()),
editData: state.datagrid.getIn([GRID.id, 'editData'], Map()),
selectedItems: state.datagrid.getIn([GRID.id, 'selectedItems'], List()),
isEditing: state.datagrid.getIn([GRID.id, 'session', 'isEditing'], false),
});
@connect(mapStateToProps, mapDispatchToProps)
export default class DatagridView extends React.Component {
static propTypes = {
// State props
allData: ImmutablePropTypes.list.isRequired,
createData: ImmutablePropTypes.list.isRequired,
editData: ImmutablePropTypes.map.isRequired,
selectedItems: ImmutablePropTypes.list.isRequired,
isEditing: PropTypes.bool.isRequired,
// Action props
cellShowMessage: PropTypes.func.isRequired,
cellShowMessages: PropTypes.func.isRequired,
setData: PropTypes.func.isRequired,
saveSuccess: PropTypes.func.isRequired,
removeSuccess: PropTypes.func.isRequired,
saveFail: PropTypes.func.isRequired,
removeFail: PropTypes.func.isRequired,
};
constructor() {
super();
this.state = {
gridSettings: GRID,
};
}
componentWillMount() {
this.props.setData(GRID, columns, data);
}
handleWarnClick = () => {
this.props.cellShowMessage(GRID, 'warning', 3, ['float'], 'Warning');
}
handleInfoClick = () => {
const messages = Map()
.setIn(['info', 1], Map({ text: 'Text message 1', date: 'Date message 1' }))
.setIn(['info', 3], Map({ text: 'Text message 3' }));
this.props.cellShowMessages(GRID, messages);
}
handleStartClick = () => {
Perf.start();
}
handleStopClick = () => {
Perf.stop();
Perf.printWasted(Perf.getLastMeasurements());
}
handleOnSave = () => {
const { createData, editData, allData } = this.props;
let newData = [];
if (createData.size) {
// Creating mode
newData = createData.toJS();
} else if (editData.size) {
// Editing mode
editData.forEach((editItem, editId) => {
const foundItem = allData.find(i => i.getIn(GRID.idKeyPath) === editId);
if (foundItem) {
newData.push(foundItem.merge(editItem));
}
});
}
// Make an api call here with newData
const errors = false;
if (!errors) {
// Take new items from backend
const savedItems = newData;
this.props.saveSuccess(GRID, columns, savedItems);
} else {
this.props.saveFail(GRID);
}
}
handleOnRemove = () => {
const removedItems = this.props.selectedItems.toJS();
const errors = false;
if (!errors) {
this.props.removeSuccess(GRID, removedItems);
} else {
this.props.removeFail(GRID);
}
}
useLocalStorage = () => {
this.setState({ gridSettings: GRID });
this.props.setData(GRID, columns, data);
}
useCustomStorage = () => {
const gridSettings = {
...GRID,
configStorage: {
load: () => ({
hiddenColumns: ['text3', 'text4'],
columnOrder: ['select', 'text', 'text2', 'number', 'float', 'boolean', 'date'],
columnWidths: { text3: 200, text4: 300 },
}),
save: (conf) => {
console.log(`Save config: ${JSON.stringify(conf)}`); // eslint-disable-line
},
},
};
this.setState({ gridSettings });
this.props.setData(gridSettings, columns, data);
}
render() {
const disableActionSave = (this.props.isEditing && this.props.editData.size === 0);
const actionBar = (
<Form inline>
<Button
onClick={this.handleWarnClick}
>
Show Warning
</Button>
<Button
onClick={this.handleInfoClick}
>
Show Info
</Button>
<Button
onClick={this.handleStartClick}
>
Start Perf
</Button>
<Button
onClick={this.handleStopClick}
>
Stop Perf
</Button>{' '}
<FormGroup>
<Radio
name="configStorage"
onChange={this.useLocalStorage}
defaultChecked
inline
>
Local storage
</Radio>
<Radio
name="configStorage"
onChange={this.useCustomStorage}
inline
>
Custom storage
</Radio>
</FormGroup>
</Form>
);
return (
<div className="oc-content oc-flex-column">
<div>
<h1>Datagrid</h1>
</div>
<Datagrid
grid={this.state.gridSettings}
columns={columns}
disableActionSave={disableActionSave}
actionBarLeft={actionBar}
filtering
columnSettings
inlineEdit
removing
multiSelect
rowSelect
rowSelectCheckboxColumn
onSave={this.handleOnSave}
onRemove={this.handleOnRemove}
/>
</div>
);
}
}