File tree Expand file tree Collapse file tree 7 files changed +124
-0
lines changed
Expand file tree Collapse file tree 7 files changed +124
-0
lines changed Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ export * from './reactivePick'
1919export * from './refAutoReset'
2020export * from './refDebounced'
2121export * from './refDefault'
22+ export * from './refManualReset'
2223export * from './refThrottled'
2324export * from './refWithControl'
2425export * from './set'
Original file line number Diff line number Diff line change 1+ <script setup lang="ts">
2+ import { refManualReset } from ' @vueuse/core'
3+
4+ const message = refManualReset (' Default message' )
5+
6+ function setMessage() {
7+ message .value = ' Changed'
8+ }
9+ function resetMessage() {
10+ message .reset ()
11+ }
12+ </script >
13+
14+ <template >
15+ <div >
16+ <input v-model =" message" type =" text" >
17+ <button @click =" setMessage()" >
18+ Change Message
19+ </button >
20+ <button @click =" resetMessage()" >
21+ Reset Message
22+ </button >
23+ </div >
24+ </template >
Original file line number Diff line number Diff line change 1+ ---
2+ category : Reactivity
3+ alias : manualResetRef
4+ ---
5+
6+ # refManualReset
7+
8+ Create a ref with manual reset functionality.
9+
10+ ## Usage
11+
12+ ``` ts
13+ import { refManualReset } from ' @vueuse/core'
14+
15+ const message = refManualReset (' default message' )
16+
17+ message .value = ' message has set'
18+
19+ message .reset ()
20+
21+ console .log (message .value ) // 'default message'
22+ ```
Original file line number Diff line number Diff line change 1+ import { describe , expect , it } from 'vitest'
2+ import { refManualReset } from './index'
3+
4+ describe ( 'refManualReset' , ( ) => {
5+ it ( 'should be defined' , ( ) => {
6+ expect ( refManualReset ) . toBeDefined ( )
7+ } )
8+
9+ it ( 'should be default at first' , ( ) => {
10+ const val = refManualReset ( 'default' )
11+ expect ( val . value ) . toBe ( 'default' )
12+ } )
13+
14+ it ( 'should be updated' , ( ) => {
15+ const val = refManualReset ( 'default' )
16+
17+ val . value = 'update'
18+ expect ( val . value ) . toBe ( 'update' )
19+ } )
20+
21+ it ( 'should be reset' , async ( ) => {
22+ const val = refManualReset ( 'default' )
23+ val . value = 'update'
24+
25+ val . reset ( )
26+ expect ( val . value ) . toBe ( 'default' )
27+ } )
28+ } )
Original file line number Diff line number Diff line change 1+ import type { MaybeRefOrGetter , Ref } from 'vue'
2+ import type { Fn } from '../utils'
3+ import { customRef , toValue } from 'vue'
4+
5+ /**
6+ * Define the shape of a ref that supports manual reset functionality.
7+ *
8+ * This interface extends the standard `Ref` type from Vue and adds a `reset` method.
9+ * The `reset` method allows the ref to be manually reset to its default value.
10+ */
11+ export interface ManualResetRefReturn < T > extends Ref < T > {
12+ reset : Fn
13+ }
14+
15+ /**
16+ * Create a ref with manual reset functionality.
17+ *
18+ * @see https://vueuse.org/refManualReset
19+ * @param defaultValue The value which will be set.
20+ */
21+ export function refManualReset < T > ( defaultValue : MaybeRefOrGetter < T > ) {
22+ let value : T = toValue ( defaultValue )
23+ let trigger : Fn
24+
25+ const reset = ( ) => {
26+ value = toValue ( defaultValue )
27+ trigger ( )
28+ }
29+
30+ const refValue = customRef < T > ( ( track , _trigger ) => {
31+ trigger = _trigger
32+ return {
33+ get ( ) {
34+ track ( )
35+ return value
36+ } ,
37+ set ( newValue ) {
38+ value = newValue
39+ trigger ( )
40+ } ,
41+ }
42+ } ) as ManualResetRefReturn < T >
43+
44+ refValue . reset = reset
45+
46+ return refValue
47+ }
Original file line number Diff line number Diff line change 100100 refAutoReset : function
101101 refDebounced : function
102102 refDefault : function
103+ refManualReset : function
103104 refThrottled : function
104105 refWithControl : function
105106 set : function
Original file line number Diff line number Diff line change 6060 refAutoReset : function
6161 refDebounced : function
6262 refDefault : function
63+ refManualReset : function
6364 refThrottled : function
6465 refWithControl : function
6566 set : function
You can’t perform that action at this time.
0 commit comments