From 4aa3a66ce9964fd8af52533204a3161f9d9171dd Mon Sep 17 00:00:00 2001 From: chansee97 Date: Wed, 5 Jun 2024 09:18:21 +0800 Subject: [PATCH] feat: add `useDefault` --- src/hooks/index.ts | 2 +- src/hooks/refForm.ts | 15 --------------- src/hooks/useDefault.ts | 15 +++++++++++++++ .../setting/account/components/TableModal.vue | 7 ++++--- src/views/setting/menu/components/TableModal.vue | 7 ++++--- 5 files changed, 24 insertions(+), 22 deletions(-) delete mode 100644 src/hooks/refForm.ts create mode 100644 src/hooks/useDefault.ts diff --git a/src/hooks/index.ts b/src/hooks/index.ts index e601b77..9d7e9f6 100644 --- a/src/hooks/index.ts +++ b/src/hooks/index.ts @@ -1,4 +1,4 @@ export * from './useBoolean' export * from './useEcharts' export * from './usePermission' -export * from './refForm' +export * from './useDefault' diff --git a/src/hooks/refForm.ts b/src/hooks/refForm.ts deleted file mode 100644 index a7f925c..0000000 --- a/src/hooks/refForm.ts +++ /dev/null @@ -1,15 +0,0 @@ -export function refForm(initValue: T) { - function jsonClone(value: T) { - return JSON.parse(JSON.stringify(value)) - } - const target = ref(initValue) - - function setDefault() { - target.value = jsonClone(initValue) - } - - return { - target, - setDefault, - } -} diff --git a/src/hooks/useDefault.ts b/src/hooks/useDefault.ts new file mode 100644 index 0000000..7aa931e --- /dev/null +++ b/src/hooks/useDefault.ts @@ -0,0 +1,15 @@ +/** + * Apply default value to a ref. + */ +export function useDefault(defaultValue: T): Ref { + const source: Ref = ref() + + return computed({ + get() { + return source.value ?? defaultValue + }, + set(value) { + source.value = value + }, + }) +} diff --git a/src/views/setting/account/components/TableModal.vue b/src/views/setting/account/components/TableModal.vue index 9caf724..a33a4f2 100644 --- a/src/views/setting/account/components/TableModal.vue +++ b/src/views/setting/account/components/TableModal.vue @@ -1,5 +1,5 @@