Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(h5): picker-view 默认值无法回显 #14713

Merged
merged 3 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,59 +22,47 @@ export class PickerViewColumn {
@State()
isInit: boolean = false

@State()
isMove: boolean = false

// 选中后的结果回调
@Event({ eventName: 'onselect' }) onChange: EventEmitter
@Event({ eventName: 'onselectstart' }) onSelectStart: EventEmitter
@Event({ eventName: 'onselectend' }) onSelectEnd: EventEmitter

@Listen('scroll')
onScroll(_event: UIEvent) {
if (!this.isMove) {
this.isMove = true
this.onSelectStart.emit()
}
this.handleSelected()
}

@Listen('mouseup')
@Listen('mouseout')
@Listen('mouseleave')
onMouseEnd() {
if (!this.isMove) return
this.isMove = false
this.handleSelected()
@Listen('touchstart')
onTouchStart() {
this.onSelectStart.emit()
}

@Listen('touchend')
onTouchEnd() {
this.isMove = false
this.handleSelected()
}

componentDidLoad() {
this.handleChange()
}

componentDidUpdate() {
if (!this.isInit) {
this.isInit = true
const childList = this.el.childNodes
let idx = 0
let sum = 0
for (const index in childList) {
const item = childList[index] as HTMLElement
if (this.initialPosition === index || !item || typeof item.offsetHeight !== 'number') {
break
}
sum += item.offsetHeight
idx++
}
this.el.scrollTo({ top: sum })
if (idx >= childList.length) {
this.onChange.emit({
curIndex: this.col,
selectedIndex: idx - 1
})
this.handleChange()
}

handleChange() {
const childList = this.el.childNodes
let idx = 0
let sum = 0
for (const index in childList) {
const item = childList[index] as HTMLElement
if (this.initialPosition === index || !item || typeof item.offsetHeight !== 'number') {
break
}
sum += item.offsetHeight
idx++
}
this.el.scrollTo({ top: sum })
if (idx >= childList.length) {
this.onChange.emit({
curIndex: this.col,
selectedIndex: idx - 1
})
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, h, Host, Element, Prop, Event, EventEmitter, Listen } from '@stencil/core'
import { Component, h, Host, Element, Prop, Event, EventEmitter, Listen, Watch } from '@stencil/core'
import classNames from 'classnames'

import { convertStyle } from '../../utils'
Expand Down Expand Up @@ -40,6 +40,11 @@ export class PickerView {
})
onPickEnd: EventEmitter

@Watch('value')
onPropsChange() {
this.handleValueChange()
}

@Listen('onselect')
onSelect(e: CustomEvent<{ curIndex: string; selectedIndex: string }>) {
e.stopPropagation()
Expand All @@ -65,12 +70,16 @@ export class PickerView {
}

componentDidLoad() {
this.handleValueChange()
}

handleValueChange() {
const childList = this.el.querySelectorAll('taro-picker-view-column-core')
childList.forEach((element, index) => {
element.setAttribute('col', `${index}`)
let selectIndex = '0'
let selectIndex = 0
if (!!this.value && this.value.length > index) {
selectIndex = `${this.value[index]}`
selectIndex = this.value[index]
}
const pickerHeight = this.el.getBoundingClientRect().height
const indicatorHeight = this.indicator?.offsetHeight || 0
Expand Down