Skip to content

Commit

Permalink
feat(picker): add prop default-value
Browse files Browse the repository at this point in the history
  • Loading branch information
xxyan0205 committed Dec 21, 2018
1 parent 9657286 commit b9d99e3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
1 change: 1 addition & 0 deletions components/picker/README.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Vue.component(Picker.name, Picker)
|data|data source|Array<{value, lable, ...}>[]|`[]`|-|
|cols|number of columns|Number|`1`|-|
|default-index|indexes of initially selected items in each column|Array|`[]`|-|
|default-value|values of initially selected items in each column|Array|`[]`|Available key `text/lable/value`|
|invalid-index|indexes of disabled items in each column|Array|`[]`|array of multiple disabled items, such as `[[1,2], 2]`|
|is-view|inline display in page, otherwise it shows as `Popup`|Boolean|`false`|-|
|is-cascade|data in each column is cascaded or not|Boolean|`false`|see #Appendix for the format of cascaded data|
Expand Down
1 change: 1 addition & 0 deletions components/picker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Vue.component(Picker.name, Picker)
|data|数据源|Array<{value, lable, ...}>[]|`[]`|-|
|cols|数据列数|Number|`1`|-|
|default-index|选择器各列初始选中项索引|Array|`[]`|-|
|default-value|选择器各列初始选中项值|Array|`[]`|可用字段`text/lable/value`|
|invalid-index|选择器各列不可用选项索引|Array|`[]`|某列多个不可用项使用数组,单个使用数字, 如`[[1,2], 2]`|
|is-view|是否内嵌在页面内展示,否则以弹层形式|Boolean|`false`|-|
|is-cascade|各列数据是否级联|Boolean|`false`|级联数据格式见附录|
Expand Down
20 changes: 18 additions & 2 deletions components/picker/cascade.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@ const defaultOptions = {
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
}

/**
Expand All @@ -26,9 +42,9 @@ export default function(picker, options = {}, fn) {

/* istanbul ignore next */
for (let i = options.currentLevel + 1; i < options.maxLevel; i++) {
const activeIndex = options.defaultIndex[i] || 0
const columnValues = values.children || []
const columnValues = (!i ? values[i] : values.children) || []
picker.setColumnValues(i, columnValues)
const activeIndex = getDefaultIndex(columnValues, options.defaultIndex[i], options.defaultValue[i])
values = columnValues[activeIndex] || []
}

Expand Down
25 changes: 14 additions & 11 deletions components/picker/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,15 @@ export default {
defaultIndex: {
type: Array,
default() {
if (this.cols < 1) {
return []
}
const arr = new Array(this.cols)
for (let i = 0, len = arr.length; i < len; i++) {
arr[i] = 0
}
return arr
// if (this.cols < 1) {
// return []
// }
// const arr = new Array(this.cols)
// for (let i = 0, len = arr.length; i < len; i++) {
// arr[i] = 0
// }
// return arr
return []
},
},
invalidIndex: {
Expand Down Expand Up @@ -221,13 +222,15 @@ export default {
}
const defaultIndex = this.$_getDefaultIndex()
const defaultIndexOfFirstColumn = defaultIndex[0] || 0
const defaultValue = this.$_getDefaultValue()
// const defaultIndexOfFirstColumn = defaultIndex[0] || 0
this.$nextTick(() => {
cascadePicker(this.column, {
currentLevel: 0,
currentLevel: -1,
maxLevel: this.cols,
values: this.data[0] ? this.data[0][defaultIndexOfFirstColumn] || [] : [],
values: this.data || [],
defaultIndex,
defaultValue,
})
})
},
Expand Down

0 comments on commit b9d99e3

Please sign in to comment.