Skip to content

Commit d8ab65e

Browse files
authored
feat(elevator): 采用唯一ID,避免未传入 className 导致的报错 (#2834)
1 parent 161733d commit d8ab65e

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/packages/elevator/elevator.taro.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import React, {
2+
createContext,
23
FunctionComponent,
3-
useRef,
44
useEffect,
5+
useRef,
56
useState,
6-
createContext,
77
} from 'react'
8-
import Taro, { nextTick, createSelectorQuery } from '@tarojs/taro'
8+
import Taro, { createSelectorQuery, nextTick } from '@tarojs/taro'
99

1010
import { ScrollView } from '@tarojs/components'
1111
import classNames from 'classnames'
1212
import { BasicComponent, ComponentDefaults } from '@/utils/typings'
13+
import useUuid from '@/utils/use-uuid'
1314

1415
export const elevatorContext = createContext({} as ElevatorData)
1516

@@ -65,6 +66,7 @@ export const Elevator: FunctionComponent<
6566
...defaultProps,
6667
...props,
6768
}
69+
const uuid = useUuid()
6870
const classPrefix = 'nut-elevator'
6971
const listview = useRef<HTMLDivElement>(null)
7072
const initData = {
@@ -106,7 +108,7 @@ export const Elevator: FunctionComponent<
106108
for (let i = 0; i < state.current.listGroup.length; i++) {
107109
const query = createSelectorQuery()
108110
query
109-
.selectAll(`.${className} .nut-elevator-item-${i}`)
111+
.selectAll(`.${classPrefix}-${uuid} .nut-elevator-item-${i}`)
110112
.boundingClientRect()
111113
// eslint-disable-next-line no-loop-func
112114
query.exec((res: any) => {
@@ -180,7 +182,7 @@ export const Elevator: FunctionComponent<
180182
const setListGroup = () => {
181183
if (listview.current) {
182184
createSelectorQuery()
183-
.selectAll(`.${className} .nut-elevator-list-item`)
185+
.selectAll(`.${classPrefix}-${uuid} .nut-elevator-list-item`)
184186
.node((el) => {
185187
state.current.listGroup = [...Object.keys(el)]
186188
calculateHeight()
@@ -220,7 +222,11 @@ export const Elevator: FunctionComponent<
220222
}, [listview])
221223

222224
return (
223-
<div className={`${classPrefix} ${className}`} style={style} {...rest}>
225+
<div
226+
className={`${classPrefix} ${className} ${classPrefix}-${uuid}`}
227+
style={style}
228+
{...rest}
229+
>
224230
<div
225231
className={`${classPrefix}-list`}
226232
style={{ height: Number.isNaN(+height) ? height : `${height}px` }}

0 commit comments

Comments
 (0)