Skip to content

Commit

Permalink
feat: test
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyair committed Apr 12, 2024
1 parent f5efa8f commit a45cb75
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
27 changes: 14 additions & 13 deletions src/Field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,12 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
return getValue(store || getFieldsValue(true), namePath);
};

public getValues = (store?: Store) => {
const { getFieldsValue }: FormInstance = this.props.fieldContext;
const namesPath = this.getNamesPath();
return namesPath.map(namePath => getValue(store || getFieldsValue(true), namePath));
};

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

const namePath = this.getNamePath();
const namesPath = this.getNamesPath();
const { getInternalHooks, getFieldsValue }: InternalFormInstance = fieldContext;
const { dispatch } = getInternalHooks(HOOK_MARK);
const value = this.getValue();
const values = this.getValues();
const mergedGetValueProps = getValueProps || ((val: StoreValue) => ({ [valuePropName]: val }));

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const originTriggerFunc: any = childProps[trigger];

const valueProps = name !== undefined ? mergedGetValueProps(value) : {};
const valueProps = name !== undefined ? mergedGetValueProps(values) : {};
console.log('valueProps', valueProps);

// warning when prop value is function
if (process.env.NODE_ENV !== 'production' && valueProps) {
Expand Down Expand Up @@ -625,14 +632,10 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
}

if (normalize) {
newValue = normalize(newValue, value, getFieldsValue(true));
newValue = normalize(newValue, values, getFieldsValue(true));
}

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

if (originTriggerFunc) {
originTriggerFunc(...args);
Expand All @@ -655,10 +658,8 @@ 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
dispatch({
type: 'validateField',
namePath,
triggerName,
namesPath.forEach(namePath => {
dispatch({ type: 'validateField', namePath, triggerName });
});
}
};
Expand Down
15 changes: 14 additions & 1 deletion src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,19 @@ interface UpdateAction {
value: StoreValue;
}

interface UpdateValuesAction {
type: 'updateValues';
namesPath: InternalNamePath[];
values: StoreValue[];
}

interface ValidateAction {
type: 'validateField';
namePath: InternalNamePath;
triggerName: string;
}

export type ReducerAction = UpdateAction | ValidateAction;
export type ReducerAction = UpdateAction | ValidateAction | UpdateValuesAction;

export class FormStore {
private formHooked: boolean = false;
Expand Down Expand Up @@ -688,6 +694,13 @@ export class FormStore {
this.updateValue(namePath, value);
break;
}
case 'updateValues': {
const { namesPath, values } = action;
namesPath.forEach((namePath, index) => {
this.updateValue(namePath, values[index]);
});
break;
}
case 'validateField': {
const { namePath, triggerName } = action;
this.validateFields([namePath], { triggerName });
Expand Down

0 comments on commit a45cb75

Please sign in to comment.