Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add form field autofocus configuration #4550

Merged
merged 32 commits into from
Oct 3, 2024
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
4b460d4
feat: captcha example
Squall2017 Sep 6, 2024
1689c27
Merge pull request #1 from vbenjs/main
Squall2017 Sep 6, 2024
d973765
fix: fix lint errors
Squall2017 Sep 6, 2024
a2e56d8
chore: event handling and methods
Squall2017 Sep 7, 2024
185c623
chore: add accessibility features ARIA labels and roles
Squall2017 Sep 7, 2024
f6d4351
Merge branch 'main' into main
vince292007 Sep 7, 2024
04cde6a
Merge branch 'main' of github.com:vbenjs/vue-vben-admin into vbenjs-main
Squall2017 Sep 9, 2024
2d44bd8
Merge branch 'vbenjs-main'
Squall2017 Sep 9, 2024
18ee871
Merge pull request #3 from vbenjs/main
Squall2017 Sep 12, 2024
b06b57a
Merge pull request #4 from vbenjs/main
Squall2017 Sep 12, 2024
d95633a
refactor: refactor code structure and improve captcha demo page
Squall2017 Sep 13, 2024
c55375d
feat: add captcha internationalization
Squall2017 Sep 13, 2024
fe2080d
chore: 适配时间戳国际化展示
Squall2017 Sep 14, 2024
c202641
fix: 1. 添加点击位置边界校验,防止点击外部导致x,y误差。2. 演示页面宽度过长添加滚动条。3. 添加hooks
Squall2017 Sep 14, 2024
0ceabbe
Merge branch 'main' of github.com:vbenjs/vue-vben-admin
Squall2017 Sep 14, 2024
54e3d70
Merge branch 'main' into main
vince292007 Sep 14, 2024
a490762
Merge branch 'main' of github.com:vbenjs/vue-vben-admin into vbenjs-main
Squall2017 Sep 21, 2024
f956412
Merge branch 'vbenjs-main'
Squall2017 Sep 21, 2024
b281534
Merge branch 'vbenjs:main' into main
Squall2017 Sep 23, 2024
e5e5a10
Merge branch 'vbenjs:main' into main
Squall2017 Sep 23, 2024
14b12f2
feat: sync test
Squall2017 Sep 23, 2024
5c63056
feat: 代码合并
Squall2017 Sep 24, 2024
344eb5f
feat: 添加合并表单功能
Squall2017 Sep 24, 2024
87eeb79
fix: 修复上一步不展示问题
Squall2017 Sep 24, 2024
944b1d1
Merge branch 'vbenjs:main' into main
Squall2017 Sep 25, 2024
78fae01
Merge branch 'vbenjs:main' into main
Squall2017 Sep 27, 2024
d8bbe6f
Merge branch 'vbenjs:main' into main
Squall2017 Sep 29, 2024
5887043
fix: 修复当全局页面出现滚动条时,锁屏页面异常显示
Squall2017 Sep 29, 2024
5644556
fix: 使用 useScrollLock 并将 tryOnBeforeMount 替换为 tryOnMounted,确保页面刷新后 sc…
Squall2017 Sep 29, 2024
d7c2747
Merge branch 'vbenjs:main' into main
Squall2017 Sep 29, 2024
b73ff5a
Merge branch 'vbenjs:main' into main
Squall2017 Sep 30, 2024
eca5e6d
feat: 添加表单字段聚焦(autofocus)配置。
Squall2017 Sep 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion packages/@core/ui-kit/form-ui/src/form-render/form-field.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { ZodType } from 'zod';

import type { FormSchema, MaybeComponentProps } from '../types';

import { computed } from 'vue';
import { computed, nextTick, ref, watch } from 'vue';

import {
FormControl,
Expand Down Expand Up @@ -52,6 +52,16 @@ const errors = useFieldError(fieldName);
const formApi = formRenderProps.form;

const isInValid = computed(() => errors.value?.length > 0);
const fieldComponentRef = ref<HTMLInputElement | null>(null);
const focus = () => {
if (
fieldComponentRef.value &&
typeof fieldComponentRef.value.focus === 'function' &&
document.activeElement !== fieldComponentRef.value // 检查当前是否有元素被聚焦
) {
fieldComponentRef.value.focus();
}
};

const fieldComponent = computed(() => {
const finalComponent = isString(component)
Expand Down Expand Up @@ -156,6 +166,18 @@ const computedProps = computed(() => {
};
});

watch(
() => computedProps.value?.autofocus,
(value) => {
if (value === true) {
nextTick(() => {
focus();
});
}
},
{ immediate: true },
);

const shouldDisabled = computed(() => {
return isDisabled.value || disabled || computedProps.value?.disabled;
});
Expand Down Expand Up @@ -275,6 +297,7 @@ function createComponentProps(slotProps: Record<string, any>) {
>
<component
:is="fieldComponent"
ref="fieldComponentRef"
Squall2017 marked this conversation as resolved.
Show resolved Hide resolved
:class="{
'border-destructive focus:border-destructive hover:border-destructive/80 focus:shadow-[0_0_0_2px_rgba(255,38,5,0.06)]':
isInValid,
Expand Down