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

feat: 支持滚动到当前选择的日期 #1145

Merged
merged 2 commits into from
Apr 8, 2024
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
13 changes: 11 additions & 2 deletions compiled/alipay/demo/pages/Calendar/index.axml
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,15 @@
<collapse-container
a:if="{{ demo9.visible }}"
title="受控模式">
<view slot="content">
<view
slot="content"
style="height: 1000rpx">
<ant-calendar
value="{{ demo9.value }}"
monthRange="{{ demo9.monthRange }}"
onChange="demo9HandleChange"
selectionMode="single" />
selectionMode="single"
changedScrollIntoView />
<ant-button
type="primary"
onTap="demo9HandlePreviousDay">
Expand All @@ -120,5 +123,11 @@
onTap="demo9HandleNextDay">
下一天
</ant-button>
<ant-button
style="margin-top: 8rpx"
type="primary"
onTap="demo9HandleInit">
回到初始化日期
</ant-button>
</view>
</collapse-container>
6 changes: 5 additions & 1 deletion compiled/alipay/demo/pages/Calendar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ Page({
demo9: {
visible: true,
value: Date.now(),
monthRange: [new Date().getTime(), new Date().getTime()],
},
},
demo3NextMonth() {
Expand Down Expand Up @@ -112,4 +111,9 @@ Page({
'demo9.value': this.data.demo9.value + 1000 * 24 * 3600,
});
},
demo9HandleInit() {
this.setData({
'demo9.value': Date.now(),
});
},
});
17 changes: 14 additions & 3 deletions compiled/alipay/src/Calendar/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ const Calendar = (props: ICalendarProps) => {

const [scrollIntoViewId, setScrollIntoViewId] = useState<string>('');

// scroll 触发滚动之后需要重置 scrollIntoViewId
function updateScrollIntoViewId(id) {
setScrollIntoViewId(id);

const timer = setTimeout(() => {
setScrollIntoViewId('');
clearTimeout(timer);
});
}

const selectionModeFromValue = getSelectionModeFromValue(value);
const selectionMode =
props.selectionMode ?? selectionModeFromValue ?? 'range';
Expand Down Expand Up @@ -183,18 +193,18 @@ const Calendar = (props: ICalendarProps) => {
useEffect(() => {
// 滚动到已选的位置
props.changedScrollIntoView &&
setScrollIntoViewId(getScrollIntoViewId(value));
updateScrollIntoViewId(getScrollIntoViewId(value));
}, [value]);

useReady(() => {
measurement();
// 初始化默认值时,滚动到选中位置
const isControl = hasValue(props.value);
if (isControl) {
setScrollIntoViewId(getScrollIntoViewId(props.value));
updateScrollIntoViewId(getScrollIntoViewId(props.value));
} else {
props.defaultValue &&
setScrollIntoViewId(getScrollIntoViewId(props.defaultValue));
updateScrollIntoViewId(getScrollIntoViewId(props.defaultValue));
}
}, []);

Expand Down Expand Up @@ -223,4 +233,5 @@ mountComponent<ICalendarProps>(Calendar, {
weekStartsOn: 'Sunday',
localeText: defaultLocaleText,
onFormatter: null,
changedScrollIntoView: null,
});
6 changes: 5 additions & 1 deletion compiled/wechat/demo/pages/Calendar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ Page({
demo9: {
visible: true,
value: Date.now(),
monthRange: [new Date().getTime(), new Date().getTime()],
},
},
demo3NextMonth() {
Expand Down Expand Up @@ -114,4 +113,9 @@ Page({
'demo9.value': this.data.demo9.value + 1000 * 24 * 3600,
});
},
demo9HandleInit() {
this.setData({
'demo9.value': Date.now(),
});
},
});
13 changes: 11 additions & 2 deletions compiled/wechat/demo/pages/Calendar/index.wxml
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,15 @@
<collapse-container
wx:if="{{ demo9.visible }}"
title="受控模式">
<view slot="content">
<view
slot="content"
style="height: 1000rpx">
<ant-calendar
value="{{ demo9.value }}"
monthRange="{{ demo9.monthRange }}"
bind:change="demo9HandleChange"
selectionMode="single" />
selectionMode="single"
changedScrollIntoView />
<ant-button
type="primary"
bind:tap="demo9HandlePreviousDay">
Expand All @@ -106,5 +109,11 @@
bind:tap="demo9HandleNextDay">
下一天
</ant-button>
<ant-button
style="margin-top: 8rpx"
type="primary"
bind:tap="demo9HandleInit">
回到初始化日期
</ant-button>
</view>
</collapse-container>
15 changes: 12 additions & 3 deletions compiled/wechat/src/Calendar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ var Calendar = function (props) {
value: props.value,
}), value = _c[0], setValue = _c[1];
var _d = useState(''), scrollIntoViewId = _d[0], setScrollIntoViewId = _d[1];
// scroll 触发滚动之后需要重置 scrollIntoViewId
function updateScrollIntoViewId(id) {
setScrollIntoViewId(id);
var timer = setTimeout(function () {
setScrollIntoViewId('');
clearTimeout(timer);
});
}
var selectionModeFromValue = getSelectionModeFromValue(value);
var selectionMode = (_b = (_a = props.selectionMode) !== null && _a !== void 0 ? _a : selectionModeFromValue) !== null && _b !== void 0 ? _b : 'range';
var triggerEvent = useComponentEvent(props).triggerEvent;
Expand Down Expand Up @@ -158,18 +166,18 @@ var Calendar = function (props) {
useEffect(function () {
// 滚动到已选的位置
props.changedScrollIntoView &&
setScrollIntoViewId(getScrollIntoViewId(value));
updateScrollIntoViewId(getScrollIntoViewId(value));
}, [value]);
useReady(function () {
measurement();
// 初始化默认值时,滚动到选中位置
var isControl = hasValue(props.value);
if (isControl) {
setScrollIntoViewId(getScrollIntoViewId(props.value));
updateScrollIntoViewId(getScrollIntoViewId(props.value));
}
else {
props.defaultValue &&
setScrollIntoViewId(getScrollIntoViewId(props.defaultValue));
updateScrollIntoViewId(getScrollIntoViewId(props.defaultValue));
}
}, []);
useEvent('measurement', function () {
Expand All @@ -195,4 +203,5 @@ mountComponent(Calendar, {
weekStartsOn: 'Sunday',
localeText: defaultLocaleText,
onFormatter: null,
changedScrollIntoView: null,
});
10 changes: 9 additions & 1 deletion demo/pages/Calendar/index.axml.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,13 @@ export default ({
{/* 受控模式 */}
{demo9.visible && (
<CollapseContainer title="受控模式">
<View slot="content">
<View slot="content" style="height: 1000rpx">
<AntCalendar
value={demo9.value}
monthRange={demo9.monthRange}
onChange="demo9HandleChange"
selectionMode="single"
changedScrollIntoView
/>
<AntButton type="primary" onTap="demo9HandlePreviousDay">
上一天
Expand All @@ -136,6 +137,13 @@ export default ({
>
下一天
</AntButton>
<AntButton
style="margin-top:8rpx"
type="primary"
onTap="demo9HandleInit"
>
回到初始化日期
</AntButton>
</View>
</CollapseContainer>
)}
Expand Down
6 changes: 5 additions & 1 deletion demo/pages/Calendar/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ Page({
demo9: {
visible: true,
value: Date.now(),
monthRange: [new Date().getTime(), new Date().getTime()],
},
},
demo3NextMonth() {
Expand Down Expand Up @@ -133,4 +132,9 @@ Page({
'demo9.value': this.data.demo9.value + 1000 * 24 * 3600,
});
},
demo9HandleInit() {
this.setData({
'demo9.value': Date.now(),
});
},
});
17 changes: 14 additions & 3 deletions src/Calendar/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ const Calendar = (props: ICalendarProps) => {

const [scrollIntoViewId, setScrollIntoViewId] = useState<string>('');

// scroll 触发滚动之后需要重置 scrollIntoViewId
function updateScrollIntoViewId(id) {
setScrollIntoViewId(id);

const timer = setTimeout(() => {
setScrollIntoViewId('');
clearTimeout(timer);
});
}

const selectionModeFromValue = getSelectionModeFromValue(value);
const selectionMode =
props.selectionMode ?? selectionModeFromValue ?? 'range';
Expand Down Expand Up @@ -183,18 +193,18 @@ const Calendar = (props: ICalendarProps) => {
useEffect(() => {
// 滚动到已选的位置
props.changedScrollIntoView &&
setScrollIntoViewId(getScrollIntoViewId(value));
updateScrollIntoViewId(getScrollIntoViewId(value));
}, [value]);

useReady(() => {
measurement();
// 初始化默认值时,滚动到选中位置
const isControl = hasValue(props.value);
if (isControl) {
setScrollIntoViewId(getScrollIntoViewId(props.value));
updateScrollIntoViewId(getScrollIntoViewId(props.value));
} else {
props.defaultValue &&
setScrollIntoViewId(getScrollIntoViewId(props.defaultValue));
updateScrollIntoViewId(getScrollIntoViewId(props.defaultValue));
}
}, []);

Expand Down Expand Up @@ -223,4 +233,5 @@ mountComponent<ICalendarProps>(Calendar, {
weekStartsOn: 'Sunday',
localeText: defaultLocaleText,
onFormatter: null,
changedScrollIntoView: null,
});
9 changes: 1 addition & 8 deletions tests/alipay/Calendar/__tests__/calendar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,15 +314,8 @@ describe('Calendar', () => {
extendFunctions.clickCell(
dayjs(defaultValue).add(1, 'd').format('YYYY-MM-DD')
);
await sleep(300);
expect(instance.getData().scrollIntoViewId).toEqual(
getScrollIntoViewId(dayjs(defaultValue).add(1, 'd').toDate().getTime())
);

extendFunctions.clickCell('2024-03-28');
await sleep(300);
expect(instance.getData().scrollIntoViewId).toEqual(
getScrollIntoViewId(dayjs('2024-03-28').toDate().getTime())
);
expect(instance.getData().scrollIntoViewId).toEqual('');
});
});
Loading