Skip to content

Commit

Permalink
fix issue #163 , array findIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
dolymood committed Apr 18, 2018
1 parent e07b734 commit 778b0ed
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
17 changes: 16 additions & 1 deletion src/common/helpers/util.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
function findIndex(ary, fn) {
if (ary.findIndex) {
return ary.findIndex(fn)
}
let index = -1
ary.some(function (item, i, ary) {
const ret = fn.call(this, item, i, ary)
if (ret) {
index = i
return ret
}
})
return index
}

function deepAssign(to, from) {
for (let key in from) {
if (!to[key] || typeof to[key] !== 'object') {
Expand Down Expand Up @@ -65,4 +80,4 @@ function resetTypeValue(obj, key, defVal) {
}
}

export { deepAssign, createAddAPI, toLocaleDateString, resetTypeValue }
export { findIndex, deepAssign, createAddAPI, toLocaleDateString, resetTypeValue }
8 changes: 4 additions & 4 deletions src/components/date-picker/date-picker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<script>
import apiMixin from '../../common/mixins/api'
import pickerMixin from '../../common/mixins/picker'
import { deepAssign } from '../../common/helpers/util'
import { deepAssign, findIndex } from '../../common/helpers/util'
import { computeNatureMaxDay, formatType } from '../../common/lang/date'
const COMPONENT_NAME = 'cube-date-picker'
Expand Down Expand Up @@ -145,13 +145,13 @@
selectedIndex() {
let selectedIndex = []
let data = this.data
let findIndex
let index
for (let i = 0; i < this.columnCount && i < 6 - this.startIndex; i++) {
findIndex = data.findIndex((item) => {
index = findIndex(data, (item) => {
return this.valueArray[i] && item.value === this.valueArray[i]
})
selectedIndex[i] = findIndex !== -1 ? findIndex : 0
selectedIndex[i] = index !== -1 ? index : 0
data = data[selectedIndex[i]].children
}
Expand Down
13 changes: 7 additions & 6 deletions src/components/select/select.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</template>

<script>
import { findIndex } from '../../common/helpers/util'
const COMPONENT_NAME = 'cube-select'
const EVENT_CHANGE = 'change'
Expand Down Expand Up @@ -67,23 +68,23 @@
return item
})]
},
findIndex() {
const findIndex = this.adaptOptions[0].findIndex((item) => {
valueIndex() {
const index = findIndex(this.adaptOptions[0], (item) => {
return item.value === this.value
})
this.picker && this.picker.setData(this.adaptOptions, findIndex !== -1 ? [findIndex] : [0])
this.picker && this.picker.setData(this.adaptOptions, index !== -1 ? [index] : [0])
return findIndex
return index
},
selectedText() {
return this.findIndex !== -1 ? this.adaptOptions[0][this.findIndex].text : ''
return this.valueIndex !== -1 ? this.adaptOptions[0][this.valueIndex].text : ''
}
},
created() {
this.picker = this.$createPicker({
title: this.title,
data: this.adaptOptions,
selectedIndex: this.findIndex !== -1 ? [this.findIndex] : [0],
selectedIndex: this.valueIndex !== -1 ? [this.valueIndex] : [0],
cancelTxt: this.cancelTxt,
confirmTxt: this.confirmTxt,
onSelect: this.hided,
Expand Down

0 comments on commit 778b0ed

Please sign in to comment.