Skip to content

Commit 4f35868

Browse files
author
李磊
committed
fix: from bug
1 parent 4cfdcab commit 4f35868

File tree

7 files changed

+73
-43
lines changed

7 files changed

+73
-43
lines changed

packages/lint-configs/eslint-config/index.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export default tseslint.config(
6565
{ argsIgnorePattern: '^_', varsIgnorePattern: '^_' },
6666
], // 禁止未使用的变量,但允许以 _ 开头的变量
6767
'no-unused-vars': 'off', // 关闭原生 ESLint 规则,避免冲突
68+
'@typescript-eslint/no-explicit-any': 'off', // 禁止使用 any 类型
6869
},
6970
},
7071
);

packages/ui/src/components/Form/hooks/useFormMethods.ts

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
omit,
1111
set,
1212
} from '@ssuperlilei/utils';
13-
import { DefineComponent, unref } from 'vue';
13+
import { type DefineComponent, unref } from 'vue';
1414
import type { FormProps } from '../types/ll-form';
1515
import type { FormState } from './useFormState';
1616

@@ -163,7 +163,7 @@ export const useFormMethods = (formMethodsContext: UseFormMethodsContext) => {
163163
continue;
164164
}
165165

166-
const [startTime, endTime]: string[] = values[field];
166+
const [startTime, endTime]: string[] = values[field] as string[];
167167

168168
values[startTimeKey] = dateUtil(startTime).format(format);
169169
values[endTimeKey] = dateUtil(endTime).format(format);
@@ -173,23 +173,33 @@ export const useFormMethods = (formMethodsContext: UseFormMethodsContext) => {
173173
return values;
174174
}
175175

176+
const setNestedFormModel = (formModel: Recordable, keys: string[], defaultValue: any) => {
177+
let current: Recordable = formModel;
178+
179+
for (let i = 0; i < keys.length; i++) {
180+
const key = keys[i];
181+
if (!current[key] || !isObject(current[key])) {
182+
current[key] = {};
183+
}
184+
if (i === keys.length - 1) {
185+
current[key] = defaultValue;
186+
}
187+
current = current[key] as Recordable;
188+
}
189+
};
190+
176191
/**
177192
* @description 初始化表单数据
178193
*/
179194
const initFormValues = (initialValues?: Recordable) => {
180195
try {
181-
unref(formPropsRef).schemas?.forEach((item: { field?: any; defaultValue?: any }) => {
196+
unref(formPropsRef).schemas?.forEach((item: { field: string; defaultValue?: any }) => {
182197
const { defaultValue } = item;
183198
const namePath = isArray(item.field) ? item.field : item.field.split('.');
184199
if (namePath.length > 1) {
185-
const target = namePath
186-
.slice(1)
187-
.reduce((prev: any, item: any) => (prev[item] ??= {}), {});
188-
target[namePath.slice(1)[0]] = defaultValue;
189-
const prop = namePath.shift()!;
190-
formModel[prop] = target;
191-
cacheFormModel[prop] = target;
192-
defaultFormValues[prop] = target;
200+
setNestedFormModel(formModel, namePath, defaultValue);
201+
setNestedFormModel(cacheFormModel, namePath, defaultValue);
202+
setNestedFormModel(defaultFormValues, namePath, defaultValue);
193203
} else if (!isNullOrUnDef(defaultValue)) {
194204
formModel[item.field] = defaultValue;
195205
cacheFormModel[item.field] = defaultValue;
@@ -205,7 +215,10 @@ export const useFormMethods = (formMethodsContext: UseFormMethodsContext) => {
205215
formModel[key] = !isNullOrUnDef(initialValues[key]) ? initialValues[key] : undefined;
206216
});
207217
}
208-
} catch (error) {}
218+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
219+
} catch (_error) {
220+
//
221+
}
209222
};
210223

211224
return {

packages/ui/src/components/Form/hooks/useFormState.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
import { cloneDeep, isFunction } from '@ssuperlilei/utils';
22
import type { FormInstance } from 'ant-design-vue';
3-
import type { DefineComponent, SetupContext } from 'vue';
4-
import { computed, reactive, ref, unref, watch, watchEffect } from 'vue';
3+
import {
4+
type DefineComponent,
5+
type SetupContext,
6+
computed,
7+
reactive,
8+
ref,
9+
unref,
10+
watch,
11+
watchEffect,
12+
} from 'vue';
513
import { ActionColOptions } from '../enums/common';
6-
import type { ComponentProps, RenderCallbackParams } from '../types';
14+
import type { ComponentProps, FormSchema, RenderCallbackParams } from '../types';
715
import type { FormProps } from '../types/ll-form';
816
import type { AdvanceState } from '../types/hooks';
917

@@ -42,7 +50,7 @@ export const useFormState = ({ props, attrs }: useFormStateParams): any => {
4250
// 表单实例
4351
const lFormRef = ref<FormInstance>();
4452
// 将所有的表单组件实例保存起来
45-
const compRefMap = new Map<string, DefineComponent<any>>();
53+
const compRefMap = new Map<string, DefineComponent<unknown>>();
4654
// 初始时的componentProps,用于updateSchema更新时不覆盖componentProps为函数时的值
4755
const originComponentPropsFnMap = new Map<
4856
string,
@@ -77,7 +85,9 @@ export const useFormState = ({ props, attrs }: useFormStateParams): any => {
7785
(): Recordable => ({ ...getFormProps.value, ...advanceState }),
7886
);
7987
watchEffect(() => {
80-
formPropsRef.value.schemas?.forEach((item: any) => {
88+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
89+
// @ts-expect-error
90+
formPropsRef.value.schemas?.forEach((item: FormSchema) => {
8191
if (isFunction(item.componentProps)) {
8292
originComponentPropsFnMap.set(item.field, item.componentProps);
8393
}

packages/ui/src/components/Form/types/form.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import type { RowProps } from 'ant-design-vue';
22
import type { FormItemProps } from 'ant-design-vue/es/form/FormItem';
33
import type { NamePath, RuleObject } from 'ant-design-vue/es/form/interface';
44
import type { Component, HTMLAttributes, VNode } from 'vue';
5-
import { JSX } from 'vue/jsx-runtime';
6-
import { LFormType } from '../hooks';
5+
import type { JSX } from 'vue/jsx-runtime';
6+
import type { LFormType } from '../hooks';
77
import type { FormInstance } from './ll-form';
88
import type { ColEx, ComponentMapType, ComponentProps } from './component';
99

@@ -20,16 +20,16 @@ export type GetFieldKeys<T> = Exclude<keyof T, symbol | number>;
2020

2121
export interface RenderCallbackParams<T = string> {
2222
schema: FormSchema<T>;
23-
formModel: T extends string ? Recordable : Record<GetFieldKeys<T>, any>;
23+
formModel: T extends string ? Recordable : Record<GetFieldKeys<T>, unknown>;
2424
field: T extends string ? string : GetFieldKeys<T>;
25-
values: any;
25+
values: unknown;
2626
/** 动态表单实例 */
2727
formInstance: LFormType;
2828
/** 作用域插槽数据 */
2929
slotData?: Recordable;
3030
}
3131
/** 自定义VNode渲染器 */
32-
export type CustomRenderFn<T = any> = (
32+
export type CustomRenderFn<T = unknown> = (
3333
opt: RenderCallbackParams<T>,
3434
) => Component | VNode | VNode[] | string | JSX.Element | Element;
3535

@@ -38,7 +38,7 @@ export interface FormActionType {
3838
submit: () => Promise<void>;
3939
setFieldsValue: <T>(values: T) => Promise<void>;
4040
resetForm: () => Promise<void>;
41-
getFormValues: () => any;
41+
getFormValues: () => unknown;
4242
clearValidate: (name?: string | string[]) => Promise<void>;
4343
updateSchema: (data: Partial<FormSchema> | Partial<FormSchema>[]) => Promise<void>;
4444
resetSchema: (data: Partial<FormSchema> | Partial<FormSchema>[]) => Promise<void>;
@@ -49,8 +49,8 @@ export interface FormActionType {
4949
prefixField: string | undefined,
5050
first?: boolean | undefined,
5151
) => Promise<void>;
52-
validateFields: (nameList?: NamePath[]) => Promise<any>;
53-
validate: (nameList?: NamePath[]) => Promise<any>;
52+
validateFields: (nameList?: NamePath[]) => Promise<unknown>;
53+
validate: (nameList?: NamePath[]) => Promise<unknown>;
5454
scrollToField: (name: NamePath, options?: ScrollOptions) => Promise<void>;
5555
}
5656

@@ -113,7 +113,7 @@ export interface FormSchema<T = string> {
113113
/** 搜索表单项排序 */
114114
order?: number;
115115
// 默认值
116-
defaultValue?: any;
116+
defaultValue?: unknown;
117117
isAdvanced?: boolean;
118118
// label 是否显示铺满
119119
labelFullWidth?: boolean;
@@ -155,13 +155,13 @@ export interface HelpComponentProps {
155155
// Whether to display the serial number
156156
showIndex: boolean;
157157
// Text list
158-
text: any;
158+
text: unknown;
159159
// colour
160160
color: string;
161161
// font size
162162
fontSize: string;
163163
icon: string;
164164
absolute: boolean;
165165
// Positioning
166-
position: any;
166+
position: unknown;
167167
}

packages/ui/src/components/ModalForm/hooks/useModalFormMethods.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { deepMerge } from '@ssuperlilei/utils';
2-
import { ModalFormEmitFn, type ModalFormProps } from '../types';
3-
import { ModalFormState } from './useModalFormState';
2+
import type { ModalFormEmitFn, ModalFormProps } from '../types';
3+
import type { ModalFormState } from './useModalFormState';
44
import { unref } from 'vue';
55

66
export type ModalFormMethods = ReturnType<typeof useModalFormMethods>;
@@ -79,6 +79,7 @@ export function useModalFormMethods(modalFormActionContext: UseModalFormActionCo
7979
* @description 内部打开弹窗
8080
*/
8181
const innerOpenModal = (e: any) => {
82+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
8283
modalFormPropsRef.value.preventDefault && e.preventDefault();
8384
innerOpen.value = true;
8485
};

packages/ui/src/components/ModalForm/hooks/useModalFormState.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { cloneDeep, deepMerge } from '@ssuperlilei/utils';
2-
import { SetupContext, computed, ref, watch, nextTick } from 'vue';
3-
import { FormProps, FormInstance } from '../../Form';
4-
import type { ModalFormEmitFn, ModalFormProps } from '../types';
5-
import { defaultFormProps } from '../types';
2+
import { type SetupContext, computed, nextTick, ref, watch } from 'vue';
3+
import type { FormInstance, FormProps } from '../../Form';
4+
import { type ModalFormEmitFn, type ModalFormProps, defaultFormProps } from '../types';
65
import { ModalSize } from '~/components';
76

87
export type useModalFormStateParams = {
@@ -17,7 +16,7 @@ export const useModalFormState = ({ props, attrs, emit }: useModalFormStateParam
1716
const cloneProps = cloneDeep(props);
1817
const modalFormPropsRef = ref<ModalFormProps>(cloneProps as ModalFormProps);
1918
// modalForm 实例
20-
const modalFormRef = ref<any>();
19+
const modalFormRef = ref<Recordable>();
2120
// 表单实例
2221
const formRef = ref<FormInstance | null>();
2322
// 内部控制 modal 的显示
@@ -31,6 +30,14 @@ export const useModalFormState = ({ props, attrs, emit }: useModalFormStateParam
3130
emit('update:open', val as boolean);
3231
},
3332
});
33+
// 监听 内部控制 modal 的显示
34+
watch(
35+
() => innerOpen.value,
36+
(v) => {
37+
emit('update:open', v as boolean);
38+
},
39+
{ immediate: true },
40+
);
3441

3542
// props.open 是否为 undefined
3643
const openUndefined = ref<boolean>(false);
@@ -63,12 +70,11 @@ export const useModalFormState = ({ props, attrs, emit }: useModalFormStateParam
6370
(v) => {
6471
if (v && props.formProps) {
6572
modalFormPropsRef.value = cloneDeep(props);
66-
const newDefaultFormProps: Record<any, any> = cloneDeep(defaultFormProps);
73+
const newDefaultFormProps: Record<string, unknown> = cloneDeep(defaultFormProps);
6774
const formProps = deepMerge(newDefaultFormProps, {
6875
...v.formProps,
69-
}) as FormProps;
70-
// @ts-ignore
71-
modalFormPropsRef.value.formProps = formProps;
76+
}) as unknown as FormProps;
77+
modalFormPropsRef.value.formProps = formProps as any;
7278
}
7379
},
7480
{ immediate: true, deep: true },
@@ -113,7 +119,7 @@ export const useModalFormState = ({ props, attrs, emit }: useModalFormStateParam
113119
async (v) => {
114120
if (v) {
115121
await nextTick();
116-
formRef.value?.resetForm();
122+
formRef.value?.resetForm?.();
117123
}
118124
},
119125
{ immediate: false },

packages/ui/src/components/ModalForm/types/modal-form.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { isObject } from '@ssuperlilei/utils';
2-
import { ButtonProps } from 'ant-design-vue';
2+
import type { ButtonProps } from 'ant-design-vue';
33
import { modalProps } from 'ant-design-vue/es/modal/Modal';
44
import type { ComponentInternalInstance, ExtractPropTypes, PropType } from 'vue';
5-
import { FormProps } from '../../Form';
5+
import type { FormProps } from '../../Form';
66
import { initDefaultProps } from '~/_utils';
77
import type ModalForm from '../index.vue';
88

@@ -106,5 +106,4 @@ export type ModalFormEmits = typeof modalFormEmits;
106106

107107
export type ModalFormEmitFn = EmitFn<ModalFormEmits>;
108108

109-
// @ts-ignore:next-line
110109
export type ModalFormInstance = InstanceType<typeof ModalForm>;

0 commit comments

Comments
 (0)