|
6 | 6 | class="nut-popover-wrapper" |
7 | 7 | @click="openPopover" |
8 | 8 | > |
9 | | - <slot name="reference"></slot |
10 | | - > |
| 9 | + <slot name="reference"></slot> |
11 | 10 | </view> |
12 | 11 | <view :class="['nut-popover', `nut-popover--${theme}`, `${customClass}`]" :style="getRootPosition"> |
13 | 12 | <nut-popup |
14 | 13 | v-model:visible="showPopup" |
15 | 14 | :pop-class="`nut-popover-content nut-popover-content--${location}`" |
16 | 15 | :style="{ background: bgColor }" |
17 | 16 | position="" |
18 | | - transition="nut-popover" |
| 17 | + transition="nut-popover-content" |
19 | 18 | :overlay="overlay" |
20 | 19 | :duration="duration" |
21 | 20 | :overlay-style="overlayStyle" |
|
41 | 40 | </view> |
42 | 41 | </template> |
43 | 42 | <script lang="ts"> |
44 | | -import { onMounted, computed, watch, ref, PropType, CSSProperties } from 'vue' |
| 43 | +import { onMounted, computed, watch, ref, PropType, CSSProperties, nextTick } from 'vue' |
45 | 44 | import { createComponent, renderIcon } from '@/packages/utils/create' |
46 | | -import { useTaroRect, useTaroRectById } from '@/packages/utils/useTaroRect' |
| 45 | +import { rectTaro, useTaroRect, useTaroRectById } from '@/packages/utils/useTaroRect' |
47 | 46 | import { PopoverList, PopoverTheme, PopoverLocation, PopoverRootPosition } from './type' |
48 | 47 | import NutPopup from '../popup/index.taro.vue' |
49 | 48 | import Taro from '@tarojs/taro' |
@@ -131,9 +130,7 @@ export default create({ |
131 | 130 | }) |
132 | 131 |
|
133 | 132 | const upperCaseFirst = (str: string) => { |
134 | | - str = str.toLowerCase() |
135 | | - str = str.replace(/\b\w+\b/g, word => word.substring(0, 1).toUpperCase() + word.substring(1)) |
136 | | - return str |
| 133 | + return str.toLowerCase().replace(/\b\w+\b/g, word => word.substring(0, 1).toUpperCase() + word.substring(1)) |
137 | 134 | } |
138 | 135 |
|
139 | 136 | const getRootPosition = computed(() => { |
@@ -194,73 +191,56 @@ export default create({ |
194 | 191 | }) |
195 | 192 |
|
196 | 193 | const getPopoverContentW = async () => { |
197 | | - useTaroRect(popoverContentRef).then( |
198 | | - (rect: { width: number, height: number }) => { |
199 | | - elRect.value = { |
200 | | - height: rect.height, |
201 | | - width: rect.width |
202 | | - } |
203 | | - }, |
204 | | - () => {} |
205 | | - ) |
| 194 | + try { |
| 195 | + const rect = await useTaroRect(popoverContentRef) |
| 196 | + elRect.value = { |
| 197 | + height: rect.height, |
| 198 | + width: rect.width |
| 199 | + } |
| 200 | + } catch (error) { |
| 201 | + console.warn('[NutUI] Failed to get rect:', error) |
| 202 | + } |
206 | 203 | } |
207 | 204 |
|
208 | 205 | // 获取宽度 |
209 | | - const getContentWidth = () => { |
| 206 | + const getContentWidth = async () => { |
| 207 | + getPopoverContentW() |
210 | 208 | Taro.createSelectorQuery() |
211 | 209 | .selectViewport() |
212 | | - .scrollOffset((res) => { |
213 | | - const distance = res.scrollTop |
214 | | -
|
| 210 | + .scrollOffset(async (result) => { |
| 211 | + const distance = result.scrollTop |
215 | 212 | if (props.targetId) { |
216 | | - useTaroRectById(props.targetId).then( |
217 | | - (rect: any) => { |
218 | | - rootPosition.value = { |
219 | | - width: rect?.width, |
220 | | - height: rect?.height, |
221 | | - left: rect?.left, |
222 | | - top: rect?.top + distance, |
223 | | - right: rect?.right |
224 | | - } |
225 | | - }, |
226 | | - () => {} |
227 | | - ) |
| 213 | + const rect = (await useTaroRectById(props.targetId)) as rectTaro |
| 214 | + rootPosition.value = { |
| 215 | + width: rect?.width, |
| 216 | + height: rect?.height, |
| 217 | + left: rect?.left, |
| 218 | + top: rect?.top + distance, |
| 219 | + right: rect?.right |
| 220 | + } |
228 | 221 | } else { |
229 | | - useTaroRect(popoverRef).then( |
230 | | - (rect: any) => { |
231 | | - rootPosition.value = { |
232 | | - width: rect?.width, |
233 | | - height: rect?.height, |
234 | | - left: rect?.left, |
235 | | - top: rect?.top + distance, |
236 | | - right: rect?.right |
237 | | - } |
238 | | - }, |
239 | | - () => {} |
240 | | - ) |
| 222 | + const rect = await useTaroRect(popoverRef) |
| 223 | + rootPosition.value = { |
| 224 | + width: rect?.width, |
| 225 | + height: rect?.height, |
| 226 | + left: rect?.left, |
| 227 | + top: rect?.top + distance, |
| 228 | + right: rect?.right |
| 229 | + } |
241 | 230 | } |
242 | 231 | }) |
243 | 232 | .exec() |
244 | | - setTimeout(() => { |
245 | | - getPopoverContentW() |
246 | | - }, 300) |
247 | 233 | } |
248 | 234 |
|
249 | 235 | onMounted(() => { |
250 | | - setTimeout(() => { |
251 | | - getContentWidth() |
252 | | - }, 300) |
| 236 | + getContentWidth() |
253 | 237 | }) |
254 | 238 |
|
255 | 239 | watch( |
256 | 240 | () => props.visible, |
257 | | - (value) => { |
| 241 | + async (value) => { |
258 | 242 | showPopup.value = value |
259 | | - if (value) { |
260 | | - Taro.nextTick(() => { |
261 | | - getContentWidth() |
262 | | - }) |
263 | | - } |
| 243 | + if (value) nextTick(() => getContentWidth()) |
264 | 244 | } |
265 | 245 | ) |
266 | 246 |
|
|
0 commit comments