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

View File

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