Skip to content

Commit 5cf5b05

Browse files
committed
feat: CalendarTime 使用 ref 替代随机ID和类名
1 parent 0cff186 commit 5cf5b05

File tree

3 files changed

+195
-207
lines changed

3 files changed

+195
-207
lines changed

src/calendar/CalendarTime.tsx

+23-35
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import {
33
ExtractPropTypes,
44
nextTick,
55
PropType,
6+
reactive,
67
ref,
78
watch,
89
} from 'vue';
10+
import { useRect } from './hooks';
911
import { CalendarDateType } from './types';
1012
import {
1113
checkPlatform,
@@ -38,8 +40,8 @@ export default defineComponent({
3840
emits: ['change'],
3941

4042
setup(props, { emit }) {
41-
const hashID = ref<string[]>([]);
42-
const hashClass = ref('');
43+
const timeContentRef = reactive<HTMLElement[]>([]);
44+
const timeItemRef = reactive<HTMLElement[]>([]);
4345
const checkedDate = ref({
4446
hours: new Date().getHours(),
4547
minutes: new Date().getMinutes(),
@@ -49,12 +51,6 @@ export default defineComponent({
4951
const timeStartY = ref(0);
5052
const timeStartUp = ref(0);
5153

52-
hashID.value = [
53-
`time${Math.floor(Math.random() * 1000000)}`,
54-
`time${Math.floor(Math.random() * 1000000)}`,
55-
];
56-
hashClass.value = `time_item_${Math.floor(Math.random() * 1000000)}`;
57-
5854
// 初始化时间选择器数据
5955
const initTimeArray = () => {
6056
const hours: number[] = [];
@@ -74,29 +70,19 @@ export default defineComponent({
7470
const checkHours = checkedDate.value.hours;
7571
const checkMinutes = checkedDate.value.minutes;
7672

77-
const hashClassEle = document.querySelector(
78-
`.${hashClass.value}`
79-
) as HTMLElement;
80-
const timeHeightStr = hashClassEle
81-
? hashClassEle.getBoundingClientRect().height
82-
: 0;
83-
timeHeight.value = timeHeightStr;
73+
timeHeight.value = useRect(timeItemRef[0]).height;
74+
console.log('timeHeight.value', timeHeight.value);
8475

76+
const [timeContentFirst, timeContentSecond] = timeContentRef;
8577
const hoursUp = (2 - checkHours) * timeHeight.value;
86-
const hashIDEle = document.querySelector(
87-
`#${hashID.value[0]}`
88-
) as HTMLElement;
89-
if (hashIDEle) {
90-
hashIDEle.style.transform = 'translate3d(0px,' + hoursUp + 'px,0px)';
78+
if (timeContentFirst) {
79+
timeContentFirst.style.transform =
80+
'translate3d(0px,' + hoursUp + 'px,0px)';
9181
}
92-
9382
const minutesUp =
9483
(2 - checkMinutes / props.minuteStep) * timeHeight.value;
95-
const hashIDEle1 = document.querySelector(
96-
`#${hashID.value[1]}`
97-
) as HTMLElement;
98-
if (hashIDEle1) {
99-
hashIDEle1.style.transform =
84+
if (timeContentSecond) {
85+
timeContentSecond.style.transform =
10086
'translate3d(0px,' + minutesUp + 'px,0px)';
10187
}
10288
});
@@ -118,9 +104,7 @@ export default defineComponent({
118104
timeStartY.value = e.changedTouches[0].pageY;
119105
const { transform } = (e.currentTarget as HTMLElement).style;
120106
if (transform) {
121-
timeStartUp.value = parseFloat(
122-
transform?.split(' ')[1]?.split('px')[0]
123-
);
107+
timeStartUp.value = parseFloat(transform.split(' ')[1].split('px')[0]);
124108
}
125109
};
126110

@@ -130,8 +114,8 @@ export default defineComponent({
130114
if (transform) {
131115
endUp = parseFloat(
132116
(e.currentTarget as HTMLElement).style.transform
133-
?.split(' ')[1]
134-
?.split('px')[0]
117+
.split(' ')[1]
118+
.split('px')[0]
135119
);
136120
}
137121

@@ -251,11 +235,13 @@ export default defineComponent({
251235
const renderTimeItem = (time: number[], index: number) =>
252236
time.map((item, j) => (
253237
<div
238+
ref={(ref) => {
239+
timeItemRef.length = 0;
240+
timeItemRef.push(ref as HTMLElement);
241+
}}
254242
class={`time_item ${
255243
isBeSelectedTime(item, index) ? 'time_item_show' : ''
256-
} ${hashClass.value} ${
257-
formatDisabledDate(item, index) ? 'time-disabled' : ''
258-
}`}
244+
} ${formatDisabledDate(item, index) ? 'time-disabled' : ''}`}
259245
key={index + j}
260246
>
261247
{fillNumber(item)}
@@ -268,7 +254,9 @@ export default defineComponent({
268254
{timeArray.value.map((item, index) => (
269255
<div
270256
class="time_content"
271-
id={hashID.value[index]}
257+
ref={(ref) => {
258+
timeContentRef.push(ref as HTMLElement);
259+
}}
272260
key={index}
273261
onTouchstart={timeTouchStart}
274262
onTouchmove={(e) => timeTouchMove(e, index)}

0 commit comments

Comments
 (0)