@indfnd/utils 0.0.40 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +20 -0
- package/dist/ind-utils.es.js +72 -10
- package/dist/ind-utils.umd.cjs +14 -14
- package/package.json +1 -1
- package/src/api/item.ts +2 -2
- package/src/utils/table.ts +87 -11
- package/src/utils/validate.ts +2 -2
- package/types/utils/table.d.ts +8 -1
- package/types/utils/table.d.ts.map +1 -1
package/package.json
CHANGED
package/src/api/item.ts
CHANGED
|
@@ -7,8 +7,8 @@ export function listItemTreeApi(params) {
|
|
|
7
7
|
return axios.get(`${CONTEXT}/tree/item/listItemTree`, { params })
|
|
8
8
|
}
|
|
9
9
|
export function getPriceInfo() {
|
|
10
|
-
return axios.get(`${CONTEXT}/basic/getPriceInfo`, {})
|
|
10
|
+
return axios.get(`${CONTEXT}/basic/getPriceInfo`, {})
|
|
11
11
|
}
|
|
12
12
|
export function getItem(params) {
|
|
13
|
-
return axios.get(`${CONTEXT}/basic/getItem`, { params })
|
|
13
|
+
return axios.get(`${CONTEXT}/basic/getItem`, { params })
|
|
14
14
|
}
|
package/src/utils/table.ts
CHANGED
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
* @Author: huxuetong
|
|
3
3
|
* @Date: 2023-10-27 15:05:25
|
|
4
4
|
* @Last Modified by: huxuetong
|
|
5
|
-
* @Last Modified time: 2024-03-
|
|
5
|
+
* @Last Modified time: 2024-03-27 18:18:47
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import _ from 'lodash'
|
|
9
|
-
import { isFunction, isNil } from './is-type'
|
|
8
|
+
import _, { head } from 'lodash'
|
|
9
|
+
import { isFunction, isNil, isString } from './is-type'
|
|
10
10
|
|
|
11
11
|
const GROUP_SEP = '__'
|
|
12
12
|
const VALUE_SEP = '--'
|
|
@@ -42,6 +42,12 @@ interface row2columnOption {
|
|
|
42
42
|
keyPropName?: string
|
|
43
43
|
// 组织后表格列定义title对应属性名称
|
|
44
44
|
titlePropName?: string
|
|
45
|
+
// 合计列的扁平化定义
|
|
46
|
+
sumColumnDefs?: Array<{ key: string; title: string; [propName: string]: any }>
|
|
47
|
+
// 合计列的key
|
|
48
|
+
sumColKeyProp?: 'sumColCode'
|
|
49
|
+
// 合计列的title
|
|
50
|
+
sumColTitleProp?: 'sumColName'
|
|
45
51
|
}
|
|
46
52
|
|
|
47
53
|
interface Column {
|
|
@@ -112,7 +118,8 @@ function renderColumnTree(
|
|
|
112
118
|
|
|
113
119
|
// 动态列
|
|
114
120
|
if (keyProp) {
|
|
115
|
-
let columnUniqData =
|
|
121
|
+
let columnUniqData =
|
|
122
|
+
_.uniqBy(data, columnGroup.keyProp)?.filter((item) => !isNil(item[columnGroup.keyProp])) || []
|
|
116
123
|
if (sortProp) {
|
|
117
124
|
const unSortData = columnUniqData.filter((item) => isNil(item[sortProp]))
|
|
118
125
|
const sortData = columnUniqData.filter((item) => !isNil(item[sortProp]))
|
|
@@ -156,6 +163,12 @@ function renderColumnTree(
|
|
|
156
163
|
...args,
|
|
157
164
|
[keyPropName]: `${keyPrefix}${keyProp}${VALUE_SEP}${item[keyProp]}${suffix}`,
|
|
158
165
|
[titlePropName]: columnTitle,
|
|
166
|
+
headerTooltip: renderHeaderTooltip({
|
|
167
|
+
tooltip: headerTooltip,
|
|
168
|
+
item,
|
|
169
|
+
parents,
|
|
170
|
+
title: columnTitle,
|
|
171
|
+
}),
|
|
159
172
|
}
|
|
160
173
|
})
|
|
161
174
|
}
|
|
@@ -224,6 +237,49 @@ function renderRowData(data: Array<any>, keys: string[]) {
|
|
|
224
237
|
return value
|
|
225
238
|
}
|
|
226
239
|
|
|
240
|
+
function getRowKeyValue(item: any, rowKey: string | string[]): string {
|
|
241
|
+
if (!item) return ''
|
|
242
|
+
if (isString(rowKey)) return item[rowKey] || ''
|
|
243
|
+
for (const key of rowKey) {
|
|
244
|
+
if (item[key]) {
|
|
245
|
+
return item[key]
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
return ''
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function renderSumColumn(keys, titles, idx, columns, def) {
|
|
252
|
+
if (idx >= keys.length) return
|
|
253
|
+
|
|
254
|
+
const key = keys[idx]
|
|
255
|
+
let header = _.find(columns, { key })
|
|
256
|
+
if (!header) {
|
|
257
|
+
header = { key, title: titles[idx], children: [] }
|
|
258
|
+
columns.push(header)
|
|
259
|
+
|
|
260
|
+
if (idx === keys.length - 1) {
|
|
261
|
+
Object.assign(header, def, { title: titles[idx] })
|
|
262
|
+
return
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
renderSumColumn(keys, titles, idx + 1, header.children, def)
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function renderSumColumns({ sumColumnDefs }: row2columnOption): Column[] {
|
|
270
|
+
const columns = []
|
|
271
|
+
|
|
272
|
+
for (const def of sumColumnDefs) {
|
|
273
|
+
const { key, title } = def
|
|
274
|
+
const keys = key.split(GROUP_SEP)
|
|
275
|
+
const titles = title.split(GROUP_SEP)
|
|
276
|
+
|
|
277
|
+
renderSumColumn(keys, titles, 0, columns, def)
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
return columns
|
|
281
|
+
}
|
|
282
|
+
|
|
227
283
|
/**
|
|
228
284
|
* 行转列公共方法
|
|
229
285
|
* ! 目前是根据非动态列下全是非动态列的分组来组织的,若动态列和非动态列相互穿插分组,那就糟糕了
|
|
@@ -237,22 +293,42 @@ function renderRowData(data: Array<any>, keys: string[]) {
|
|
|
237
293
|
export function row2column(
|
|
238
294
|
data: Array<any> = [],
|
|
239
295
|
columnGroups: ColumnGroup[] = [],
|
|
240
|
-
rowKey: string,
|
|
296
|
+
rowKey: string | string[],
|
|
241
297
|
option: row2columnOption = {},
|
|
242
298
|
): row2columnResult {
|
|
243
299
|
// 组织表头
|
|
244
|
-
const columns =
|
|
245
|
-
|
|
246
|
-
|
|
300
|
+
const columns = []
|
|
301
|
+
if (option.sumColumnDefs && option.sumColumnDefs.length) {
|
|
302
|
+
columns.push(...renderSumColumns(option))
|
|
303
|
+
}
|
|
304
|
+
const groupColumns =
|
|
305
|
+
_.flatten<Column>(columnGroups.map((group) => renderColumnTree(data, group, option))) || []
|
|
306
|
+
columns.push(...groupColumns)
|
|
247
307
|
const leafColumns = getLeafColumns(columns)
|
|
248
308
|
|
|
249
309
|
// 组织数据
|
|
250
|
-
const rowKeyUniqData =
|
|
251
|
-
|
|
310
|
+
const rowKeyUniqData =
|
|
311
|
+
_.uniqBy(data, (item) => getRowKeyValue(item, rowKey))?.filter(
|
|
312
|
+
(item) => !isNil(getRowKeyValue(item, rowKey)),
|
|
313
|
+
) || []
|
|
314
|
+
const groupData = _.groupBy(data, (item) => getRowKeyValue(item, rowKey))
|
|
252
315
|
const rltData = rowKeyUniqData.map((item) => {
|
|
253
|
-
const
|
|
316
|
+
const rowKeyValue = getRowKeyValue(item, rowKey)
|
|
317
|
+
const rowGroupData = groupData[rowKeyValue] || []
|
|
254
318
|
const newItem = { ...item }
|
|
255
319
|
|
|
320
|
+
if (option.sumColumnDefs && option.sumColumnDefs.length) {
|
|
321
|
+
option.sumColumnDefs.forEach((def) => {
|
|
322
|
+
const { key } = def
|
|
323
|
+
const keys = key.split(GROUP_SEP) || []
|
|
324
|
+
const code = keys?.[0] || ''
|
|
325
|
+
const indexCol = keys?.[keys.length - 1] || ''
|
|
326
|
+
const { sumColKeyProp = 'sumColCode' } = option
|
|
327
|
+
const itemData = _.find(rowGroupData, { [sumColKeyProp]: code }) || {}
|
|
328
|
+
newItem[key] = itemData[indexCol]
|
|
329
|
+
})
|
|
330
|
+
}
|
|
331
|
+
|
|
256
332
|
leafColumns.forEach((column) => {
|
|
257
333
|
const columnKey: string = column[option.keyPropName || 'key'] || ''
|
|
258
334
|
const keys = columnKey.split(GROUP_SEP) || []
|
package/src/utils/validate.ts
CHANGED
|
@@ -135,7 +135,7 @@ export function checkTel(val) {
|
|
|
135
135
|
export function checkVehicleNo(str: string): boolean {
|
|
136
136
|
// 新能源车
|
|
137
137
|
const newReg =
|
|
138
|
-
/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z]-(([DF]((?![IO])[a-zA-Z0-9](?![IO]))[0-9]{4})|([0-9]{5}[DF]))$/
|
|
138
|
+
/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z](-)?(([DF]((?![IO])[a-zA-Z0-9](?![IO]))[0-9]{4})|([0-9]{5}[DF]))$/
|
|
139
139
|
|
|
140
140
|
if (newReg.test(str)) {
|
|
141
141
|
return true
|
|
@@ -143,7 +143,7 @@ export function checkVehicleNo(str: string): boolean {
|
|
|
143
143
|
|
|
144
144
|
// 非新能源车
|
|
145
145
|
const reg =
|
|
146
|
-
/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z]-[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]$/
|
|
146
|
+
/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z](-)?[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]$/
|
|
147
147
|
|
|
148
148
|
return reg.test(str)
|
|
149
149
|
}
|
package/types/utils/table.d.ts
CHANGED
|
@@ -15,6 +15,13 @@ interface ColumnGroup {
|
|
|
15
15
|
interface row2columnOption {
|
|
16
16
|
keyPropName?: string
|
|
17
17
|
titlePropName?: string
|
|
18
|
+
sumColumnDefs?: Array<{
|
|
19
|
+
key: string
|
|
20
|
+
title: string
|
|
21
|
+
[propName: string]: any
|
|
22
|
+
}>
|
|
23
|
+
sumColKeyProp?: 'sumColCode'
|
|
24
|
+
sumColTitleProp?: 'sumColName'
|
|
18
25
|
}
|
|
19
26
|
interface Column {
|
|
20
27
|
key?: string
|
|
@@ -39,7 +46,7 @@ interface row2columnResult {
|
|
|
39
46
|
export declare function row2column(
|
|
40
47
|
data: Array<any>,
|
|
41
48
|
columnGroups: ColumnGroup[],
|
|
42
|
-
rowKey: string,
|
|
49
|
+
rowKey: string | string[],
|
|
43
50
|
option?: row2columnOption,
|
|
44
51
|
): row2columnResult
|
|
45
52
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"table.d.ts","sourceRoot":"","sources":["../../src/utils/table.ts"],"names":[],"mappings":"AAaA,UAAU,WAAW;IAEnB,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,KAAK,CAAC,EAAE,MAAM,CAAA;IAGd,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,cAAc,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,CAAA;IAE1C,QAAQ,CAAC,EAAE,MAAM,CAAA;IAEjB,SAAS,CAAC,EAAE,OAAO,GAAG,KAAK,GAAG,MAAM,CAAA;IAEpC,aAAa,CAAC,EAAE,MAAM,CAAA;IAEtB,eAAe,CAAC,EAAE,OAAO,CAAA;IAGzB,aAAa,CAAC,EAAE,OAAO,GAAG,MAAM,GAAG,CAAC,CAAC,IAAI,EAAE,GAAG,EAAE,OAAO,EAAE,KAAK,CAAC,GAAG,CAAC,KAAK,MAAM,CAAC,CAAA;IAG/E,QAAQ,CAAC,EAAE,WAAW,EAAE,CAAA;IACxB,CAAC,QAAQ,EAAE,MAAM,GAAG,GAAG,CAAA;CACxB;AAED,UAAU,gBAAgB;IAExB,WAAW,CAAC,EAAE,MAAM,CAAA;IAEpB,aAAa,CAAC,EAAE,MAAM,CAAA;
|
|
1
|
+
{"version":3,"file":"table.d.ts","sourceRoot":"","sources":["../../src/utils/table.ts"],"names":[],"mappings":"AAaA,UA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