t20-common-lib 0.15.3 → 0.15.4
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/package.json
CHANGED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<T20-Scroll-Load-Select
|
|
3
|
+
v-model="_value"
|
|
4
|
+
v-bind="passThroughProps"
|
|
5
|
+
v-on="innerListeners"
|
|
6
|
+
@canGetData="handleCanGetData"
|
|
7
|
+
/>
|
|
8
|
+
</template>
|
|
9
|
+
|
|
10
|
+
<script>
|
|
11
|
+
import { getDataForGet, getDataForPost } from '../utils/request'
|
|
12
|
+
|
|
13
|
+
export default {
|
|
14
|
+
name: 'ScrollLoadSelect',
|
|
15
|
+
props: {
|
|
16
|
+
value: {
|
|
17
|
+
type: [String, Number, Array, Object],
|
|
18
|
+
default: undefined
|
|
19
|
+
},
|
|
20
|
+
items: {
|
|
21
|
+
type: Object,
|
|
22
|
+
default: () => ({})
|
|
23
|
+
},
|
|
24
|
+
label: {
|
|
25
|
+
type: String,
|
|
26
|
+
default: ''
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
computed: {
|
|
30
|
+
_value: {
|
|
31
|
+
get() {
|
|
32
|
+
return this.value
|
|
33
|
+
},
|
|
34
|
+
set(val) {
|
|
35
|
+
this.$emit('input', val)
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
innerListeners() {
|
|
39
|
+
const listeners = { ...this.$listeners }
|
|
40
|
+
delete listeners.canGetData
|
|
41
|
+
return listeners
|
|
42
|
+
},
|
|
43
|
+
runtimeLabelKey() {
|
|
44
|
+
return this.items?.api?.labelKey || this.items?.props?.labelKey || this.$attrs.labelKey || 'label'
|
|
45
|
+
},
|
|
46
|
+
runtimeValueKey() {
|
|
47
|
+
return this.items?.api?.valueKey || this.items?.props?.valueKey || this.$attrs.valueKey || 'value'
|
|
48
|
+
},
|
|
49
|
+
runtimeSearchKeyName() {
|
|
50
|
+
return this.items?.api?.searchKeyName || this.items?.props?.searchKeyName || this.$attrs.searchKeyName || 'searchKey'
|
|
51
|
+
},
|
|
52
|
+
runtimePageSize() {
|
|
53
|
+
return this.items?.api?.pageSize || this.items?.props?.pageSize || this.$attrs.pageSize || 20
|
|
54
|
+
},
|
|
55
|
+
runtimeSelectedLabel() {
|
|
56
|
+
return this.items?.props?.selectedLabel || this.$attrs.selectedLabel || this.label || ''
|
|
57
|
+
},
|
|
58
|
+
runtimeRequestFn() {
|
|
59
|
+
if (this.items?.api?.apiUrl && this.items?.api?.requestMethod) {
|
|
60
|
+
return this.fetchByApiConfig
|
|
61
|
+
}
|
|
62
|
+
return this.$attrs.requestFn || this.$attrs['request-fn'] || this.fetchByStaticOptions
|
|
63
|
+
},
|
|
64
|
+
passThroughProps() {
|
|
65
|
+
return {
|
|
66
|
+
...this.$attrs,
|
|
67
|
+
...(this.items?.props || {}),
|
|
68
|
+
requestFn: this.runtimeRequestFn,
|
|
69
|
+
labelKey: this.runtimeLabelKey,
|
|
70
|
+
valueKey: this.runtimeValueKey,
|
|
71
|
+
searchKeyName: this.runtimeSearchKeyName,
|
|
72
|
+
pageSize: this.runtimePageSize,
|
|
73
|
+
selectedLabel: this.runtimeSelectedLabel
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
methods: {
|
|
78
|
+
resolvePath(data, path) {
|
|
79
|
+
if (!path) return data
|
|
80
|
+
return String(path)
|
|
81
|
+
.split('.')
|
|
82
|
+
.reduce((acc, key) => (acc == null ? acc : acc[key]), data)
|
|
83
|
+
},
|
|
84
|
+
normalizeList(rawList) {
|
|
85
|
+
if (!Array.isArray(rawList)) return []
|
|
86
|
+
return rawList.map(item => {
|
|
87
|
+
if (!item || typeof item !== 'object') {
|
|
88
|
+
return {
|
|
89
|
+
[this.runtimeLabelKey]: String(item),
|
|
90
|
+
[this.runtimeValueKey]: item
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return item
|
|
94
|
+
})
|
|
95
|
+
},
|
|
96
|
+
async fetchByApiConfig(params = {}) {
|
|
97
|
+
const apiConfig = this.items?.api || {}
|
|
98
|
+
const method = String(apiConfig.requestMethod || '').toUpperCase()
|
|
99
|
+
const reqKeys = apiConfig.reqKeys || {}
|
|
100
|
+
const headerConfig = apiConfig.headerConfig || {}
|
|
101
|
+
const requestData = {
|
|
102
|
+
...reqKeys,
|
|
103
|
+
...params
|
|
104
|
+
}
|
|
105
|
+
let res = null
|
|
106
|
+
if (method === 'GET') {
|
|
107
|
+
res = await getDataForGet(apiConfig.apiUrl, requestData, headerConfig)
|
|
108
|
+
} else if (method === 'POST') {
|
|
109
|
+
res = await getDataForPost(apiConfig.apiUrl, requestData, headerConfig)
|
|
110
|
+
} else {
|
|
111
|
+
return []
|
|
112
|
+
}
|
|
113
|
+
const list = apiConfig.dataProp ? this.resolvePath(res, apiConfig.dataProp) : (res && res.data)
|
|
114
|
+
return this.normalizeList(list)
|
|
115
|
+
},
|
|
116
|
+
async fetchByStaticOptions(params = {}) {
|
|
117
|
+
const sourceOptions = Array.isArray(this.items?.options) ? this.items.options : []
|
|
118
|
+
const current = Number(params.current || 1)
|
|
119
|
+
const size = Number(params.size || this.runtimePageSize || 20)
|
|
120
|
+
const searchValue = params[this.runtimeSearchKeyName]
|
|
121
|
+
const normalizedKeyword = searchValue == null ? '' : String(searchValue).toLowerCase()
|
|
122
|
+
const filtered = sourceOptions.filter(item => {
|
|
123
|
+
if (!normalizedKeyword) return true
|
|
124
|
+
const label = item && item[this.runtimeLabelKey]
|
|
125
|
+
return String(label == null ? '' : label).toLowerCase().includes(normalizedKeyword)
|
|
126
|
+
})
|
|
127
|
+
const start = (current - 1) * size
|
|
128
|
+
return filtered.slice(start, start + size)
|
|
129
|
+
},
|
|
130
|
+
handleCanGetData(val, option) {
|
|
131
|
+
if (Array.isArray(option)) {
|
|
132
|
+
const labels = option
|
|
133
|
+
.map(item => item && item[this.runtimeLabelKey])
|
|
134
|
+
.filter(label => label !== undefined && label !== null)
|
|
135
|
+
this.$emit('update:label', labels.join(','))
|
|
136
|
+
} else if (option && typeof option === 'object') {
|
|
137
|
+
this.$emit('update:label', option[this.runtimeLabelKey] || '')
|
|
138
|
+
} else if (val === '' || val === null || val === undefined) {
|
|
139
|
+
this.$emit('update:label', '')
|
|
140
|
+
}
|
|
141
|
+
this.$emit('canGetData', val, option)
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
</script>
|