Browse Source

feat: add submitOnEnter configuration to form (#4670)

pull/4673/head
Vben 2 years ago
committed by GitHub
parent
commit
6cd9937c03
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 1
      docs/src/components/common-ui/vben-form.md
  2. 5
      packages/@core/ui-kit/form-ui/src/components/form-actions.vue
  3. 1
      packages/@core/ui-kit/form-ui/src/form-api.ts
  4. 7
      packages/@core/ui-kit/form-ui/src/types.ts
  5. 13
      packages/@core/ui-kit/form-ui/src/vben-use-form.vue
  6. 1
      playground/src/views/examples/form/basic.vue
  7. 2
      playground/src/views/examples/vxe-table/form.vue

1
docs/src/components/common-ui/vben-form.md

@ -311,6 +311,7 @@ useVbenForm 返回的第二个参数,是一个对象,包含了一些表单
| collapsedRows | 折叠时保持的行数 | `number` | `1` |
| commonConfig | 表单项的通用配置,每个配置都会传递到每个表单项,表单项可覆盖 | `FormCommonConfig` | - |
| schema | 表单项的每一项配置 | `FormSchema` | - |
| submitOnEnter | 按下回车健时提交表单 | `boolean` | false |
### TS 类型说明

5
packages/@core/ui-kit/form-ui/src/components/form-actions.vue

@ -75,6 +75,11 @@ watch(
}
},
);
defineExpose({
handleReset,
handleSubmit,
});
</script>
<template>
<div

1
packages/@core/ui-kit/form-ui/src/form-api.ts

@ -35,6 +35,7 @@ function getDefaultState(): VbenFormProps {
showCollapseButton: false,
showDefaultActions: true,
submitButtonOptions: {},
submitOnEnter: false,
wrapperClass: 'grid-cols-1',
};
}

7
packages/@core/ui-kit/form-ui/src/types.ts

@ -319,7 +319,6 @@ export interface VbenFormProps<
*
*/
resetButtonOptions?: ActionButtonOptions;
/**
*
* @default true
@ -330,6 +329,12 @@ export interface VbenFormProps<
*
*/
submitButtonOptions?: ActionButtonOptions;
/**
*
* @default false
*/
submitOnEnter?: boolean;
}
export type ExtendedFormApi = {

13
packages/@core/ui-kit/form-ui/src/vben-use-form.vue

@ -6,6 +6,8 @@ import type { ExtendedFormApi, VbenFormProps } from './types';
import { useForwardPriorityValues } from '@vben-core/composables';
// import { isFunction } from '@vben-core/shared/utils';
import { useTemplateRef } from 'vue';
import FormActions from './components/form-actions.vue';
import {
COMPONENT_BIND_EVENT_MAP,
@ -21,6 +23,8 @@ interface Props extends VbenFormProps {
const props = defineProps<Props>();
const formActionsRef = useTemplateRef<typeof FormActions>('formActionsRef');
const state = props.formApi?.useStore?.();
const forward = useForwardPriorityValues(props, state);
@ -34,10 +38,18 @@ props.formApi?.mount?.(form);
const handleUpdateCollapsed = (value: boolean) => {
props.formApi?.setState({ collapsed: !!value });
};
function handleKeyDownEnter() {
if (!state.value.submitOnEnter || !formActionsRef.value) {
return;
}
formActionsRef.value?.handleSubmit?.();
}
</script>
<template>
<Form
@keydown.enter.prevent="handleKeyDownEnter"
v-bind="forward"
:collapsed="state.collapsed"
:component-bind-event-map="COMPONENT_BIND_EVENT_MAP"
@ -56,6 +68,7 @@ const handleUpdateCollapsed = (value: boolean) => {
<slot v-bind="slotProps">
<FormActions
v-if="forward.showDefaultActions"
ref="formActionsRef"
:model-value="state.collapsed"
@update:model-value="handleUpdateCollapsed"
>

1
playground/src/views/examples/form/basic.vue

@ -16,6 +16,7 @@ const [BaseForm, baseFormApi] = useVbenForm({
class: 'w-full',
},
},
//
handleSubmit: onSubmit,
// labelinputvertical

2
playground/src/views/examples/vxe-table/form.vue

@ -65,6 +65,8 @@ const formOptions: VbenFormProps = {
],
//
showCollapseButton: true,
//
submitOnEnter: false,
};
const gridOptions: VxeGridProps<RowType> = {

Loading…
Cancel
Save