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(TimePicker): 修复边界滚动异常问题 #1426

Merged
merged 7 commits into from
Sep 3, 2022
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 @@ -2563,6 +2563,7 @@ exports[`panel.jsx 1`] = `
</div>
<ul
class="t-time-picker__panel-body-scroll"
style="--timePickerPanelOffsetTop: 0; --timePickerPanelOffsetBottom: 0;"
>
<li
class="t-time-picker__panel-body-scroll-item"
Expand Down Expand Up @@ -2687,6 +2688,7 @@ exports[`panel.jsx 1`] = `
</ul>
<ul
class="t-time-picker__panel-body-scroll"
style="--timePickerPanelOffsetTop: 0; --timePickerPanelOffsetBottom: 0;"
>
<li
class="t-time-picker__panel-body-scroll-item t-is-current"
Expand Down Expand Up @@ -2991,6 +2993,7 @@ exports[`panel.jsx 1`] = `
</ul>
<ul
class="t-time-picker__panel-body-scroll"
style="--timePickerPanelOffsetTop: 0; --timePickerPanelOffsetBottom: 0;"
>
<li
class="t-time-picker__panel-body-scroll-item t-is-current"
Expand Down Expand Up @@ -5184,6 +5187,7 @@ exports[`panel.jsx 1`] = `
</div>
<ul
class="t-time-picker__panel-body-scroll"
style="--timePickerPanelOffsetTop: 0; --timePickerPanelOffsetBottom: 0;"
>
<li
class="t-time-picker__panel-body-scroll-item t-is-current"
Expand Down Expand Up @@ -5308,6 +5312,7 @@ exports[`panel.jsx 1`] = `
</ul>
<ul
class="t-time-picker__panel-body-scroll"
style="--timePickerPanelOffsetTop: 0; --timePickerPanelOffsetBottom: 0;"
>
<li
class="t-time-picker__panel-body-scroll-item t-is-current"
Expand Down Expand Up @@ -5612,6 +5617,7 @@ exports[`panel.jsx 1`] = `
</ul>
<ul
class="t-time-picker__panel-body-scroll"
style="--timePickerPanelOffsetTop: 0; --timePickerPanelOffsetBottom: 0;"
>
<li
class="t-time-picker__panel-body-scroll-item t-is-current"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@ exports[`panel.jsx 1`] = `
</div>
<ul
class="t-time-picker__panel-body-scroll"
style="--timePickerPanelOffsetTop: 0; --timePickerPanelOffsetBottom: 0;"
>
<li
class="t-time-picker__panel-body-scroll-item"
Expand Down Expand Up @@ -642,6 +643,7 @@ exports[`panel.jsx 1`] = `
</ul>
<ul
class="t-time-picker__panel-body-scroll"
style="--timePickerPanelOffsetTop: 0; --timePickerPanelOffsetBottom: 0;"
>
<li
class="t-time-picker__panel-body-scroll-item t-is-current"
Expand Down Expand Up @@ -946,6 +948,7 @@ exports[`panel.jsx 1`] = `
</ul>
<ul
class="t-time-picker__panel-body-scroll"
style="--timePickerPanelOffsetTop: 0; --timePickerPanelOffsetBottom: 0;"
>
<li
class="t-time-picker__panel-body-scroll-item t-is-current"
Expand Down
70 changes: 50 additions & 20 deletions src/time-picker/panel/SinglePanel.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FC, useCallback, useEffect, useState, useRef, useMemo } from 'react';
import React, { FC, useCallback, useEffect, useState, useRef, useMemo, CSSProperties } from 'react';
import classNames from 'classnames';
import dayjs from 'dayjs';
import customParseFormat from 'dayjs/plugin/customParseFormat';
Expand All @@ -20,6 +20,7 @@ import {
import { closestLookup } from '../../_common/js/time-picker/utils';

import { TdTimePickerProps, TimeRangePickerPartial } from '../type';
import useIsomorphicLayoutEffect from '../../_util/useLayoutEffect';

const timeArr = [EPickerCols.hour, EPickerCols.minute, EPickerCols.second, EPickerCols.milliSecond];

Expand Down Expand Up @@ -93,10 +94,31 @@ const SinglePanel: FC<SinglePanelProps> = (props) => {
// 获取每个时间的高度
const getItemHeight = useCallback(() => {
const maskDom = maskRef?.current?.querySelector('div');
const timeItemTotalHeight = maskDom.offsetHeight + parseInt(getComputedStyle(maskDom).marginTop, 10);
return timeItemTotalHeight;
if (!maskDom) {
return {
offsetHeight: 0,
margin: 0,
};
}
return {
offsetHeight: maskDom.offsetHeight,
margin: parseInt(getComputedStyle(maskDom).marginTop, 10),
};
}, []);

const panelOffset = useRef({
top: 0,
bottom: 0,
});

useIsomorphicLayoutEffect(() => {
const { offsetHeight, margin } = getItemHeight();
panelOffset.current = {
top: offsetHeight * 0.5 + margin * 0.5,
bottom: offsetHeight * 0.5 + margin * 1.5,
};
}, [cols]);

const timeItemCanUsed = useCallback(
(col: EPickerCols, el: string | number) => {
const colIdx = timeArr.indexOf(col);
Expand Down Expand Up @@ -149,8 +171,9 @@ const SinglePanel: FC<SinglePanelProps> = (props) => {
(time as number) %= 12; // 一定是数字,直接 cast

const itemIdx = getColList(col).indexOf(padStart(String(time), 2, '0'));
const timeItemTotalHeight = getItemHeight();
const distance = Math.abs(itemIdx * timeItemTotalHeight + timeItemTotalHeight / 2);
const { offsetHeight, margin } = getItemHeight();
const timeItemTotalHeight = offsetHeight + margin;
const distance = Math.abs(itemIdx * timeItemTotalHeight);
return distance;
},
[getItemHeight, getColList, format],
Expand All @@ -160,8 +183,9 @@ const SinglePanel: FC<SinglePanelProps> = (props) => {
let val: number | string;
let formattedVal: string;
const scrollTop = colsRef.current[idx]?.scrollTop;

let colStep = Math.abs(Math.round(scrollTop / getItemHeight() + 0.5));
const { offsetHeight, margin } = getItemHeight();
const timeItemTotalHeight = offsetHeight + margin;
let colStep = Math.abs(Math.round(scrollTop / timeItemTotalHeight + 0.5));
const meridiem = MERIDIEM_LIST[Math.min(colStep - 1, 1)].toLowerCase(); // 处理PM、AM与am、pm

if (Number.isNaN(colStep)) colStep = 1;
Expand Down Expand Up @@ -192,21 +216,21 @@ const SinglePanel: FC<SinglePanelProps> = (props) => {
const distance = getScrollDistance(col, val);

if (!dayjs(dayjsValue).isValid()) return;
if (distance !== scrollTop) {
if (timeArr.includes(col)) {
if (timeItemCanUsed(col, val)) formattedVal = dayjsValue[col]?.(val).format(format);
} else {
const currentHour = dayjsValue.hour();
if (meridiem === AM && currentHour >= 12) {
formattedVal = dayjsValue.hour(currentHour - 12).format(format);
} else if (meridiem === PM && currentHour < 12) {
formattedVal = dayjsValue.hour(currentHour + 12).format(format);
}
if (timeArr.includes(col)) {
if (timeItemCanUsed(col, val)) formattedVal = dayjsValue[col]?.(val).format(format);
} else {
const currentHour = dayjsValue.hour();
if (meridiem === AM && currentHour >= 12) {
formattedVal = dayjsValue.hour(currentHour - 12).format(format);
} else if (meridiem === PM && currentHour < 12) {
formattedVal = dayjsValue.hour(currentHour + 12).format(format);
}
onChange(formattedVal);
const scrollCtrl = colsRef.current[cols.indexOf(col)];
if (!distance || !scrollCtrl || scrollCtrl.scrollTop === distance) return;
}
onChange(formattedVal);
const scrollCtrl = colsRef.current[cols.indexOf(col)];
if (!distance || !scrollCtrl || scrollCtrl.scrollTop === distance) return;

if (distance !== scrollTop) {
scrollCtrl.scrollTo?.({
top: distance,
behavior: 'smooth',
Expand Down Expand Up @@ -302,6 +326,12 @@ const SinglePanel: FC<SinglePanelProps> = (props) => {
ref={(el) => (colsRef.current[idx] = el)}
className={`${panelClassName}-body-scroll`}
onScroll={debounce(() => handleScroll(col, idx), 50)}
style={
{
'--timePickerPanelOffsetTop': panelOffset.current.top,
'--timePickerPanelOffsetBottom': panelOffset.current.bottom,
} as CSSProperties
}
>
{getColList(col).map((el) => (
<li
Expand Down