Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loading page #686

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/.vitepress/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,9 @@ export default defineConfig({
}, {
link: "/component/loading",
text: "Loading 加载"
}, {
link: "/component/loading-page",
text: "LoadingPage 加载页"
}, {
link: "/component/message-box",
text: "MessageBox 弹框"
Expand Down
94 changes: 94 additions & 0 deletions docs/component/loading-page.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<frame/>

# Loading-page 加载页

## 基本用法

基本用法

```html
<wd-loading-page :loading="show" />
```

```js
const show = ref(false)
```

## 自定义背景色

通过 `bgColor` 修改背景色。

```html
<wd-loading-page :loading="show" bgColor="rgba(0, 0, 0, .9)" />
```

```js
const show = ref(false)
```

## 自定义图片

通过 `loadingImage` 属性修改 loding 图片。

```html
<wd-loading-page :loading="show" loadingImage="https://wot-design-uni.pages.dev/wot-design.png" />
```

```js
const show = ref(false)
```

Comment on lines +29 to +40
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

修正文档错误并补充图片说明

  1. "loding" 应更正为 "loading"
  2. 需要补充图片尺寸建议和格式要求

建议按照以下方式修改:

 ## 自定义图片
 
-通过 `loadingImage` 属性修改 loding 图片。
+通过 `loadingImage` 属性修改 loading 图片。建议使用正方形图片,推荐尺寸 100x100px,支持 PNG、JPG 格式。
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
## 自定义图片
通过 `loadingImage` 属性修改 loding 图片。
```html
<wd-loading-page :loading="show" loadingImage="https://wot-design-uni.pages.dev/wot-design.png" />
```
```js
const show = ref(false)
```
## 自定义图片
通过 `loadingImage` 属性修改 loading 图片。建议使用正方形图片,推荐尺寸 100x100px,支持 PNG、JPG 格式
```html
<wd-loading-page :loading="show" loadingImage="https://wot-design-uni.pages.dev/wot-design.png" />
```
```js
const show = ref(false)
```

## 自定义插槽内容

通过 `#default` 自定义 loding 内容。

```html
<wd-loading-page :loading="show">
<template #default>
<view class="custom-class">
<view class="loading">
<wd-loading />
</view>
<view class="mt-2">加载中...</view>
</view>
</template>
</wd-loading-page>
```

```js
const show = ref(false)
```

```scss
.custom-class {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
width: 100vw;
background-color: rgba($color: #000000, $alpha: 0.8);
}

.mt-2 {
margin-top: 8px;
}
```
Comment on lines +41 to +76
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

⚠️ Potential issue

完善自定义插槽内容的说明

  1. 修正文字错误:loding -> loading
  2. 建议使用变量替代硬编码的样式值
  3. 需要说明示例中使用的 wd-loading 组件

建议按照以下方式修改:

 ## 自定义插槽内容
 
-通过 `#default` 自定义 loding 内容。
+通过 `#default` 自定义 loading 内容。可以使用 `wd-loading` 组件或其他自定义内容。
 
+> 提示:示例中使用了 wd-loading 组件,需要确保已引入该组件。

建议优化 SCSS 代码:

 .custom-class {
   display: flex;
   flex-direction: column;
   align-items: center;
   justify-content: center;
   height: 100vh;
   width: 100vw;
-  background-color: rgba($color: #000000, $alpha: 0.8);
+  background-color: var(--wd-loading-page-bg-color, rgba(0, 0, 0, 0.8));
 }
 
 .mt-2 {
-  margin-top: 8px;
+  margin-top: var(--wd-loading-page-gap, 8px);
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
## 自定义插槽内容
通过 `#default` 自定义 loding 内容。
```html
<wd-loading-page :loading="show">
<template #default>
<view class="custom-class">
<view class="loading">
<wd-loading />
</view>
<view class="mt-2">加载中...</view>
</view>
</template>
</wd-loading-page>
```
```js
const show = ref(false)
```
```scss
.custom-class {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
width: 100vw;
background-color: rgba($color: #000000, $alpha: 0.8);
}
.mt-2 {
margin-top: 8px;
}
```
## 自定义插槽内容
通过 `#default` 自定义 loading 内容。可以使用 `wd-loading` 组件或其他自定义内容。
> 提示:示例中使用了 wd-loading 组件,需要确保已引入该组件。
```html
<wd-loading-page :loading="show">
<template #default>
<view class="custom-class">
<view class="loading">
<wd-loading />
</view>
<view class="mt-2">加载中...</view>
</view>
</template>
</wd-loading-page>
```
```js
const show = ref(false)
```
```scss
.custom-class {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
width: 100vw;
background-color: var(--wd-loading-page-bg-color, rgba(0, 0, 0, 0.8));
}
.mt-2 {
margin-top: var(--wd-loading-page-gap, 8px);
}
```


## Attributes

| 参数 | 说明 | 类型 | 可选值 | 默认值 | 最低版本 |
| ------------ | ----------------------------------- | ------- | ------ | -------- | ---------------- |
| fontSize | 字体大小 | string | - | 16px | $LOWEST_VERSION$ |
| color | 字体颜色 | string | - | 18px | $LOWEST_VERSION$ |
| loadingText | 加载时显示的文字 | string | - | 正在加载 | $LOWEST_VERSION$ |
| loading | 是否加载中 | boolean | - | false | $LOWEST_VERSION$ |
| bgColor | 背景颜色 | string | - | #ffffff | $LOWEST_VERSION$ |
| loadingColor | 加载图标颜色 | string | - | #C8C8C8 | $LOWEST_VERSION$ |
| loadingImage | 文字上方用于替换 loading 动画的图片 | string | - | 空字符串 | $LOWEST_VERSION$ |

## 外部样式类

| 类名 | 说明 | 最低版本 |
| ------------ | ---------- | -------- |
| custom-class | 根节点样式 | - |
Comment on lines +78 to +94
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

修正 API 文档中的问题

  1. color 属性的默认值(18px)似乎有误,应该是颜色值
  2. 需要补充 fontSize 支持的单位说明
  3. 需要替换 $LOWEST_VERSION$ 占位符
  4. 建议补充事件说明部分

建议按照以下方式修改属性表格:

 | 参数         | 说明                                | 类型    | 可选值 | 默认值   | 最低版本         |
 | ------------ | ----------------------------------- | ------- | ------ | -------- | ---------------- |
-| fontSize     | 字体大小                            | string  | -      | 16px     | $LOWEST_VERSION$ |
+| fontSize     | 字体大小,支持 px/em/rem            | string  | -      | 16px     | 0.1.0 |
-| color        | 字体颜色                            | string  | -      | 18px     | $LOWEST_VERSION$ |
+| color        | 字体颜色                            | string  | -      | #666666  | 0.1.0 |
-| loadingText  | 加载时显示的文字                    | string  | -      | 正在加载 | $LOWEST_VERSION$ |
+| loadingText  | 加载时显示的文字                    | string  | -      | 正在加载 | 0.1.0 |

建议在 API 文档末尾添加事件说明:

## Events

| 事件名称 | 说明           | 参数 |
|----------|--------------|------|
| change   | loading 状态变化时触发 | value: boolean |

10 changes: 10 additions & 0 deletions src/pages.json
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,16 @@
"navigationBarTitleText": "Loading 加载指示器"
}
},
{
"path": "pages/loadingPage/Index",
"name": "loadingPage",
"style": {
"mp-alipay": {
"allowsBounceVertical": "NO"
},
"navigationBarTitleText": "LoadingPage 加载页"
}
},
{
"path": "pages/progress/Index",
"name": "progress",
Expand Down
4 changes: 4 additions & 0 deletions src/pages/index/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,10 @@ const list = ref([
id: 'loading',
name: 'Loading 加载指示器'
},
{
id: 'loadingPage',
name: 'LoadingPage 加载页'
},
{
id: 'messageBox',
name: 'MessageBox 弹框'
Expand Down
86 changes: 86 additions & 0 deletions src/pages/loadingPage/Index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<template>
<page-wraper>
<demo-block title="基本用法">
<view class="flex" @click="clickShow">基本用法</view>
<wd-loading-page :loading="show" />
</demo-block>
<demo-block title="自定义背景色">
<view class="flex" @click="clickShow2">自定义背景色</view>
<wd-loading-page :loading="show2" bgColor="rgba(0, 0, 0, .9)" />
</demo-block>
<demo-block title="自定义图片">
<view class="flex" @click="clickShow3">自定义图片</view>
<wd-loading-page :loading="show3" loadingImage="https://wot-design-uni.pages.dev/wot-design.png" />
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

建议使用相对路径或资源管理系统

建议不要直接使用外部域名的图片URL,这可能导致以下问题:

  • 外部资源可能无法访问
  • 增加了对外部服务的依赖
  • 可能影响加载性能

建议将图片资源放在项目内部,使用相对路径或资源管理系统。

-      <wd-loading-page :loading="show3" loadingImage="https://wot-design-uni.pages.dev/wot-design.png" />
+      <wd-loading-page :loading="show3" loadingImage="/assets/images/wot-design.png" />

Committable suggestion was skipped due to low confidence.

</demo-block>
<demo-block title="自定义插槽内容">
<view class="flex" @click="clickShow4">自定义插槽内容</view>
<wd-loading-page :loading="show4">
<template #default>
<view class="custom-class">
<view class="loading">
<wd-loading />
</view>
<view class="mt-2">加载中...</view>
</view>
</template>
</wd-loading-page>
</demo-block>
</page-wraper>
</template>
<script lang="ts" setup>
import { ref } from 'vue'

const show = ref(false)
const show2 = ref(false)
const show3 = ref(false)
const show4 = ref(false)
const clickShow = () => {
show.value = !show.value

// 模拟请求-3秒后关闭
setTimeout(() => {
show.value = false
}, 3000)
}
const clickShow2 = () => {
show2.value = !show2.value

// 模拟请求-3秒后关闭
setTimeout(() => {
show2.value = false
}, 3000)
}

const clickShow3 = () => {
show3.value = !show3.value

// 模拟请求-3秒后关闭
setTimeout(() => {
show3.value = false
}, 6000)
}

const clickShow4 = () => {
show4.value = !show4.value

// 模拟请求-3秒后关闭
setTimeout(() => {
show4.value = false
}, 3000)
}
Comment on lines +37 to +70
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

优化代码结构和资源清理

当前代码存在以下问题:

  1. 点击处理函数有重复代码
  2. setTimeout 没有在组件卸载时清理
  3. 延迟时间使用魔法数字

建议重构如下:

+const LOADING_DELAY = {
+  DEFAULT: 3000,
+  LONG: 6000
+} as const
+
+const timers = ref<number[]>([])
+
+const handleLoading = (show: Ref<boolean>, duration: number = LOADING_DELAY.DEFAULT) => {
+  show.value = !show.value
+  const timer = setTimeout(() => {
+    show.value = false
+  }, duration)
+  timers.value.push(timer)
+}
+
+onBeforeUnmount(() => {
+  timers.value.forEach(timer => clearTimeout(timer))
+})
+
 const clickShow = () => {
-  show.value = !show.value
-
-  // 模拟请求-3秒后关闭
-  setTimeout(() => {
-    show.value = false
-  }, 3000)
+  handleLoading(show)
 }
+
 const clickShow2 = () => {
-  show2.value = !show2.value
-
-  // 模拟请求-3秒后关闭
-  setTimeout(() => {
-    show2.value = false
-  }, 3000)
+  handleLoading(show2)
 }
+
 const clickShow3 = () => {
-  show3.value = !show3.value
-
-  // 模拟请求-3秒后关闭
-  setTimeout(() => {
-    show3.value = false
-  }, 6000)
+  handleLoading(show3, LOADING_DELAY.LONG)
 }
+
 const clickShow4 = () => {
-  show4.value = !show4.value
-
-  // 模拟请求-3秒后关闭
-  setTimeout(() => {
-    show4.value = false
-  }, 3000)
+  handleLoading(show4)
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const clickShow = () => {
show.value = !show.value
// 模拟请求-3秒后关闭
setTimeout(() => {
show.value = false
}, 3000)
}
const clickShow2 = () => {
show2.value = !show2.value
// 模拟请求-3秒后关闭
setTimeout(() => {
show2.value = false
}, 3000)
}
const clickShow3 = () => {
show3.value = !show3.value
// 模拟请求-3秒后关闭
setTimeout(() => {
show3.value = false
}, 6000)
}
const clickShow4 = () => {
show4.value = !show4.value
// 模拟请求-3秒后关闭
setTimeout(() => {
show4.value = false
}, 3000)
}
const LOADING_DELAY = {
DEFAULT: 3000,
LONG: 6000
} as const
const timers = ref<number[]>([])
const handleLoading = (show: Ref<boolean>, duration: number = LOADING_DELAY.DEFAULT) => {
show.value = !show.value
const timer = setTimeout(() => {
show.value = false
}, duration)
timers.value.push(timer)
}
onBeforeUnmount(() => {
timers.value.forEach(timer => clearTimeout(timer))
})
const clickShow = () => {
handleLoading(show)
}
const clickShow2 = () => {
handleLoading(show2)
}
const clickShow3 = () => {
handleLoading(show3, LOADING_DELAY.LONG)
}
const clickShow4 = () => {
handleLoading(show4)
}

</script>
<style lang="scss" scoped>
.custom-class {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
width: 100vw;
background-color: rgba($color: #000000, $alpha: 0.8);
}

.mt-2 {
margin-top: 8px;
}
</style>
43 changes: 43 additions & 0 deletions src/uni_modules/wot-design-uni/components/wd-loading-page/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* @Author: PdxLook
* @Date: 2024-10-30 10:14:34
* @LastEditTime: 2024-10-30 10:48:34
* @LastEditors: PdxLook
* @Description: 定义了加载页面组件的 props 属性及其类型,用于控制加载页面的显示内容和样式。
* @FilePath: /wot-design-uni/src/uni_modules/wot-design-uni/components/wd-loading-page/types.ts
*/

import type { ExtractPropTypes } from 'vue'
import { baseProps, makeBooleanProp, makeStringProp } from '../common/props'

/**
* 定义加载页面组件的属性,包括文本内容、颜色和加载状态等。
* 可通过传递不同的 props 值来自定义组件的显示效果。
*/
export const loadingPageProps = {
// 继承通用属性,便于在多个组件中重复使用
...baseProps,

// 提示内容:加载页面显示的文本,默认为“正在加载”
loadingText: makeStringProp('正在加载'),

// 是否加载中:布尔值,指示是否处于加载状态,默认值为 false
loading: makeBooleanProp(false),

// 背景颜色:加载页面的背景色,默认为白色(#ffffff)
bgColor: makeStringProp('#ffffff'),

// 字体颜色:加载文本的颜色,默认为浅灰色(#C8C8C8)
color: makeStringProp('#C8C8C8'),

// 字体大小:加载文本的字号大小,默认为 16px
fontSize: makeStringProp('16px'),

// 加载图标颜色:加载动画的颜色,默认为浅灰色(#C8C8C8)
loadingColor: makeStringProp('#C8C8C8'),

// 文字上方用于替换loading动画的图片:路径默认为空字符串;如有值,替换默认的加载动画
loadingImage: makeStringProp('')
}
Comment on lines +17 to +41
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

建议对属性类型进行更严格的定义

当前的属性定义结构清晰,但可以通过以下几点改进来增强类型安全性:

  1. 建议为颜色属性定义专门的类型
  2. 建议为字体大小定义固定的尺寸选项
  3. 建议对加载图片路径添加验证

建议按如下方式重构:

// 定义颜色类型
type ColorType = `#${string}` | `rgb(${number}, ${number}, ${number})` | `rgba(${number}, ${number}, ${number}, ${number})`

// 定义字体尺寸
type FontSize = '12px' | '14px' | '16px' | '18px' | '20px'

export const loadingPageProps = {
  ...baseProps,
  loadingText: makeStringProp('正在加载'),
  loading: makeBooleanProp(false),
  bgColor: {
    type: String as PropType<ColorType>,
    default: '#ffffff'
  },
  color: {
    type: String as PropType<ColorType>,
    default: '#C8C8C8'
  },
  fontSize: {
    type: String as PropType<FontSize>,
    default: '16px'
  },
  loadingColor: {
    type: String as PropType<ColorType>,
    default: '#C8C8C8'
  },
  loadingImage: {
    type: String,
    default: '',
    validator: (value: string) => {
      return value === '' || /\.(png|jpg|jpeg|gif|svg)$/.test(value)
    }
  }
}


export type LoadingPageProps = ExtractPropTypes<typeof loadingPageProps>
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<template>
<view :class="`wd-loading-page ${props.customClass}`">
<wd-overlay :show="props.loading" :style="`--wot-overlay-bg:${props.bgColor}`">
<slot v-if="$slots.default"></slot>
<view class="wrapper" v-else>
<view v-if="props.loadingImage">
<image class="" :src="props.loadingImage" mode="aspectFill"></image>
</view>
<wd-loading :color="props.loadingColor" v-else />
<view class="wd-loading-page-text" :style="textStyle">{{ props.loadingText }}</view>
</view>
</wd-overlay>
</view>
</template>
<script lang="ts">
export default {
name: 'wd-loading-page',
options: {
virtualHost: true,
addGlobalClass: true,
styleIsolation: 'shared'
}
}
</script>

<script lang="ts" setup>
import { computed, ref, type CSSProperties } from 'vue'
import { objToStyle, addUnit, isDef } from '../common/util'
import { loadingPageProps } from './types'

const props = defineProps(loadingPageProps)

// 计算样式
const textStyle = computed(() => {
const style: CSSProperties = {}
if (isDef(props.fontSize)) {
style.fontSize = addUnit(props.fontSize)
}
if (isDef(props.color)) {
style.color = props.color
}
return `${objToStyle(style)}; ${props.customStyle}`
})
</script>

<style lang="scss" scoped>
.wrapper {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
height: 100%;

Comment on lines +47 to +53
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

建议增加响应式设计支持

当前布局使用了固定的高度设置,建议考虑添加响应式设计支持,以适应不同屏幕尺寸。

 .wrapper {
   display: flex;
   align-items: center;
   justify-content: center;
   flex-direction: column;
-  height: 100%;
+  min-height: 100%;
+  padding: var(--wd-loading-page-padding, 20px);
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
.wrapper {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
height: 100%;
.wrapper {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
min-height: 100%;
padding: var(--wd-loading-page-padding, 20px);
}

.wd-loading-page-text {
margin: var(--wd-loading-page-text-margin, 8px 0 0 0);
}
}
</style>