fix: revert useDefault

This commit is contained in:
chansee97 2024-06-08 13:58:09 +08:00
parent 530231a5cb
commit 57739f960b
4 changed files with 10 additions and 41 deletions

View File

@ -1,4 +1,3 @@
export * from './useBoolean'
export * from './useEcharts'
export * from './usePermission'
export * from './useDefault'

View File

@ -1,15 +0,0 @@
/**
* Apply default value to a ref.
*/
export function useDefault<T>(defaultValue: T): Ref<T> {
const source: Ref<T | undefined | null> = ref()
return computed({
get() {
return source.value ?? defaultValue
},
set(value) {
source.value = value
},
})
}

View File

@ -1,5 +1,5 @@
<script setup lang="ts">
import { useBoolean, useDefault } from '@/hooks'
import { useBoolean } from '@/hooks'
import { fetchRoleList } from '@/service'
interface Props {
@ -19,14 +19,14 @@ const { bool: modalVisible, setTrue: showModal, setFalse: hiddenModal } = useBoo
const { bool: submitLoading, setTrue: startLoading, setFalse: endLoading } = useBoolean(false)
const formModel = useDefault<Entity.User>({
const formDefault: Entity.User = {
userName: '',
gender: undefined,
email: '',
tel: '',
role: [],
status: 1,
})
}
const formModel = ref<Entity.User>({ ...formDefault })
type ModalType = 'add' | 'view' | 'edit'
const modalType = shallowRef<ModalType>('add')
@ -46,8 +46,7 @@ async function openModal(type: ModalType = 'add', data: any) {
getRoleList()
const handlers = {
async add() {
// @ts-expect-error undefined is safe
formModel.value = undefined
formModel.value = { ...formDefault }
},
async view() {
if (!data)

View File

@ -3,7 +3,7 @@ import type {
FormItemRule,
} from 'naive-ui'
import HelpInfo from '@/components/common/HelpInfo.vue'
import { useBoolean, useDefault } from '@/hooks'
import { useBoolean } from '@/hooks'
import { Regex } from '@/constants'
import { fetchRoleList } from '@/service'
@ -24,24 +24,20 @@ const emit = defineEmits<{
const { bool: modalVisible, setTrue: showModal, setFalse: hiddenModal } = useBoolean(false)
const { bool: submitLoading, setTrue: startLoading, setFalse: endLoading } = useBoolean(false)
const formModel = useDefault<AppRoute.RowRoute>({
const formDefault: AppRoute.RowRoute = {
name: '',
path: '',
id: -1,
pid: null,
title: '',
icon: '',
requiresAuth: true,
roles: [],
keepAlive: false,
hide: false,
order: undefined,
href: undefined,
activeMenu: undefined,
withoutTab: true,
pinTab: false,
menuType: 'page',
})
}
const formModel = ref<AppRoute.RowRoute>({ ...formDefault })
type ModalType = 'add' | 'view' | 'edit'
const modalType = shallowRef<ModalType>('add')
@ -61,8 +57,7 @@ async function openModal(type: ModalType = 'add', data: AppRoute.RowRoute) {
showModal()
const handlers = {
async add() {
// @ts-expect-error undefined is safe
formModel.value = undefined
formModel.value = { ...formDefault }
},
async view() {
if (!data)
@ -164,15 +159,6 @@ const rules = {
message: '请输入菜单标题',
trigger: 'blur',
},
href: {
validator(rule: FormItemRule, value: string) {
if (!new RegExp(Regex.Url).test(value))
return new Error('请输入正确的URL地址')
return true
},
trigger: 'blur',
},
}
const options = ref<Entity.Role[]>([])