perf: 登录组件添加enter键触发提交,锁屏组件自动聚焦输入框
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { useI18n } from '@/hooks/web/useI18n'
|
import { useI18n } from '@/hooks/web/useI18n'
|
||||||
import { ref } from 'vue'
|
import { ref, watch } from 'vue'
|
||||||
import { Dialog } from '@/components/Dialog'
|
import { Dialog } from '@/components/Dialog'
|
||||||
import { Form } from '@/components/Form'
|
import { Form } from '@/components/Form'
|
||||||
import { useForm } from '@/hooks/web/useForm'
|
import { useForm } from '@/hooks/web/useForm'
|
||||||
@@ -35,6 +35,20 @@ const dialogVisible = computed({
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// 自动聚焦输入框
|
||||||
|
watch(
|
||||||
|
dialogVisible,
|
||||||
|
async (val) => {
|
||||||
|
if (val) {
|
||||||
|
const formExposeInput = await getComponentExpose('password')
|
||||||
|
setTimeout(() => {
|
||||||
|
formExposeInput?.focus()
|
||||||
|
}, 10)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{ immediate: true }
|
||||||
|
)
|
||||||
|
|
||||||
const dialogTitle = ref(t('lock.lockScreen'))
|
const dialogTitle = ref(t('lock.lockScreen'))
|
||||||
|
|
||||||
const rules = reactive({
|
const rules = reactive({
|
||||||
@@ -48,14 +62,20 @@ const schema: FormSchema[] = reactive([
|
|||||||
component: 'Input',
|
component: 'Input',
|
||||||
componentProps: {
|
componentProps: {
|
||||||
type: 'password',
|
type: 'password',
|
||||||
showPassword: true
|
showPassword: true,
|
||||||
|
// 按下enter键触发登录
|
||||||
|
onKeydown: (_e: any) => {
|
||||||
|
if (_e.key === 'Enter') {
|
||||||
|
handleLock()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
])
|
])
|
||||||
|
|
||||||
const { formRegister, formMethods } = useForm()
|
const { formRegister, formMethods } = useForm()
|
||||||
|
|
||||||
const { getFormData, getElFormExpose } = formMethods
|
const { getFormData, getElFormExpose, getComponentExpose } = formMethods
|
||||||
|
|
||||||
const handleLock = async () => {
|
const handleLock = async () => {
|
||||||
const formExpose = await getElFormExpose()
|
const formExpose = await getElFormExpose()
|
||||||
|
|||||||
@@ -59,8 +59,15 @@ async function goLogin() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const passwordInputRef = ref<ComponentRef<typeof ElInput>>()
|
||||||
|
|
||||||
function handleShowForm(show = false) {
|
function handleShowForm(show = false) {
|
||||||
showDate.value = show
|
showDate.value = show
|
||||||
|
if (!show) {
|
||||||
|
requestAnimationFrame(() => {
|
||||||
|
passwordInputRef.value?.focus()
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@@ -102,6 +109,8 @@ function handleShowForm(show = false) {
|
|||||||
:placeholder="t('lock.placeholder')"
|
:placeholder="t('lock.placeholder')"
|
||||||
class="enter-x"
|
class="enter-x"
|
||||||
v-model="password"
|
v-model="password"
|
||||||
|
@keydown.enter="unLock"
|
||||||
|
ref="passwordInputRef"
|
||||||
/>
|
/>
|
||||||
<span :class="`text-14px ${prefixCls}-entry__err-msg enter-x`" v-if="errMsg">
|
<span :class="`text-14px ${prefixCls}-entry__err-msg enter-x`" v-if="errMsg">
|
||||||
{{ t('lock.message') }}
|
{{ t('lock.message') }}
|
||||||
|
|||||||
@@ -72,7 +72,13 @@ const schema = reactive<FormSchema[]>([
|
|||||||
style: {
|
style: {
|
||||||
width: '100%'
|
width: '100%'
|
||||||
},
|
},
|
||||||
placeholder: 'admin or test'
|
placeholder: 'admin or test',
|
||||||
|
// 按下enter键触发登录
|
||||||
|
onKeydown: (_e: any) => {
|
||||||
|
if (_e.key === 'Enter') {
|
||||||
|
signIn()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user