Skip to content

Commit 865336a

Browse files
authored
fix: cannot use 'in' operator to search for "value" (#345)
* fix: cannot use 'in' operator to search for "value" close ant-design/ant-design#33380 * fix: tsc errors
1 parent ca2dbe7 commit 865336a

File tree

6 files changed

+39
-19
lines changed

6 files changed

+39
-19
lines changed

src/Field.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export interface InternalFieldProps<Values = any> {
7272
validateTrigger?: string | string[] | false;
7373
validateFirst?: boolean | 'parallel';
7474
valuePropName?: string;
75-
getValueProps?: (value: StoreValue) => object;
75+
getValueProps?: (value: StoreValue) => Record<string, unknown>;
7676
messageVariables?: Record<string, string>;
7777
initialValue?: any;
7878
onReset?: () => void;
@@ -265,12 +265,12 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
265265
}
266266
break;
267267

268-
/**
269-
* In case field with `preserve = false` nest deps like:
270-
* - A = 1 => show B
271-
* - B = 1 => show C
272-
* - Reset A, need clean B, C
273-
*/
268+
/**
269+
* In case field with `preserve = false` nest deps like:
270+
* - A = 1 => show B
271+
* - B = 1 => show C
272+
* - Reset A, need clean B, C
273+
*/
274274
case 'remove': {
275275
if (shouldUpdate) {
276276
this.reRender();

src/FormContext.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import * as React from 'react';
2-
import { ValidateMessages, FormInstance, FieldData, Store } from './interface';
2+
import type { ValidateMessages, FormInstance, FieldData, Store } from './interface';
33

4-
export interface Forms {
5-
[name: string]: FormInstance;
6-
}
4+
export type Forms = Record<string, FormInstance>;
75

86
export interface FormChangeInfo {
97
changedFields: FieldData[];

src/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ const InternalForm = React.forwardRef<FormInstance, FormProps>(FieldForm) as <Va
1212
props: React.PropsWithChildren<FormProps<Values>> & { ref?: React.Ref<FormInstance<Values>> },
1313
) => React.ReactElement;
1414

15-
type InternalForm = typeof InternalForm;
16-
interface RefForm extends InternalForm {
15+
type InternalFormType = typeof InternalForm;
16+
interface RefFormType extends InternalFormType {
1717
FormProvider: typeof FormProvider;
1818
Field: typeof Field;
1919
List: typeof List;
2020
useForm: typeof useForm;
2121
}
2222

23-
const RefForm: RefForm = InternalForm as RefForm;
23+
const RefForm: RefFormType = InternalForm as RefFormType;
2424

2525
RefForm.FormProvider = FormProvider;
2626
RefForm.Field = Field;

src/interface.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,13 @@ type RecursivePartial<T> = T extends object
220220
export interface FormInstance<Values = any> {
221221
// Origin Form API
222222
getFieldValue: (name: NamePath) => StoreValue;
223-
getFieldsValue(): Values;
224-
getFieldsValue(nameList: NamePath[] | true, filterFunc?: (meta: Meta) => boolean): any;
223+
getFieldsValue: (() => Values) &
224+
((nameList: NamePath[] | true, filterFunc?: (meta: Meta) => boolean) => any);
225225
getFieldError: (name: NamePath) => string[];
226226
getFieldsError: (nameList?: NamePath[]) => FieldError[];
227227
getFieldWarning: (name: NamePath) => string[];
228-
isFieldsTouched(nameList?: NamePath[], allFieldsTouched?: boolean): boolean;
229-
isFieldsTouched(allFieldsTouched?: boolean): boolean;
228+
isFieldsTouched: ((nameList?: NamePath[], allFieldsTouched?: boolean) => boolean) &
229+
((allFieldsTouched?: boolean) => boolean);
230230
isFieldTouched: (name: NamePath) => boolean;
231231
isFieldValidating: (name: NamePath) => boolean;
232232
isFieldsValidating: (nameList: NamePath[]) => boolean;

src/utils/valueUtil.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ export function isSimilar(source: SimilarObject, target: SimilarObject) {
119119

120120
export function defaultGetValueFromEvent(valuePropName: string, ...args: EventArgs) {
121121
const event = args[0];
122-
if (event && event.target && valuePropName in event.target) {
122+
if (event && event.target && typeof event.target === 'object' && valuePropName in event.target) {
123123
return (event.target as HTMLInputElement)[valuePropName];
124124
}
125125

tests/index.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,4 +704,26 @@ describe('Form.Basic', () => {
704704
});
705705
expect(form.getFieldsValue(['password'], meta => meta.touched)).toEqual({});
706706
});
707+
708+
it('should not crash when return value contains target field', async () => {
709+
const CustomInput = ({ value, onChange }) => {
710+
const onInputChange = e => {
711+
onChange({
712+
value: e.target.value,
713+
target: 'string',
714+
});
715+
};
716+
return <Input value={value} onChange={onInputChange} />;
717+
};
718+
const wrapper = mount(
719+
<Form>
720+
<Field name="user">
721+
<CustomInput />
722+
</Field>
723+
</Form>,
724+
);
725+
expect(() => {
726+
wrapper.find('Input').simulate('change', { event: { target: { value: 'Light' } } });
727+
}).not.toThrowError();
728+
});
707729
});

0 commit comments

Comments
 (0)