Skip to content

Commit

Permalink
feat(RangePicker): change logic
Browse files Browse the repository at this point in the history
  • Loading branch information
wyj committed Jul 26, 2022
1 parent 7c157f7 commit cc1e95d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 28 deletions.
9 changes: 4 additions & 5 deletions src/DatePicker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ Component({
const min = this.getMin();
const max = this.getMax();
if (max < min) {
this.setData({ columns: [] });
return;
return [];
}
let currentPickerDay = dayjs();
if (currentValue.length > 0) {
Expand Down Expand Up @@ -146,10 +145,10 @@ Component({
date = max.toDate();
selectedIndex = getValueByDate(date, precision);
}
const newClolumns = this.generateData(selectedIndex);
if (!equal(newClolumns, this.data.columns)) {
const newColumns = this.generateData(selectedIndex);
if (!equal(newColumns, this.data.columns)) {
this.setData({
columns: newClolumns
columns: newColumns
}, () => {
this.setData({ currentValue: selectedIndex });
if (onPickerChange) {
Expand Down
4 changes: 0 additions & 4 deletions src/Picker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ Component({
onChange(e) {
const { onChange } = this.props;
const { value: selectedIndex } = e.detail;
console.log('picker onChange selectedIndex', selectedIndex)
this.tempSelectedIndex = selectedIndex;
this.isChangingPickerView = true;
const { matchedColumn, matchedValues } = getMatchedItemByIndex(
Expand All @@ -165,7 +164,6 @@ Component({
async onOk() {
let result;
if (this.tempSelectedIndex) {
console.log('picker onOK columns', this.data.columns)
result = getMatchedItemByIndex(
this.data.columns,
this.tempSelectedIndex,
Expand All @@ -189,8 +187,6 @@ Component({
return
}
}
console.log('picker onOK tempSelectedIndex', this.tempSelectedIndex)
console.log('picker onOk matchedValues', matchedValues)
this.setData({
cValue: matchedValues,
});
Expand Down
59 changes: 40 additions & 19 deletions src/RangePicker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ Component({
setCurrentValue() {
const { _visible } = this; // 隐藏状态下从CValue触发,展开状态使用当前数据
const { precision } = this.props;
const { cValue, pickerType } = this.data;
const { cValue, pickerType, columns } = this.data;
let { currentStartDate, currentEndDate } = this.data;
const currentStartDateByCValue = cValue?.[0] || null;
const currentEndDateByCValue = cValue?.[1] || null;
Expand Down Expand Up @@ -176,20 +176,24 @@ Component({
pickerType === 'start' ? currentStartDate : currentEndDate,
precision
);
this.setData({ currentStartDate, currentEndDate, currentValue });
this.generateData();
const newColumns = this.generateData(currentValue);
if (!equal(newColumns, columns)) {
this.setData({ columns: newColumns }, () => {
this.setData({ currentStartDate, currentEndDate, currentValue });
});
} else {
this.setData({ currentStartDate, currentEndDate, currentValue });
}
},
/**
* 生成选项数据,didmound、picker change、打开弹窗、切换picker type触发
*/
generateData() {
generateData(currentValue) {
const { precision } = this.props;
const { columns, currentValue } = this.data;
const min = this.getMin();
const max = this.getMax();
if (max < min) {
this.setData({ columns: [] });
return;
return [];
}
let currentPickerDay = dayjs();
if (currentValue.length > 0) {
Expand All @@ -200,9 +204,7 @@ Component({
}

const newColumns = getRangeData(precision, min, max, currentPickerDay);
if (!equal(columns, newColumns)) {
this.setData({ columns: newColumns });
}
return newColumns;
},

onChange(selectedIndex) {
Expand All @@ -219,7 +221,7 @@ Component({
date = max.toDate();
selectedIndex = getValueByDate(date, precision);
}
const { pickerType } = this.data;
const { pickerType, columns } = this.data;
const newData: any = {
currentValue: selectedIndex,
};
Expand All @@ -228,15 +230,34 @@ Component({
} else {
newData.currentEndDate = date;
}
this.setData(newData);
this.generateData();
if (onPickerChange) {
onPickerChange(
pickerType,
date,
dayjs(date).format(format),
selectedIndex
const newColumns = this.generateData(selectedIndex);
if (!equal(newColumns, columns)) {
this.setData(
{
columns: newColumns,
},
() => {
this.setData(newData);
if (onPickerChange) {
onPickerChange(
pickerType,
date,
dayjs(date).format(format),
selectedIndex
);
}
}
);
} else {
this.setData(newData);
if (onPickerChange) {
onPickerChange(
pickerType,
date,
dayjs(date).format(format),
selectedIndex
);
}
}
},

Expand Down

0 comments on commit cc1e95d

Please sign in to comment.