This commit is contained in:
dap
2024-12-18 13:32:33 +08:00
20 changed files with 186 additions and 29 deletions

View File

@@ -138,7 +138,11 @@ defineExpose({
<template>
<div
:class="
cn('col-span-full w-full pb-6 text-right', rootProps.actionWrapperClass)
cn(
'col-span-full w-full text-right',
rootProps.compact ? 'pb-2' : 'pb-6',
rootProps.actionWrapperClass,
)
"
:style="queryFormStyle"
>

View File

@@ -55,7 +55,7 @@ const values = useFormValues();
const errors = useFieldError(fieldName);
const fieldComponentRef = useTemplateRef<HTMLInputElement>('fieldComponentRef');
const formApi = formRenderProps.form;
const compact = formRenderProps.compact;
const isInValid = computed(() => errors.value?.length > 0);
const FieldComponent = computed(() => {
@@ -281,8 +281,10 @@ function autofocus() {
'form-valid-error': isInValid,
'flex-col': isVertical,
'flex-row items-center': !isVertical,
'pb-6': !compact,
'pb-2': compact,
}"
class="flex pb-6"
class="flex"
v-bind="$attrs"
>
<FormLabel

View File

@@ -273,6 +273,10 @@ export interface FormRenderProps<
* 表单项通用后备配置,当子项目没配置时使用这里的配置,子项目配置优先级高于此配置
*/
commonConfig?: FormCommonConfig;
/**
* 紧凑模式(移除表单每一项底部为校验信息预留的空间)
*/
compact?: boolean;
/**
* 组件v-model事件绑定
*/

View File

@@ -6,7 +6,7 @@ import type { ExtendedFormApi, VbenFormProps } from './types';
import { useForwardPriorityValues } from '@vben-core/composables';
// import { isFunction } from '@vben-core/shared/utils';
import { nextTick, onMounted, useTemplateRef, watch } from 'vue';
import { nextTick, onMounted, watch } from 'vue';
import { cloneDeep } from '@vben-core/shared/utils';
@@ -27,8 +27,6 @@ interface Props extends VbenFormProps {
const props = defineProps<Props>();
const formActionsRef = useTemplateRef<typeof FormActions>('formActionsRef');
const state = props.formApi?.useStore?.();
const forward = useForwardPriorityValues(props, state);
@@ -44,11 +42,7 @@ const handleUpdateCollapsed = (value: boolean) => {
};
function handleKeyDownEnter(event: KeyboardEvent) {
if (
!state.value.submitOnEnter ||
!formActionsRef.value ||
!formActionsRef.value.handleSubmit
) {
if (!state.value.submitOnEnter || !forward.value.formApi?.isMounted) {
return;
}
// 如果是 textarea 不阻止默认行为,否则会导致无法换行。
@@ -58,12 +52,12 @@ function handleKeyDownEnter(event: KeyboardEvent) {
}
event.preventDefault();
formActionsRef.value?.handleSubmit?.();
forward.value.formApi.validateAndSubmitForm();
}
const handleValuesChangeDebounced = useDebounceFn((newVal) => {
forward.value.handleValuesChange?.(cloneDeep(newVal));
state.value.submitOnChange && formActionsRef.value?.handleSubmit?.();
state.value.submitOnChange && forward.value.formApi?.validateAndSubmitForm();
}, 300);
onMounted(async () => {
@@ -94,7 +88,6 @@ onMounted(async () => {
<slot v-bind="slotProps">
<FormActions
v-if="forward.showDefaultActions"
ref="formActionsRef"
:model-value="state.collapsed"
@update:model-value="handleUpdateCollapsed"
>