forked from didi/mand-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcascade.js
More file actions
53 lines (46 loc) · 1.38 KB
/
cascade.js
File metadata and controls
53 lines (46 loc) · 1.38 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
import {extend, warn} from '../_util'
const defaultOptions = {
currentLevel: 0,
maxLevel: 0,
values: [],
defaultIndex: [],
defaultValue: [],
}
function getDefaultIndex(data, defaultIndex, defaultValue) {
let activeIndex = 0
if (defaultIndex !== undefined) {
return defaultIndex
} else if (defaultValue !== undefined) {
data.some((item, index) => {
if (item.text === defaultValue || item.label === defaultValue || item.value === defaultValue) {
activeIndex = index
return true
}
})
}
return activeIndex
}
/**
* cascade column by set value of following columns
* @param {*} picker instance of picker-column
* @param {*} options { currentLevel, maxLevel, values }
* @param {*} fn
*/
export default function(picker, options = {}, fn) {
options = extend(defaultOptions, options)
/* istanbul ignore if */
if (!picker) {
warn('cascade: picker is undefined')
return
}
let values = options.values
/* istanbul ignore next */
for (let i = options.currentLevel + 1; i < options.maxLevel; i++) {
const columnValues = (!i ? values[i] : values.children) || []
picker.setColumnValues(i, columnValues)
let activeIndex = getDefaultIndex(columnValues, options.defaultIndex[i], options.defaultValue[i])
activeIndex >= columnValues.length && (activeIndex = 0)
values = columnValues[activeIndex] || []
}
fn && fn()
}