/
use-picker.ts
224 lines (193 loc) · 6.04 KB
/
use-picker.ts
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
import { computed, reactive, ref, toRefs, watch } from 'vue'
import type { PickerFieldNames, PickerOption } from '../pickercolumn/types'
import { CANCEL_EVENT, CHANGE_EVENT, CONFIRM_EVENT, PREFIX, UPDATE_MODEL_EVENT } from '../_constants'
import { getMainClass } from '../_utils'
const DEFAULT_FILED_NAMES = {
text: 'text',
value: 'value',
children: 'children',
className: '',
}
export const componentName = `${PREFIX}-picker`
export function usePicker(props: any, emit: any) {
const state = reactive({
formattedColumns: props.columns,
})
const columnFieldNames = computed(() => {
return {
...DEFAULT_FILED_NAMES,
...(props.fieldNames as PickerFieldNames),
}
})
// 选中项
const defaultValues = ref<(number | string)[]>([])
// 当前类型
const columnsType = computed(() => {
const firstColumn: PickerOption | PickerOption[] = state.formattedColumns[0]
const fields = columnFieldNames.value
if (firstColumn) {
if (Array.isArray(firstColumn))
return 'multiple'
if (fields.children in firstColumn)
return 'cascade'
}
return 'single'
})
// 级联数据格式化
const formatCascade = (columns: PickerOption[], defaultValues: (number | string)[]) => {
const formatted: PickerOption[][] = []
const fields = columnFieldNames.value
let cursor: PickerOption = {
text: '',
value: '',
[fields.children]: columns,
}
let columnIndex = 0
while (cursor && cursor[fields.children]) {
const options: PickerOption[] = cursor[fields.children]
const value = defaultValues[columnIndex]
let index = options.findIndex(columnItem => columnItem[fields.value] === value)
if (index === -1)
index = 0
cursor = cursor[fields.children][index]
columnIndex++
formatted.push(options)
}
return formatted
}
// 将传入的 columns 格式化
const columnsList = computed(() => {
let result: PickerOption[][] = []
switch (columnsType.value) {
case 'multiple':
result = state.formattedColumns as PickerOption[][]
break
case 'cascade':
// 级联数据处理
result = formatCascade(
state.formattedColumns as PickerOption[],
defaultValues.value ? defaultValues.value : [],
)
break
default:
result = [state.formattedColumns] as PickerOption[][]
break
}
return result
})
const defaultIndexes = computed(() => {
const fields = columnFieldNames.value
return (columnsList.value as PickerOption[][]).map((column: PickerOption[], index: number) => {
const targetIndex = column.findIndex(item => item[fields.value] === defaultValues.value[index])
return targetIndex === -1 ? 0 : targetIndex
})
})
const pickerColumn = ref<any[]>([])
const swipeRef = (el: any) => {
if (el && pickerColumn.value.length < columnsList.value.length)
pickerColumn.value.push(el)
}
const classes = computed(() => {
return getMainClass(props, componentName)
})
const selectedOptions = computed(() => {
const fields = columnFieldNames.value
return (columnsList.value as PickerOption[][]).map((column: PickerOption[], index: number) => {
return column.find(item => item[fields.value] === defaultValues.value[index]) || column[0]
})
})
const cancel = () => {
emit(CANCEL_EVENT, {
selectedValue: defaultValues.value,
selectedOptions: selectedOptions.value,
})
}
const changeHandler = (columnIndex: number, option: PickerOption) => {
const fields = columnFieldNames.value
if (option && Object.keys(option).length) {
defaultValues.value = defaultValues.value ? defaultValues.value : []
if (columnsType.value === 'cascade') {
defaultValues.value[columnIndex] = option[fields.value] ? option[fields.value] : ''
let index = columnIndex
let cursor = option
while (cursor && cursor[fields.children] && cursor[fields.children][0]) {
defaultValues.value[index + 1] = cursor[fields.children][0][fields.value]
index++
cursor = cursor[fields.children][0]
}
// 当前改变列 的 下一列 children 值为空
if (cursor && cursor[fields.children] && cursor[fields.children].length === 0)
defaultValues.value = defaultValues.value.slice(0, index + 1)
}
else {
defaultValues.value[columnIndex] = Object.prototype.hasOwnProperty.call(option, fields.value)
? option[fields.value]
: ''
}
emit(CHANGE_EVENT, {
columnIndex,
selectedValue: defaultValues.value,
selectedOptions: selectedOptions.value,
})
}
}
const confirm = () => {
const fields = columnFieldNames.value
if (defaultValues.value && !defaultValues.value.length) {
columnsList.value.forEach((columns) => {
defaultValues.value.push(columns[0][fields.value])
})
}
emit(CONFIRM_EVENT, {
selectedValue: defaultValues.value,
selectedOptions: selectedOptions.value,
})
}
const isSameValue = (valA: any, valB: any) => JSON.stringify(valA) === JSON.stringify(valB)
const confirmHandler = () => {
pickerColumn.value.length > 0
&& pickerColumn.value.forEach((column) => {
column.stopMomentum()
})
confirm()
}
watch(
() => props.modelValue,
(newValues) => {
if (!isSameValue(newValues, defaultValues.value))
defaultValues.value = newValues
},
{ deep: true, immediate: true },
)
watch(
defaultValues,
(newValues) => {
if (!isSameValue(newValues, props.modelValue))
emit(UPDATE_MODEL_EVENT, newValues)
},
{ deep: true },
)
watch(
() => props.columns,
(val) => {
state.formattedColumns = val as PickerOption[]
},
)
return {
classes,
...toRefs(state),
columnsType,
columnsList,
columnFieldNames,
cancel,
changeHandler,
confirmHandler,
confirm,
defaultValues,
defaultIndexes,
pickerColumn,
swipeRef,
selectedOptions,
isSameValue,
}
}