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: 修复页面 Picker 无变更的渲染导致选择项重置 #261

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion src/Picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class Picker extends React.Component<IPickerProp & IPickerProps, any> {
indicatorRef: any;
itemHeight: number;
scrollValue: any;
// 在拖动或者动画滚动过程中,不响应 props 更新
isMovingOrScrolling = false;

scrollHanders = (() => {
let scrollY = -1;
Expand Down Expand Up @@ -112,6 +114,7 @@ class Picker extends React.Component<IPickerProp & IPickerProps, any> {
}

isMoving = true;
this.isMovingOrScrolling = true;
startY = y;
lastY = scrollY;
};
Expand Down Expand Up @@ -243,7 +246,9 @@ class Picker extends React.Component<IPickerProp & IPickerProps, any> {
}

componentDidUpdate() {
this.props.select(this.state.selectedValue, this.itemHeight, this.scrollToWithoutAnimation);
if (!this.isMovingOrScrolling) {
this.props.select(this.state.selectedValue, this.itemHeight, this.scrollToWithoutAnimation);
}
}

scrollTo = (top) => {
Expand Down Expand Up @@ -285,6 +290,7 @@ class Picker extends React.Component<IPickerProp & IPickerProps, any> {
}

scrollingComplete = () => {
this.isMovingOrScrolling = false;
const top = this.scrollHanders.getValue();
if (top >= 0) {
this.props.doScrollingComplete(top, this.itemHeight, this.fireValueChange);
Expand Down
16 changes: 13 additions & 3 deletions src/PopupMixin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,19 @@ export default function PopupMixin(getModal, platformProps) {

componentWillReceiveProps(nextProps) {
if ('value' in nextProps) {
this.setState({
pickerValue: nextProps.value,
});
// value 有实际的变化才更新
if (!Array.isArray(nextProps.value) || !Array.isArray(this.props.value)) {
// 不是数组的话,跟原来的逻辑一样,实际上 Picker 不会走到这个逻辑,因为 Picker 在使用的时候必须传数组
this.setState({
pickerValue: nextProps.value,
});
} else if (nextProps.value.length !== this.props.value.length || nextProps.value.some(
(item, index) => item !== this.props.value[index]
)) {
this.setState({
pickerValue: nextProps.value,
});
}
}
if ('visible' in nextProps) {
this.setVisibleState(nextProps.visible);
Expand Down