-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.jsx
More file actions
258 lines (226 loc) · 8.89 KB
/
index.jsx
File metadata and controls
258 lines (226 loc) · 8.89 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/**
* NOTE: !!!!!
* when retrieving the selection from FlexSelect which is an object (map), where the keys are the id's of the flex-select elements. if you use Object.keys(selection) then the resulting array will contain the id's as string! if you need them as integer do the following Object.keys(selection).map(id => parseInd(id)) !!!!!!!!
*
*/
import React, { PureComponent } from "react";
import './styles.scss';
import classNames from 'classnames'
class FlexSelect extends PureComponent {
constructor(props) {
super(props);
this.state = {
selection: {}
}
const { getResetSelectionMethod, getSetSelectionMethod } = this.props;
if (typeof getResetSelectionMethod === 'function') getResetSelectionMethod(this.resetSelection)
if (typeof getSetSelectionMethod === 'function') getSetSelectionMethod(this.setSelection);
}
setSelection = (selection) => {
return new Promise(resolve => {
this.setState({
selection: {
...selection
}
}, () => {
resolve(selection)
})
});
}
resetSelection = () => {
const { onSelectionChange } = this.props;
return new Promise(resolve => {
this.setState({
selection: {}
}, () => {
onSelectionChange({});
resolve();
});
});
}
onElClick = (selectionId, evt) => {
const { onElClick, onSelect, onUnselect, multiple, metaData, onSelectionChange, disabled } = this.props;
// console.log("%cflex select on click ====", "color: yellow; background: orange;");
// console.log({
// disabled,
// selectionId,
// state: this.state
// });
if (!disabled) {
new Promise((resolve) => {
if (multiple) {
if (this.state.selection[selectionId]) { // selected so we inselect
const newSelection = {
...this.state.selection
};
delete newSelection[selectionId];
this.setState({
selection: newSelection
}, () => {
if (typeof onUnselect === 'function') onUnselect(selectionId, this.state.selection, metaData, this.resetSelection);
resolve({
select: false,
unselect: true,
multiple: false
});
});
} else { // select it (as it is not selected)
this.setState({
...this.state.selection,
selection: {
...this.state.selection,
[selectionId]: true
}
}, () => {
// console.log("%conElselect no multiple !!!", "color: yellow; background: black;");
// console.log({
// selectionId,
// selection: this.state.selection
// });
if (typeof onSelect === 'function') onSelect(selectionId, this.state.selection, metaData, this.resetSelection);
resolve({
select: true,
unselect: false,
multiple: true
});
});
}
} else { // not multiple
const oldSelectionIds = Object.keys(this.state.selection);
const unselect = oldSelectionIds.length > 0 ? true: false;
this.setState({
selection: {
[selectionId]: true // removing anything else, and choose the new one
}
}, () => {
resolve({
select: true,
unselect,
oldSelectionIds,
multiple: false
});
if (typeof onSelect === 'function') onSelect(selectionId, this.state.selection, metaData, this.resetSelection);
});
// just doing the selection (not closing anything)
}
})
.then((selectionInfo) => { // state changed
if (typeof onSelectionChange === 'function') onSelectionChange(this.state.selection);
// console.log("%conElClick flexSELECT", "color: yellow; background: black;");
// console.log({
// selectionId,
// selectionInfo,
// selection: this.state.selection
// });
if (typeof onElClick === 'function') onElClick(selectionId, selectionInfo, this.state.selection, metaData, this.resetSelection);
});
}
}
confirm = () => {
const { metaData, onConfirm } = this.props;
if (typeof onConfirm === 'function') onConfirm(this.state.selection, metaData, this.resetSelection);
// can handle that conditionally
// this.setState({
// selection: {}
// }, () => {
// })
}
cancel = () => {
const { metaData, onCancel, onSelectionChange } = this.props;
const selectionBeforeCancel = {...this.state.selection};
this.setState(
{
selection: {}
},
() => {
if (typeof onSelectionChange === 'function') onSelectionChange({}, true);
if (typeof onCancel === 'function') onCancel(selectionBeforeCancel, metaData);
}
)
}
unselectAll = () => {
const { metaData, onUnselectAll, onSelectionChange } = this.props;
const selectionBeforeUnselect = {...this.state.selection};
this.setState({
selection: {}
}, () => {
if (typeof onSelectionChange === 'function') onSelectionChange({});
if (typeof onUnselectAll === 'function') onUnselectAll(selectionBeforeUnselect, metaData);
});
}
setAsEl = (el, selectionId) => {
return React.cloneElement(
el,
{
onClick: () => { this.onElClick(selectionId) }
}
)
}
render() {
const { list, renderEl, multiple, forceConfirm, confirmBtnName, cancelBtnName, unselectAllBtnName, controls, classType, children, className, disabled} = this.props;
return (
<div
className={
classNames(
'FlexSelect',
className,
{
[classType]: !!classType
},
{
disabled
}
)
}
>
<div className="selectionContainer">
{
children({
setAsEl: this.setAsEl,
selection: this.state.selection
// later offer ready nice renderers (next projects and needs)
}) // rendering prop
}
</div>
{
controls &&
<div className="controls">
{
(multiple || forceConfirm) &&
<div className="confirm btn btn-primary btn-rounded" onClick={this.confirm}>
{
confirmBtnName
}
</div>
}
<div className="cancel btn btn-secondary btn-rounded" onClick={this.cancel}>
{
cancelBtnName
}
</div>
{
multiple &&
<div className="unselectAll btn btn-secondary btn-rounded" onClick={this.unselectAll}>
{
unselectAllBtnName
}
</div>
}
</div>
}
</div>
);
}
}
FlexSelect.defaultProps = {
list: [],
multiple: false,
forceConfirm: false,
confirmBtnName: 'Select',
cancelBtnName: 'Cancel',
unselectAllBtnName: 'Unselect all',
controls: true,
getSelection: null,
onSelectionChange: null
}
export default FlexSelect;