Skip to content

Commit

Permalink
feat: test
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyair committed Mar 14, 2024
1 parent a59e846 commit ebb6a7b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 41 deletions.
53 changes: 16 additions & 37 deletions src/Field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -196,24 +196,11 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
};

// ================================== Utils ==================================
public getNamePath = (name?: InternalNamePath): InternalNamePath => {
const { name: propsName, fieldContext } = this.props;
const _name = name ?? propsName;

const { prefixName = [] }: InternalFormInstance = fieldContext;

return _name !== undefined ? [...prefixName, ..._name] : [];
};

public getNamesPath = (): InternalNamePath | InternalNamePath[] => {
const { name, names, fieldContext } = this.props;

public getNamePath = (): InternalNamePath => {
const { name, fieldContext } = this.props;
const { prefixName = [] }: InternalFormInstance = fieldContext;
if (name) {
return name !== undefined ? [...prefixName, ...name] : [];
}

return names !== undefined ? names.map(name => [...prefixName, ...name]) : [];
return name !== undefined ? [...prefixName, ...name] : [];
};

public getRules = (): RuleObject[] => {
Expand Down Expand Up @@ -569,15 +556,9 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F

// ============================== Field Control ==============================
public getValue = (store?: Store) => {
const { names } = this.props;
const { getFieldsValue }: FormInstance = this.props.fieldContext;
if (names) {
const namesValue = names.map(name => {
return getValue(store || getFieldsValue(true), this.getNamePath(name));
});
return namesValue;
}
return getValue(store || getFieldsValue(true), this.getNamePath());
const namePath = this.getNamePath();
return getValue(store || getFieldsValue(true), namePath);
};

public getControlled = (childProps: ChildProps = {}) => {
Expand All @@ -594,7 +575,7 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
const mergedValidateTrigger =
validateTrigger !== undefined ? validateTrigger : fieldContext.validateTrigger;

const namesPath = this.getNamesPath();
const namePath = this.getNamePath();
const { getInternalHooks, getFieldsValue }: InternalFormInstance = fieldContext;
const { dispatch } = getInternalHooks(HOOK_MARK);
const value = this.getValue();
Expand All @@ -614,6 +595,7 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
);
});
}

const control = {
...childProps,
...valueProps,
Expand All @@ -637,12 +619,11 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
if (normalize) {
newValue = normalize(newValue, value, getFieldsValue(true));
}
namesPath.forEach((name, index) => {
dispatch({
type: 'updateValue',
namePath: this.getNamePath(name),
value: newValue[index],
});

dispatch({
type: 'updateValue',
namePath,
value: newValue,
});

if (originTriggerFunc) {
Expand All @@ -666,12 +647,10 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
if (rules && rules.length) {
// We dispatch validate to root,
// since it will update related data with other field with same name
namesPath.forEach(name => {
dispatch({
type: 'validateField',
namePath: this.getNamePath(name),
triggerName,
});
dispatch({
type: 'validateField',
namePath,
triggerName,
});
}
};
Expand Down
7 changes: 3 additions & 4 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ export interface FieldEntity {
isPreserve: () => boolean;
validateRules: (options?: InternalValidateOptions) => Promise<RuleError[]>;
getMeta: () => Meta;
getNamePath: (name?: InternalNamePath) => InternalNamePath;
getNamesPath: () => InternalNamePath | InternalNamePath[];
getNamePath: () => InternalNamePath;
getErrors: () => string[];
getWarnings: () => string[];
props: {
Expand Down Expand Up @@ -241,8 +240,8 @@ type RecursivePartial<T> = NonNullable<T> extends object
[P in keyof T]?: NonNullable<T[P]> extends (infer U)[]
? RecursivePartial<U>[]
: NonNullable<T[P]> extends object
? RecursivePartial<T[P]>
: T[P];
? RecursivePartial<T[P]>
: T[P];
}
: T;

Expand Down

0 comments on commit ebb6a7b

Please sign in to comment.