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 @@