@@ -3,9 +3,11 @@ import {
3
3
ExtractPropTypes ,
4
4
nextTick ,
5
5
PropType ,
6
+ reactive ,
6
7
ref ,
7
8
watch ,
8
9
} from 'vue' ;
10
+ import { useRect } from './hooks' ;
9
11
import { CalendarDateType } from './types' ;
10
12
import {
11
13
checkPlatform ,
@@ -38,8 +40,8 @@ export default defineComponent({
38
40
emits : [ 'change' ] ,
39
41
40
42
setup ( props , { emit } ) {
41
- const hashID = ref < string [ ] > ( [ ] ) ;
42
- const hashClass = ref ( '' ) ;
43
+ const timeContentRef = reactive < HTMLElement [ ] > ( [ ] ) ;
44
+ const timeItemRef = reactive < HTMLElement [ ] > ( [ ] ) ;
43
45
const checkedDate = ref ( {
44
46
hours : new Date ( ) . getHours ( ) ,
45
47
minutes : new Date ( ) . getMinutes ( ) ,
@@ -49,12 +51,6 @@ export default defineComponent({
49
51
const timeStartY = ref ( 0 ) ;
50
52
const timeStartUp = ref ( 0 ) ;
51
53
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
-
58
54
// 初始化时间选择器数据
59
55
const initTimeArray = ( ) => {
60
56
const hours : number [ ] = [ ] ;
@@ -74,29 +70,19 @@ export default defineComponent({
74
70
const checkHours = checkedDate . value . hours ;
75
71
const checkMinutes = checkedDate . value . minutes ;
76
72
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 ) ;
84
75
76
+ const [ timeContentFirst , timeContentSecond ] = timeContentRef ;
85
77
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)' ;
91
81
}
92
-
93
82
const minutesUp =
94
83
( 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 =
100
86
'translate3d(0px,' + minutesUp + 'px,0px)' ;
101
87
}
102
88
} ) ;
@@ -118,9 +104,7 @@ export default defineComponent({
118
104
timeStartY . value = e . changedTouches [ 0 ] . pageY ;
119
105
const { transform } = ( e . currentTarget as HTMLElement ) . style ;
120
106
if ( transform ) {
121
- timeStartUp . value = parseFloat (
122
- transform ?. split ( ' ' ) [ 1 ] ?. split ( 'px' ) [ 0 ]
123
- ) ;
107
+ timeStartUp . value = parseFloat ( transform . split ( ' ' ) [ 1 ] . split ( 'px' ) [ 0 ] ) ;
124
108
}
125
109
} ;
126
110
@@ -130,8 +114,8 @@ export default defineComponent({
130
114
if ( transform ) {
131
115
endUp = parseFloat (
132
116
( e . currentTarget as HTMLElement ) . style . transform
133
- ? .split ( ' ' ) [ 1 ]
134
- ? .split ( 'px' ) [ 0 ]
117
+ . split ( ' ' ) [ 1 ]
118
+ . split ( 'px' ) [ 0 ]
135
119
) ;
136
120
}
137
121
@@ -251,11 +235,13 @@ export default defineComponent({
251
235
const renderTimeItem = ( time : number [ ] , index : number ) =>
252
236
time . map ( ( item , j ) => (
253
237
< div
238
+ ref = { ( ref ) => {
239
+ timeItemRef . length = 0 ;
240
+ timeItemRef . push ( ref as HTMLElement ) ;
241
+ } }
254
242
class = { `time_item ${
255
243
isBeSelectedTime ( item , index ) ? 'time_item_show' : ''
256
- } ${ hashClass . value } ${
257
- formatDisabledDate ( item , index ) ? 'time-disabled' : ''
258
- } `}
244
+ } ${ formatDisabledDate ( item , index ) ? 'time-disabled' : '' } `}
259
245
key = { index + j }
260
246
>
261
247
{ fillNumber ( item ) }
@@ -268,7 +254,9 @@ export default defineComponent({
268
254
{ timeArray . value . map ( ( item , index ) => (
269
255
< div
270
256
class = "time_content"
271
- id = { hashID . value [ index ] }
257
+ ref = { ( ref ) => {
258
+ timeContentRef . push ( ref as HTMLElement ) ;
259
+ } }
272
260
key = { index }
273
261
onTouchstart = { timeTouchStart }
274
262
onTouchmove = { ( e ) => timeTouchMove ( e , index ) }
0 commit comments