Skip to content

Commit a1970e2

Browse files
authored
feat: 🚀 merge useControlledState, concurrent request support, and various optimizations from beta (#277)
* feat: support component (#273) * feat: support component * feat: support component * feat: support component * refactor: 修改 createUseRequestQueryComponent 为 createUseRequestComponent,增强类型支持并简化 props 处理 * version: 2.4.0-alpha.1 * version: 2.4.0-alpha.2 * version: 2.4.0-alpha.3 * feat: Add concurrent request support and enhance parameter management * test: add useBoolean * version: 2.4.0-alpha.4 * feat: 更新文档和组件,添加组件 Hook 指南及 useRequest 组件示例 * fix: 更新文档链接,修复文件系统模块导入,优化代码结构 * refactor: update useEventEmitter implementation and improve documentation links * version: bump to 2.4.0-alpha.5 * test: enhance useEventEmitter tests with subscription, unsubscription, and event replay scenarios * test: add unit tests for useControlledState to validate controlled and uncontrolled behavior * test: 🔧 add test * test: 🔧 add test * docs: mark version * version: 2.4.0-beta.1 * docs: updated * chore: command opt * style: format * chore: rm unuse * chore: opt 🚛 * chore: opt 🚛 * chore: rm dep * feat: 🚀 useControlledState * version: 2.4.0-beta.2 * docs: opt * test: update desc * version: 2.4.0
1 parent 112fd55 commit a1970e2

File tree

72 files changed

+4727
-2225
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+4727
-2225
lines changed

.markdownlint.json

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
{
22
"no-inline-html": {
3-
"allowed_elements": ["ul", "li", "div", "img", "a", "br", "script", "Badge", "Home","demo","VPTeamPage","VPTeamPageTitle","VPTeamMembers","VPTeamPageSection"]
3+
"allowed_elements": [
4+
"ul",
5+
"li",
6+
"div",
7+
"img",
8+
"a",
9+
"br",
10+
"script",
11+
"Badge",
12+
"Home",
13+
"demo",
14+
"VPTeamPage",
15+
"VPTeamPageTitle",
16+
"VPTeamMembers",
17+
"VPTeamPageSection"
18+
]
419
},
520
"MD013": false,
621
"MD041": false
7-
}
22+
}

.vscode/settings.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

docs/.vitepress/config/en.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ function sidebarGuide(): DefaultTheme.SidebarItem[] {
114114
{
115115
text: 'Guide',
116116
items: [
117-
{ text: 'Introduction', link: "/guide" },
117+
{ text: 'Introduction', link: "/guide/introduction" },
118118
{ text: 'Getting Started', link: '/guide/getting-started' },
119119
{ text: '🫶 Migrate to v2 version', link: '/guide/migrate' }
120120
],
@@ -137,6 +137,7 @@ export const en = defineConfig({
137137
...sidebarHooks(),
138138
]
139139
},
140+
'/components/': sidebarComponents(),
140141
},
141142

142143
editLink: {
@@ -155,9 +156,9 @@ function nav(): DefaultTheme.NavItem[] {
155156
return [
156157
{
157158
text: 'Guide',
158-
activeMatch: '/guide/',
159+
activeMatch: '^/guide/',
159160
items: [
160-
{ text: 'Introduction', link: '/guide' },
161+
{ text: 'Introduction', link: '/guide/introduction' },
161162
{ text: 'Getting Started', link: '/guide/getting-started' },
162163
{ text: '🫶 Migrate to v2 version', link: '/guide/migrate' }
163164
]
@@ -167,6 +168,11 @@ function nav(): DefaultTheme.NavItem[] {
167168
link: '/hooks/useRequest/quick-start',
168169
activeMatch: '/hooks'
169170
},
171+
{
172+
text: "Components",
173+
link: '/components/guide',
174+
activeMatch: '/components/'
175+
},
170176
{
171177
text: '🤺 Playground',
172178
link: 'https://inhiblabcore.github.io/vue-hooks-plus-playground/play',
@@ -191,7 +197,7 @@ export function sidebarHooks(): DefaultTheme.SidebarItem[] {
191197
text: 'State',
192198
items: [
193199
{ text: 'useBoolean', link: 'useBoolean' },
194-
// { text: "useControlledState", link: "useControlledState" },
200+
{ text: "useControlledState", link: "useControlledState" },
195201
{ text: 'useImmer', link: 'useImmer' },
196202
{ text: 'useUrlState', link: 'useUrlState' },
197203
{ text: 'useFormatResult', link: 'useFormatResult' },
@@ -265,7 +271,7 @@ export function sidebarHooks(): DefaultTheme.SidebarItem[] {
265271
{
266272
text: 'Advanced',
267273
items: [
268-
{ text: 'useEventEmitter', link: '/useEventEmitter/' },
274+
{ text: 'useEventEmitter', link: 'useEventEmitter' },
269275
{
270276
text: 'useAsyncOrder',
271277
link: 'useAsyncOrder',
@@ -286,3 +292,19 @@ export function sidebarHooks(): DefaultTheme.SidebarItem[] {
286292
})
287293
}
288294

295+
296+
297+
export function sidebarComponents(): DefaultTheme.SidebarItem[] {
298+
return [
299+
{
300+
text: 'Components',
301+
items: [
302+
{
303+
text: 'Guide',
304+
link: '/components/guide',
305+
},
306+
{ text: 'useRequest', link: '/components/useRequest' },
307+
],
308+
}
309+
]
310+
}

docs/.vitepress/config/factory.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import fsExtra from 'fs-extra';
1+
import * as fs from 'node:fs';
22
import { dirname, join, sep } from 'path';
33
import { MarkdownRenderer } from 'vitepress';
44

@@ -48,13 +48,13 @@ export function genDemoByCode(
4848
while (true) {
4949
demoName = `demo-${fenceIndex++}.vue`;
5050
demoPath = join(dirname(path), 'dist', demoName);
51-
if (!fsExtra.existsSync(demoPath)) {
51+
if (!fs.existsSync(demoPath)) {
5252
break;
5353
}
5454
}
5555

56-
fsExtra.createFileSync(demoPath);
57-
fsExtra.writeFileSync(demoPath, code);
56+
fs.writeFileSync(demoPath, '');
57+
fs.writeFileSync(demoPath, code);
5858

5959
codeFileMap[code] = {
6060
demoName,

docs/.vitepress/config/zh.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,19 @@ function sidebarGuide(): DefaultTheme.SidebarItem[] {
123123
}
124124
]
125125
}
126+
127+
function sidebarComponents(): DefaultTheme.SidebarItem[] {
128+
return [
129+
{
130+
text: 'Components',
131+
items: [
132+
{ text: 'Guide', link: '/zh/components/guide' },
133+
{ text: 'useRequest', link: '/zh/components/useRequest' },
134+
],
135+
}
136+
]
137+
}
138+
126139
export const zh = defineConfig({
127140
lang: 'zh-Hans',
128141
description: '高性能的 Vue 组合式函数库',
@@ -138,7 +151,8 @@ export const zh = defineConfig({
138151
...siderbarUseRequestPlugin(),
139152
...sidebarHooks()
140153
]
141-
}
154+
},
155+
'/zh/components/': sidebarComponents(),
142156
},
143157

144158
editLink: {
@@ -182,18 +196,23 @@ function nav(): DefaultTheme.NavItem[] {
182196
return [
183197
{
184198
text: '指南',
185-
activeMatch: '/zh/guide/',
199+
activeMatch: '^/zh/guide/',
186200
items: [
187-
{ text: '介绍', link: '/zh/guide' },
201+
{ text: '介绍', link: '/zh/guide/introduction' },
188202
{ text: '开始使用', link: '/zh/guide/getting-started' },
189203
{ text: '🫶 迁移到 v2 版本', link: '/zh/guide/migrate' }
190204
]
191205
},
192206
{
193-
text: 'Hooks',
207+
text: 'Hooks 函数',
194208
link: '/zh/hooks/useRequest/quick-start',
195209
activeMatch: '/zh/hooks'
196210
},
211+
{
212+
text: '函数组件',
213+
link: '/zh/components/guide',
214+
activeMatch: '/zh/components/'
215+
},
197216
{
198217
text: '🤺 演武场',
199218
link: 'https://inhiblabcore.github.io/vue-hooks-plus-playground/play',

docs/.vitepress/plugins/applyMdPlugin.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { parseProps } from "../config/utils";
22
import { resolve } from 'path'
3-
import fsExtra from 'fs-extra'
3+
import * as fs from 'node:fs'
44
import { getDemoComponent } from "../config/factory";
55
export const applyMdPlugin = (md: any) => {
66
const htmlBlock = md.renderer.rules.html_block!
7-
md.renderer.rules.html_block = function (tokens, idx, options, env, self) {
7+
md.renderer.rules.html_block = function (tokens: any, idx: any, options: any, env: any, self: any) {
88
const token = tokens[idx];
99
const content = token.content.trim();
1010
const { path } = env;
@@ -17,7 +17,7 @@ export const applyMdPlugin = (md: any) => {
1717
}
1818

1919
const srcPath = resolve(process.cwd(), "docs", "demo", props.src);
20-
const code = fsExtra.readFileSync(srcPath, 'utf8');
20+
const code = fs.readFileSync(srcPath, 'utf8');
2121
const demoScripts = getDemoComponent(md, env, {
2222
title: props?.title,
2323
desc: props?.desc,

docs/demo/useControlledState/demo.vue

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<template>
2+
<div>
3+
<div>Show default value: {{ count }}</div>
4+
<div>Show form value: {{ countFormValue }}</div>
5+
<vhp-button @click="() => (value = 10)">Set value</vhp-button>
6+
</div>
7+
</template>
8+
9+
<script lang="ts" setup>
10+
import { ref } from 'vue'
11+
import { useControlledState } from 'vue-hooks-plus'
12+
const value = ref<number | undefined>(undefined)
13+
const [count] = useControlledState(undefined, 10)
14+
const [countFormValue] = useControlledState(value, 0)
15+
</script>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<template>
2+
<div>
3+
<vhp-button
4+
:style="{ backgroundColor: color }"
5+
@click="
6+
() => {
7+
setColor(randomColor())
8+
}
9+
"
10+
>Uncontrolled color</vhp-button
11+
>
12+
</div>
13+
</template>
14+
15+
<script lang="ts" setup>
16+
import { useControlledState } from 'vue-hooks-plus'
17+
const [color, setColor] = useControlledState(undefined, 'red')
18+
const randomColor = () => {
19+
return '#' + Math.floor(Math.random() * 16777215).toString(16)
20+
}
21+
</script>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<template>
2+
<div>
3+
<vhp-button
4+
style="margin-right: 10px"
5+
:style="{ backgroundColor: color1 }"
6+
@click="
7+
() => {
8+
setColor1(randomColor())
9+
}
10+
"
11+
>Controlled unvalid value</vhp-button
12+
>
13+
14+
<vhp-button
15+
:style="{ backgroundColor: color }"
16+
@click="
17+
() => {
18+
setColor(randomColor())
19+
}
20+
"
21+
>Controlled color</vhp-button
22+
>
23+
</div>
24+
</template>
25+
26+
<script lang="ts" setup>
27+
import { ref } from 'vue'
28+
import { useControlledState } from 'vue-hooks-plus'
29+
30+
const value = ref<string | undefined>('red')
31+
32+
const value2 = ref<string | undefined>('red')
33+
34+
const [color1, setColor1] = useControlledState(value2, undefined)
35+
const [color, setColor] = useControlledState(value, undefined, v => {
36+
value.value = v
37+
})
38+
const randomColor = () => {
39+
return '#' + Math.floor(Math.random() * 16777215).toString(16)
40+
}
41+
</script>

docs/demo/useEventEmitter/App.vue

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<template>
2+
<div>
3+
<vhp-button @click="refRouter = !refRouter" style="margin-bottom: 16px;">
4+
toggle router</vhp-button
5+
>
6+
<demo v-if="refRouter" />
7+
<div v-else>
8+
SHOW TEXT
9+
</div>
10+
</div>
11+
</template>
12+
13+
<script lang="ts" setup>
14+
import { ref } from 'vue'
15+
import Demo from './demo.vue'
16+
17+
const refRouter = ref(true)
18+
</script>

docs/demo/useEventEmitter/DemoA.vue

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,30 @@
66
</template>
77

88
<script lang="ts" setup>
9-
import { ref } from 'vue'
9+
import { onMounted, onUnmounted, ref } from 'vue'
1010
1111
import { useEventEmitter } from 'vue-hooks-plus'
12+
import { EventEmitter } from 'vue-hooks-plus/src/useEventEmitter/event'
1213
13-
const event = useEventEmitter({ global: true })
14+
const event_ = useEventEmitter({ global: true })
1415
const props = defineProps<{
15-
event: any
16+
event: EventEmitter<any>
1617
}>()
1718
const count = ref(0)
1819
19-
props.event.useSubscription('change', () => {
20-
count.value += 1
20+
let unsubscribe: (() => void) | null = null
21+
22+
onMounted(() => {
23+
unsubscribe = props.event.subscribe('change', () => {
24+
console.log('props', props.event)
25+
count.value += 1
26+
})
27+
})
28+
onUnmounted(() => {
29+
if (unsubscribe) unsubscribe()
2130
})
2231
2332
const changeGlobal = () => {
24-
event.emit('change')
33+
event_.emit('change')
2534
}
2635
</script>

docs/demo/useEventEmitter/GlobalDemo.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
const event = useEventEmitter({ global: true })
1111
const globalCount = ref(0)
1212
13-
event.useSubscription('change', () => {
13+
event.subscribe('change', () => {
14+
console.log('global', event)
1415
globalCount.value += 1
1516
})
1617
</script>

docs/demo/useEventEmitter/demo.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
}
2828
2929
const changeA = () => {
30-
// globalCount.value += 1
3130
eventA.emit('change')
3231
}
3332
</script>

docs/demo/useMouse/demo.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
<p>Client - x: {{ mouse.clientX }}, y: {{ mouse.clientY }}</p>
44
<p>Page - x: {{ mouse.pageX }}, y: {{ mouse.pageY }}</p>
55
<p>Screen - x: {{ mouse.screenX }}, y: {{ mouse.screenY }}</p>
6+
<UseMouse v-slot="{ clientX, clientY }">
7+
<p>component use: {{ clientX }}, y: {{ clientY }}</p>
8+
</UseMouse>
69
</div>
710
</template>
811

912
<script lang="ts" setup>
10-
import { useMouse } from 'vue-hooks-plus'
13+
import { useMouse, UseMouse } from 'vue-hooks-plus'
1114
const mouse = useMouse()
1215
</script>

0 commit comments

Comments
 (0)