Skip to content

Commit f191634

Browse files
authored
feat: ✨ usePrevious
feat: optimized style and add usePrevious
2 parents d4a74f7 + f2bc66c commit f191634

File tree

20 files changed

+271
-60
lines changed

20 files changed

+271
-60
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@
2424
"@typescript-eslint/eslint-plugin": "^5.33.0",
2525
"@typescript-eslint/parser": "^5.33.0",
2626
"@vitest/ui": "^0.25.3",
27-
"@ruabick/vite-plugin-gen-temp": "^0.2.8",
2827
"@types/fs-extra": "^9.0.13",
2928
"@types/jest": "^29.0.0",
3029
"@types/lodash": "^4.14.178",
3130
"@types/marked": "^4.0.3",
3231
"@types/node": "^17.0.21",
3332
"@types/qs": "^6.9.7",
33+
"@types/markdown-it": "^12.2.3",
3434
"@vitejs/plugin-vue": "^2.3.1",
35+
"@vue-hooks-plus/vite-plugin-gen-temp": "2.6.0",
3536
"@vue-hooks-plus/md-demo-plugins": "^1.0.0",
3637
"@vue-hooks-plus/vitepress": "1.2.3",
3738
"@vue-hooks-plus/vitepress-demo-block": "^1.3.1",
@@ -43,6 +44,7 @@
4344
"eslint-plugin-vue": "^9.6.0",
4445
"husky": "^8.0.1",
4546
"lint-staged": "^13.0.3",
47+
"markdown-it": "^13.0.1",
4648
"vue-eslint-parser": "^9.0.3",
4749
"cross-env": "^7.0.3",
4850
"fast-glob": "^3.2.12",

packages/hooks/docs/.vitepress/config.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import { defineConfig } from 'vitepress'
22
import { applyPlugins } from '@vue-hooks-plus/md-demo-plugins'
3-
import { genTemp } from '@ruabick/vite-plugin-gen-temp'
3+
import { genTemp } from '@vue-hooks-plus/vite-plugin-gen-temp'
44
import { resolve } from 'path'
55
import { svg } from './theme/home/iconBase64'
66
import { getRouterConfig } from './router'
7-
// import { ref } from 'vue'
8-
97
const base =
108
process.env.NODE_ENV === 'production'
119
? process.env?.BASE_URL
@@ -15,8 +13,6 @@ const base =
1513

1614
const isGithub = process.env.FLOW === 'github'
1715

18-
// const version = ref()
19-
2016
// fetch('https://api.github.com/repos/InhiblabCore/vue-hooks-plus/releases/latest')
2117
// .then(response => response.json())
2218
// .then(data => {
@@ -74,7 +70,7 @@ export default defineConfig({
7470
locales: {
7571
'/': {
7672
nav: [
77-
{ text: 'Hooks', link: '/useRequest/' },
73+
{ text: 'Hooks', link: '/useRequest/' },
7874
{
7975
text: '演武场',
8076
link: 'http://43.138.187.142:9000/vue-hooks-plus/vhp-playground/',
@@ -85,10 +81,6 @@ export default defineConfig({
8581
link: 'https://inhiblab-core.gitee.io/docs/hooks/',
8682
}
8783
: null,
88-
// {
89-
// text: 'Github 站点',
90-
// link: 'https://inhiblabcore.github.io/docs/hooks/',
91-
// },
9284
{
9385
text: '发行版本',
9486
link: 'https://github.com/InhiblabCore/vue-hooks-plus/releases/',
@@ -98,7 +90,7 @@ export default defineConfig({
9890
},
9991
'/en/': {
10092
nav: [
101-
{ text: 'Hooks', link: '/en/useRequest/' },
93+
{ text: 'Hooks', link: '/en/useRequest/' },
10294
{
10395
text: 'Playground',
10496
link: 'http://43.138.187.142:9000/vue-hooks-plus/vhp-playground/',

packages/hooks/docs/.vitepress/router.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const Router = [
1313
{ text: 'useSessionStorageState', link: '/useSessionStorageState/' },
1414
{ text: 'useMap', link: '/useMap/' },
1515
{ text: 'useSet', link: '/useSet/' },
16+
{ text: 'usePrevious', link: '/usePrevious/' },
1617
{ text: 'useSetState', link: '/useSetState/' },
1718
],
1819
},
@@ -242,6 +243,10 @@ export function getRouterConfig(langPrefix = '/') {
242243
text: langPrefix === '/' ? '📐 UseRequest规范' : '📐 UseRequest specification',
243244
link: `${langPrefix}useRequest/guide/`,
244245
},
246+
// {
247+
// text: langPrefix === '/' ? '🧑‍🏫 在线教程' : '🧑‍🏫 Online Teaching',
248+
// link: `${langPrefix}onlineTeaching/`,
249+
// },
245250
],
246251
},
247252
...getUseRequestRouter(langPrefix),

packages/hooks/docs/.vitepress/theme/home/Home-EN.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@
161161
font-size: 16px;
162162
display: inline-block;
163163
background: var(--vhp-getstart-bg);
164-
border-radius: 20px;
164+
border-radius: 12px;
165165
padding: 8px 18px;
166166
font-weight: 500;
167167
transition: background-color 0.5s, color 0.5s;

packages/hooks/docs/.vitepress/theme/home/Home.vue

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<p class="description"> </p>
2121
<p class="actions" style="position: relative; z-index: 9;">
2222
<a class="get-started" href="/docs/hooks/guide/">
23-
Get Started
23+
快速开始
2424
<svg
2525
class="icon"
2626
xmlns="http://www.w3.org/2000/svg"
@@ -96,10 +96,6 @@
9696
<div class="demo-name">
9797
Demo.vue
9898
</div>
99-
100-
<!-- <div class="language-md !bg-transparent">
101-
<pre ref="block" class="text-left whitespace-normal font-mono bg-transparent"></pre>
102-
</div> -->
10399
</DemoEditor>
104100
<!-- <HomeTeam /> -->
105101
</div>
@@ -163,7 +159,7 @@
163159
font-size: 16px;
164160
display: inline-block;
165161
background: var(--vhp-getstart-bg);
166-
border-radius: 20px;
162+
border-radius: 12px;
167163
padding: 8px 18px;
168164
font-weight: 500;
169165
transition: background-color 0.5s, color 0.5s;

packages/hooks/docs/.vitepress/theme/var.less

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
--vp-home-hero-name-background: -webkit-linear-gradient(315deg, #42d392 25%, #647eff);
1313

1414
--vp-font-color: rgba(41, 65, 86, 1);
15+
// --vp-c-text-code: var(--vp-c-brand);
1516

1617
--vhp-func-bg: rgba(0, 0, 0, 0.08);
1718

packages/hooks/docs/guide/index.en-US.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,6 @@ import useRequest from 'vue-hooks-plus/es/useRequest'
4343

4444
<br />
4545

46-
::: warning does not contain vue2 :::
46+
::: warning does not contain vue2
47+
48+
:::

packages/hooks/docs/guide/index.zh-CN.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,6 @@ import useRequest from 'vue-hooks-plus/es/useRequest'
4343

4444
<br />
4545

46-
::: warning 不兼容 vue2 版本 :::
46+
::: warning 不兼容 vue2 版本
47+
48+
:::
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
page: true
3+
title: Tutorial
4+
sidebar: false
5+
aside: false
6+
footer: false
7+
returnToTop: false
8+
---
9+
10+
test
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
layout: home
3+
sidebar: false
4+
---
5+
6+
test

packages/hooks/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import useFetchs from './useFetchs'
1818
import useFullscreen from './useFullscreen'
1919
import useHover from './useHover'
2020
import usePreview from './usePreview'
21+
import usePrevious from './usePrevious'
2122
import useInterval from './useInterval'
2223
import useInfiniteScroll from './useInfiniteScroll'
2324
import useInViewport from './useInViewport'
@@ -78,6 +79,7 @@ export {
7879
useMouse,
7980
useNetwork,
8081
usePreview,
82+
usePrevious,
8183
useSessionStorageState,
8284
useSet,
8385
useSetState,
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<template>
2+
<div>
3+
<p>counter current value:{{ counter }}</p>
4+
<p>counter previous value:{{ prevCounter }}</p>
5+
<vhp-button @click="inc()">increase</vhp-button>
6+
<vhp-button @click="dec()" style="margin-left: 8px;">decrease</vhp-button>
7+
</div>
8+
</template>
9+
10+
<script lang="ts" setup>
11+
import { usePrevious, useCounter } from 'vue-hooks-plus'
12+
const [counter, { inc, dec }] = useCounter(0)
13+
const prevCounter = usePrevious(counter)
14+
</script>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<template>
2+
<div style="border: 1px solid rgba(100, 126, 255, 0.7);border-radius: 12px; padding: 6px;">
3+
<p>current name: {{ currentName }}</p>
4+
<p>current age: {{ currentAge }}</p>
5+
</div>
6+
<br>
7+
<div style="border: 1px solid rgba(59, 206, 128, 0.7);border-radius: 12px; padding: 6px;">
8+
<p>previous name: {{ previousName }}</p>
9+
<p>previous age: {{ previousage }}</p>
10+
</div>
11+
<br>
12+
<div>
13+
<input v-model="currentNameInput" placeholder="Input Name">
14+
<vhp-button @click="handleUpdateName" style="margin-left: 8px;">Update</vhp-button>
15+
</div>
16+
17+
<br>
18+
<div>
19+
<input v-model="currentAgeInput" placeholder="Input Age">
20+
<vhp-button @click="handleUpdateAge" style="margin-left: 8px;">Update</vhp-button>
21+
</div>
22+
</template>
23+
24+
<script lang="ts" setup>
25+
import { ref } from 'vue'
26+
import { usePrevious } from 'vue-hooks-plus'
27+
28+
const currentNameInput = ref('')
29+
const currentName = ref('')
30+
const currentAgeInput = ref()
31+
const currentAge = ref()
32+
33+
const nameCompareFunction = (prev: string | undefined, next: string) => {
34+
if (!prev) {
35+
return true
36+
}
37+
if (prev !== next) {
38+
return true
39+
}
40+
return false
41+
}
42+
43+
const ageCompareFunction = (prev: number | undefined, next: number) => {
44+
if (!prev) {
45+
return true
46+
}
47+
if (prev !== next) {
48+
return true
49+
}
50+
return false
51+
}
52+
53+
const previousName = usePrevious(currentName, nameCompareFunction)
54+
const previousage = usePrevious(currentAge, ageCompareFunction)
55+
56+
const handleUpdateName = () => {
57+
currentName.value = currentNameInput.value
58+
}
59+
60+
const handleUpdateAge = () => {
61+
currentAge.value = currentAgeInput.value
62+
}
63+
</script>
64+
65+
<style scoped lang="less"></style>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
map:
3+
# 映射到docs的路径
4+
path: /usePrevious
5+
---
6+
7+
# usePrevious
8+
9+
A Hook to return the previous state.
10+
11+
## Basic usage
12+
13+
<demo src="./demo/demo.vue"
14+
language="vue"
15+
title="Basic usage"
16+
desc="Record the previous value"> </demo>
17+
18+
## Custom shouldUpdate function
19+
20+
<demo src="./demo/demo1.vue"
21+
language="vue"
22+
title="Custom shouldUpdate function"
23+
desc="Previous value update only when the shouldUpdate function return true"> </demo>
24+
25+
## Result
26+
27+
| Property | Description | Type |
28+
| ----------- | ------------------ | -------- |
29+
| previousRef | The previous value | `Ref<T>` |
30+
31+
## Params
32+
33+
| Property | Description | Type | Default Value |
34+
| --- | --- | --- | --- |
35+
| currentRef | The state that needs to be tracked | `Ref<T>` \| `ComputedRef<T>` | - |
36+
| shouldUpdate | Optional. Customize whether the state value should be updated | `(prev: T \| undefined, next: T) => boolean` | `(a, b) => a !== b` |
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { ComputedRef, Ref, ref, watchEffect } from 'vue'
2+
3+
export type ShouldUpdateFunc<T> = (prev: T | undefined, next: T) => boolean
4+
const defaultShouldUpdate = <T>(a?: T, b?: T) => !Object.is(a, b)
5+
6+
function usePrevious<T>(
7+
state: Ref<T> | ComputedRef<T>,
8+
shouldUpdate: ShouldUpdateFunc<T> = defaultShouldUpdate,
9+
) {
10+
const prevRef = ref<T>()
11+
const curRef = ref<T>()
12+
13+
watchEffect(() => {
14+
if (shouldUpdate(curRef.value, state.value)) {
15+
prevRef.value = curRef.value
16+
curRef.value = state.value
17+
}
18+
})
19+
20+
return prevRef
21+
}
22+
23+
export default usePrevious
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
map:
3+
# 映射到docs的路径
4+
path: /usePrevious
5+
---
6+
7+
# usePrevious
8+
9+
保存上一次状态的 Hook。
10+
11+
## 基础用法
12+
13+
<demo src="./demo/demo.vue"
14+
language="vue"
15+
title="基本用法"
16+
desc="记录上次的 count 值"> </demo>
17+
18+
## 自定义 shouldUpdate 函数
19+
20+
<demo src="./demo/demo1.vue"
21+
language="vue"
22+
title="自定义 shouldUpdate 函数"
23+
desc="只有 shouldUpdate function 返回 true 时,才会记录值的变化"> </demo>
24+
25+
## Result
26+
27+
| 参数 | 说明 | 类型 |
28+
| ----------- | --------------- | -------- |
29+
| previousRef | 上次 state 的值 | `Ref<T>` |
30+
31+
## Params
32+
33+
| 参数 | 说明 | 类型 | 默认值 |
34+
| --- | --- | --- | --- |
35+
| currentRef | 需要记录变化的值 | `Ref<T>` \| `ComputedRef<T>` | - |
36+
| shouldUpdate | 可选,自定义判断值是否变化 | `(prev: T \| undefined, next: T) => boolean` | `(a, b) => a !== b` |

packages/hooks/src/useRequest/docs/basic/index.en-US.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ const {
174174
);
175175
```
176176

177-
### Result
177+
## Result
178178

179179
| Property | Description | Type |
180180
| --- | --- | --- |
@@ -190,7 +190,7 @@ const {
190190
| mutate | Mutate `data` directly | `(data?: TData / ((oldData?: TData) => (TData / undefined))) => void` |
191191
| cancel | Ignore the current promise response | `() => void` |
192192

193-
### Options
193+
## Options
194194

195195
| Property | Description | Type | Default |
196196
| --- | --- | --- | --- |

0 commit comments

Comments
 (0)