Commit 9b2d41ea44ed0da4dde22856bf23b52748244642

Authored by Netfan
Committed by GitHub
1 parent 3bb6d11e

feat(form): add prop autoSubmitOnEnter (#620)

为Form添加autoSubmitOnEnter属性,当设置为true时,可以在input组件上按下回车时自动提交表单
src/components/Form/src/BasicForm.vue
1 <template> 1 <template>
2 - <Form v-bind="{ ...$attrs, ...$props }" :class="getFormClass" ref="formElRef" :model="formModel"> 2 + <Form
  3 + v-bind="{ ...$attrs, ...$props }"
  4 + :class="getFormClass"
  5 + ref="formElRef"
  6 + :model="formModel"
  7 + @keypress.enter="handleEnterPress"
  8 + >
3 <Row :style="getRowWrapStyle"> 9 <Row :style="getRowWrapStyle">
4 <slot name="formHeader"></slot> 10 <slot name="formHeader"></slot>
5 <template v-for="schema in getSchema" :key="schema.field"> 11 <template v-for="schema in getSchema" :key="schema.field">
@@ -81,11 +87,9 @@ @@ -81,11 +87,9 @@
81 const { prefixCls } = useDesign('basic-form'); 87 const { prefixCls } = useDesign('basic-form');
82 88
83 // Get the basic configuration of the form 89 // Get the basic configuration of the form
84 - const getProps = computed(  
85 - (): FormProps => {  
86 - return { ...props, ...unref(propsRef) } as FormProps;  
87 - }  
88 - ); 90 + const getProps = computed((): FormProps => {
  91 + return { ...props, ...unref(propsRef) } as FormProps;
  92 + });
89 93
90 const getFormClass = computed(() => { 94 const getFormClass = computed(() => {
91 return [ 95 return [
@@ -97,12 +101,10 @@ @@ -97,12 +101,10 @@
97 }); 101 });
98 102
99 // Get uniform row style 103 // Get uniform row style
100 - const getRowWrapStyle = computed(  
101 - (): CSSProperties => {  
102 - const { baseRowStyle = {} } = unref(getProps);  
103 - return baseRowStyle;  
104 - }  
105 - ); 104 + const getRowWrapStyle = computed((): CSSProperties => {
  105 + const { baseRowStyle = {} } = unref(getProps);
  106 + return baseRowStyle;
  107 + });
106 108
107 const getSchema = computed((): FormSchema[] => { 109 const getSchema = computed((): FormSchema[] => {
108 const schemas: FormSchema[] = unref(schemaRef) || (unref(getProps).schemas as any); 110 const schemas: FormSchema[] = unref(schemaRef) || (unref(getProps).schemas as any);
@@ -213,6 +215,17 @@ @@ -213,6 +215,17 @@
213 formModel[key] = value; 215 formModel[key] = value;
214 } 216 }
215 217
  218 + function handleEnterPress(e: KeyboardEvent) {
  219 + const { autoSubmitOnEnter } = unref(getProps);
  220 + if (!autoSubmitOnEnter) return;
  221 + if (e.key === 'Enter' && e.target && e.target instanceof HTMLElement) {
  222 + const target: HTMLElement = e.target as HTMLElement;
  223 + if (target && target.tagName && target.tagName.toUpperCase() == 'INPUT') {
  224 + handleSubmit();
  225 + }
  226 + }
  227 + }
  228 +
216 const formActionType: Partial<FormActionType> = { 229 const formActionType: Partial<FormActionType> = {
217 getFieldsValue, 230 getFieldsValue,
218 setFieldsValue, 231 setFieldsValue,
@@ -236,6 +249,7 @@ @@ -236,6 +249,7 @@
236 249
237 return { 250 return {
238 handleToggleAdvanced, 251 handleToggleAdvanced,
  252 + handleEnterPress,
239 formModel, 253 formModel,
240 defaultValueRef, 254 defaultValueRef,
241 advanceState, 255 advanceState,
src/components/Form/src/props.ts
@@ -37,6 +37,8 @@ export const basicProps = { @@ -37,6 +37,8 @@ export const basicProps = {
37 type: Object as PropType<Partial<ColEx>>, 37 type: Object as PropType<Partial<ColEx>>,
38 }, 38 },
39 autoSetPlaceHolder: propTypes.bool.def(true), 39 autoSetPlaceHolder: propTypes.bool.def(true),
  40 + // 在INPUT组件上单击回车时,是否自动提交
  41 + autoSubmitOnEnter: propTypes.bool.def(false),
40 submitOnReset: propTypes.bool, 42 submitOnReset: propTypes.bool,
41 size: propTypes.oneOf(['default', 'small', 'large']).def('default'), 43 size: propTypes.oneOf(['default', 'small', 'large']).def('default'),
42 // 禁用表单 44 // 禁用表单
src/components/Form/src/types/form.ts
@@ -83,6 +83,8 @@ export interface FormProps { @@ -83,6 +83,8 @@ export interface FormProps {
83 fieldMapToTime?: FieldMapToTime; 83 fieldMapToTime?: FieldMapToTime;
84 // Placeholder is set automatically 84 // Placeholder is set automatically
85 autoSetPlaceHolder?: boolean; 85 autoSetPlaceHolder?: boolean;
  86 + // Auto submit on press enter on input
  87 + autoSubmitOnEnter?: boolean;
86 // Check whether the information is added to the label 88 // Check whether the information is added to the label
87 rulesMessageJoinLabel?: boolean; 89 rulesMessageJoinLabel?: boolean;
88 // Whether to show collapse and expand buttons 90 // Whether to show collapse and expand buttons
@@ -125,7 +127,10 @@ export interface FormSchema { @@ -125,7 +127,10 @@ export interface FormSchema {
125 // Auxiliary text 127 // Auxiliary text
126 subLabel?: string; 128 subLabel?: string;
127 // Help text on the right side of the text 129 // Help text on the right side of the text
128 - helpMessage?: string | string[] | ((renderCallbackParams: RenderCallbackParams) => string | string[]); 130 + helpMessage?:
  131 + | string
  132 + | string[]
  133 + | ((renderCallbackParams: RenderCallbackParams) => string | string[]);
129 // BaseHelp component props 134 // BaseHelp component props
130 helpComponentProps?: Partial<HelpComponentProps>; 135 helpComponentProps?: Partial<HelpComponentProps>;
131 // Label width, if it is passed, the labelCol and WrapperCol configured by itemProps will be invalid 136 // Label width, if it is passed, the labelCol and WrapperCol configured by itemProps will be invalid