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): boundary scroll #1618

Merged
merged 1 commit into from
Sep 6, 2022
Merged
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
60 changes: 42 additions & 18 deletions src/time-picker/panel/single-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ dayjs.extend(customParseFormat);

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

const panelOffset = {
top: 15,
bottom: 21,
};

export default defineComponent({
name: 'TTimePickerPanelCol',
props: {
Expand Down Expand Up @@ -95,8 +100,16 @@ export default defineComponent({
// 获取每个时间的高度
const getItemHeight = () => {
const maskDom = maskRef.value?.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 timeItemCanUsed = (col: EPickerCols, el: string | number) => {
Expand Down Expand Up @@ -152,17 +165,20 @@ export default defineComponent({
(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(Math.max(0, itemIdx) * timeItemTotalHeight);
return distance;
};

const handleScroll = (col: EPickerCols, idx: number) => {
let val: number | string;
let formattedVal: string;
const scrollTop = colsRef[idx]?.scrollTop;
const { offsetHeight, margin } = getItemHeight();
const timeItemTotalHeight = offsetHeight + margin;
let colStep = Math.abs(Math.round(scrollTop / timeItemTotalHeight + 0.5));

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

if (Number.isNaN(colStep)) colStep = 1;
Expand All @@ -189,23 +205,27 @@ export default defineComponent({
else val = meridiem;

const distance = getScrollDistance(col, val);
if (!dayjs(dayjsValue.value).isValid()) return;
if (distance !== scrollTop) {
if (timeArr.includes(col)) {
if (timeItemCanUsed(col, val)) formattedVal = dayjsValue.value[col]?.(val).format(format.value);
if (!dayjs(dayjsValue.value).isValid() || (value.value && !dayjs(value.value, format.value, true).isValid()))
return;

if (timeArr.includes(col)) {
if (timeItemCanUsed(col, val)) formattedVal = dayjsValue.value[col]?.(val).format(format.value);
} else {
const currentHour = dayjsValue.value.hour();
if (meridiem === AM && currentHour >= 12) {
formattedVal = dayjsValue.value.hour(currentHour - 12).format(format.value);
} else if (meridiem === PM && currentHour < 12) {
formattedVal = dayjsValue.value.hour(currentHour + 12).format(format.value);
} else {
const currentHour = dayjsValue.value.hour();
if (meridiem === AM && currentHour >= 12) {
formattedVal = dayjsValue.value.hour(currentHour - 12).format(format.value);
} else if (meridiem === PM && currentHour < 12) {
formattedVal = dayjsValue.value.hour(currentHour + 12).format(format.value);
}
formattedVal = dayjsValue.value.format(format.value);
}
props.onChange?.(formattedVal);
}

if (formattedVal !== value.value) props.onChange?.(formattedVal);
if (distance !== scrollTop) {
const scrollCtrl = colsRef[cols.value.indexOf(col)];

if (!distance || !scrollCtrl || scrollCtrl.scrollTop === distance) return;
if (!scrollCtrl || scrollCtrl.scrollTop === distance) return;

scrollCtrl.scrollTo?.({
top: distance,
Expand All @@ -222,7 +242,7 @@ export default defineComponent({
) => {
const distance = getScrollDistance(col, time);
const scrollCtrl = colsRef[idx];
if (!distance || !scrollCtrl || scrollCtrl.scrollTop === distance || !timeItemCanUsed(col, time)) return;
if (!scrollCtrl || scrollCtrl.scrollTop === distance || !timeItemCanUsed(col, time)) return;

scrollCtrl.scrollTo?.({
top: distance,
Expand Down Expand Up @@ -308,6 +328,10 @@ export default defineComponent({
ref={(el) => (colsRef[idx] = el)}
class={`${panelClassName.value}-body-scroll`}
onScroll={debounce(() => handleScroll(col, idx), 50)}
style={{
'--timePickerPanelOffsetTop': panelOffset.top,
'--timePickerPanelOffsetBottom': panelOffset.bottom,
}}
>
{getColList(col).map((el) => (
<li
Expand Down