-
-
Notifications
You must be signed in to change notification settings - Fork 338
Expand file tree
/
Copy pathTimeUnitColumn.tsx
More file actions
109 lines (99 loc) · 3.02 KB
/
TimeUnitColumn.tsx
File metadata and controls
109 lines (99 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import * as React from 'react';
import { useRef, useLayoutEffect } from 'react';
import classNames from 'classnames';
import { scrollTo, waitElementReady } from '../../utils/uiUtil';
import PanelContext from '../../PanelContext';
import type { CellRender, Locale } from '../../interface';
export type Unit = {
label: React.ReactText;
value: number;
disabled: boolean;
};
export type TimeUnitColumnProps<DateType> = {
prefixCls?: string;
units?: Unit[];
value?: number;
active?: boolean;
hideDisabledOptions?: boolean;
onSelect?: (value: number) => void;
type: 'hour' | 'minute' | 'second' | 'meridiem';
info: {
today: DateType;
locale: Locale;
cellRender: CellRender<DateType, number>;
};
};
function TimeUnitColumn<DateType>(props: TimeUnitColumnProps<DateType>) {
const { prefixCls, units, onSelect, value, active, hideDisabledOptions, info, type } = props;
const cellPrefixCls = `${prefixCls}-cell`;
const { open } = React.useContext(PanelContext);
const ulRef = useRef<HTMLUListElement>(null);
const liRefs = useRef<Map<number, HTMLElement | null>>(new Map());
const scrollRef = useRef<Function>();
// `useLayoutEffect` here to avoid blink by duration is 0
useLayoutEffect(() => {
const li = liRefs.current.get(value!);
if (li && open !== false && type !== 'meridiem') {
scrollTo(ulRef.current!, li.offsetTop, 120);
}
}, [value]);
useLayoutEffect(() => {
if (open && type !== 'meridiem') {
const li = liRefs.current.get(value!);
if (li) {
scrollRef.current = waitElementReady(li, () => {
scrollTo(ulRef.current!, li.offsetTop, 0);
});
}
}
return () => {
scrollRef.current?.();
};
}, [open]);
return (
<ul
className={classNames(`${prefixCls}-column`, {
[`${prefixCls}-column-active`]: active,
})}
ref={ulRef}
style={{ position: 'relative' }}
>
{units!.map((unit) => {
if (hideDisabledOptions && unit.disabled) {
return null;
}
return (
<li
key={unit.value}
ref={(element) => {
liRefs.current.set(unit.value, element);
}}
className={classNames(cellPrefixCls, {
[`${cellPrefixCls}-disabled`]: unit.disabled,
[`${cellPrefixCls}-selected`]: value === unit.value,
})}
onClick={() => {
if (unit.disabled) {
return;
}
onSelect!(unit.value);
}}
>
{info.cellRender ? (
info.cellRender(unit.value, {
today: info.today,
locale: info.locale,
originNode: <div className={`${cellPrefixCls}-inner`}>{unit.label}</div>,
type: 'time',
subType: type,
})
) : (
<div className={`${cellPrefixCls}-inner`}>{unit.label}</div>
)}
</li>
);
})}
</ul>
);
}
export default TimeUnitColumn;