Skip to content

Commit

Permalink
fix: picker reset index (#99) (#102)
Browse files Browse the repository at this point in the history
* fix: 解决Picker切换选项后无法重置的问题 [#99]
  • Loading branch information
zuolung authored Dec 3, 2021
1 parent 7f9f1d7 commit 4fd2a76
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
16 changes: 8 additions & 8 deletions packages/vantui/src/picker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const Picker = forwardRef(function Index(
} = props

const children = useRef<Array<any>>([])
const handleIndex = useRef<number>(-1)

useEffect(
function () {
Expand All @@ -58,6 +59,7 @@ const Picker = forwardRef(function Index(
if (typeof event === 'number' || !type) {
if (onChange) {
const event_ = {}
handleIndex.current = event
Object.defineProperties(event_, {
detail: {
value: {
Expand Down Expand Up @@ -133,11 +135,8 @@ const Picker = forwardRef(function Index(
[columns],
)

const setColumnValues = useCallback(function (
index,
options,
needReset = false,
) {
const setColumnValues = useCallback(function (index, options) {
if (index <= handleIndex.current) return
const column = children.current[index]
if (column == null) {
return Promise.reject(new Error('setColumnValues: 对应列不存在'))
Expand All @@ -147,15 +146,16 @@ const Picker = forwardRef(function Index(
if (isSame) {
return Promise.resolve()
}
const cIndex = column.getCurrentIndex()
return column.set({ options }).then(() => {
if (needReset) {
if (cIndex > options.length) {
setTimeout(() => {
column.setIndex(0)
handleIndex.current = -1
})
}
})
},
[])
}, [])

const getValues = useCallback(function () {
return children.current.map((child) => child.getValue())
Expand Down
6 changes: 1 addition & 5 deletions packages/vantui/types/picker.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,7 @@ export type IPickerInstance = {
/**
* @description 设置每一列的值
*/
setColumnValues: (
index: number,
options: string[],
needReset: boolean,
) => Promise<any>
setColumnValues: (index: number, options: string[]) => Promise<any>
/**
* @description 获取每一列的值
* @default
Expand Down

0 comments on commit 4fd2a76

Please sign in to comment.