1
|
|
|
import { List, fromJS } from 'immutable'; |
2
|
|
|
import { camelize } from './camelize'; |
3
|
|
|
|
4
|
|
|
export const getData = ( |
5
|
|
|
row = {}, columns = {}, colIndex = 0, editorValues = {} |
6
|
|
|
) => { |
7
|
|
|
|
8
|
|
|
const column = columns[colIndex]; |
9
|
|
|
|
10
|
|
|
if (!column) { |
11
|
|
|
return undefined; |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
const dataIndex = column.dataIndex || null; |
15
|
|
|
|
16
|
|
|
if (!dataIndex) { |
17
|
|
|
throw new Error('No dataIndex found on column', column); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
if (editorValues |
21
|
|
|
&& editorValues.get |
22
|
|
|
&& editorValues.get(dataIndex) !== undefined) { |
23
|
|
|
return editorValues.get(dataIndex); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
if (typeof dataIndex === 'string') { |
27
|
|
|
const val = row |
28
|
|
|
&& row.get |
29
|
|
|
&& row.get(dataIndex) !== undefined |
30
|
|
|
? row.get(dataIndex) |
31
|
|
|
: null; |
32
|
|
|
|
33
|
|
|
return val && val.toJS ? val.toJS() : val; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
else if (Array.isArray(dataIndex)) { |
|
|
|
|
37
|
|
|
const val = getValueFromDataIndexArr(row, dataIndex); |
38
|
|
|
return val && val.toJS ? val.toJS() : val; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
}; |
42
|
|
|
|
43
|
|
|
export const setKeysInData = (data) => { |
44
|
|
|
|
45
|
|
|
if (List.isList(data)) { |
46
|
|
|
|
47
|
|
|
if (data.getIn([0, '_key'])) { |
48
|
|
|
return data; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return data.map((item, i) => item.set('_key', `row-${i}`)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (!data || !Array.isArray(data)) { |
55
|
|
|
return List([]); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (data[0] && data[0]._key === undefined) { |
59
|
|
|
data.forEach((row, i) => { |
60
|
|
|
row._key = `row-${i}`; |
61
|
|
|
}); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return fromJS(data); |
65
|
|
|
}; |
66
|
|
|
|
67
|
|
|
export const getRowKey = (columns, rowValues, suffix) => { |
68
|
|
|
|
69
|
|
|
const uniqueCol = columns.filter(col => col.createKeyFrom); |
70
|
|
|
let val = rowValues.get('_key'); |
71
|
|
|
|
72
|
|
|
if (uniqueCol.length > 1) { |
73
|
|
|
throw new Error('Only one column can declare createKeyFrom'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (uniqueCol.length > 0) { |
77
|
|
|
const dataIndex = nameFromDataIndex(uniqueCol[0]); |
78
|
|
|
val = rowValues && rowValues.get(dataIndex) |
79
|
|
|
? rowValues.get(dataIndex) |
80
|
|
|
: rowValues.get('_key'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (suffix) { |
84
|
|
|
val = `${val}-${suffix}`; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return val; |
88
|
|
|
}; |
89
|
|
|
|
90
|
|
|
export const setDataAtDataIndex = (row, dataIndex, val) => { |
91
|
|
|
|
92
|
|
|
if (!row.toJS) { |
93
|
|
|
row = fromJS(row); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (typeof dataIndex === 'string') { |
97
|
|
|
return row.set(dataIndex, val); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if (row.getIn(dataIndex)) { |
101
|
|
|
return row.setIn(dataIndex, val); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
throw new Error('Invalid key path'); |
105
|
|
|
}; |
106
|
|
|
|
107
|
|
|
export const getValueFromDataIndexArr = (row, dataIndex) => { |
108
|
|
|
const val = row.getIn(dataIndex); |
109
|
|
|
|
110
|
|
|
return val !== undefined |
111
|
|
|
? val |
112
|
|
|
: ''; |
113
|
|
|
}; |
114
|
|
|
|
115
|
|
|
export const nameFromDataIndex = (column) => { |
116
|
|
|
|
117
|
|
|
if (!column) { |
118
|
|
|
return ''; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if (typeof column.dataIndex === 'string') { |
122
|
|
|
return column.dataIndex; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if (Array.isArray(column.dataIndex)) { |
126
|
|
|
return column.dataIndex[column.dataIndex.length - 1]; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if (!column.dataIndex) { |
|
|
|
|
130
|
|
|
return camelize(column.name); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
}; |
134
|
|
|
|
This check looks for functions where a
return
statement is found in some execution paths, but not in all.Consider this little piece of code
The function
isBig
will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly returnundefined
.This behaviour may not be what you had intended. In any case, you can add a
return undefined
to the other execution path to make the return value explicit.