Skip to content

Commit

Permalink
feat: test
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyair committed Apr 15, 2024
1 parent 86101a5 commit 16928d7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 61 deletions.
18 changes: 9 additions & 9 deletions docs/examples/names.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ export default () => {
}}
>
<Field
names={['start', 'end']}
// getValueProps={value => {
// console.log('11', value);
// return { value: value };
// }}
// getValueFromEvent={value => {
// console.log('2', value);
// return value;
// }}
// names={['start', 'end']}
// getValueProps={value => {
// console.log('11', value);
// return { value: value };
// }}
// getValueFromEvent={value => {
// console.log('2', value);
// return value;
// }}
>
<RangeInput />
</Field>
Expand Down
65 changes: 17 additions & 48 deletions src/Field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
containsNamePath,
defaultGetValueFromEvent,
getNamePath,
getNamesPath,
getValue,
} from './utils/valueUtil';

Expand Down Expand Up @@ -70,8 +69,6 @@ export interface InternalFieldProps<Values = any> {
dependencies?: NamePath[];
getValueFromEvent?: (...args: EventArgs) => StoreValue;
name?: InternalNamePath;
names?: InternalNamePath[];
noField?: boolean;
normalize?: (value: StoreValue, prevValue: StoreValue, allValues: Store) => StoreValue;
rules?: Rule[];
shouldUpdate?: ShouldUpdate<Values>;
Expand Down Expand Up @@ -102,9 +99,8 @@ export interface InternalFieldProps<Values = any> {
}

export interface FieldProps<Values = any>
extends Omit<InternalFieldProps<Values>, 'name' | 'names' | 'fieldContext'> {
extends Omit<InternalFieldProps<Values>, 'name' | 'fieldContext'> {
name?: NamePath<Values>;
names?: NamePath<Values>[];
}

export interface FieldState {
Expand Down Expand Up @@ -563,17 +559,9 @@ 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 { names } = this.props;
return names.map(name => getValue(store || getFieldsValue(true), name));
};

public getControlled = (childProps: ChildProps = {}) => {
const {
name,
names,
noField,
trigger,
validateTrigger,
getValueFromEvent,
Expand All @@ -582,19 +570,19 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
getValueProps,
fieldContext,
} = this.props;
if (noField) return;

const mergedValidateTrigger =
validateTrigger !== undefined ? validateTrigger : fieldContext.validateTrigger;

const namePath = this.getNamePath();
const { getInternalHooks, getFieldsValue }: InternalFormInstance = fieldContext;
const { dispatch } = getInternalHooks(HOOK_MARK);
const value = names ? this.getValues() : this.getValue();
const value = this.getValue();
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) : {};

// warning when prop value is function
Expand Down Expand Up @@ -630,14 +618,12 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
if (normalize) {
newValue = normalize(newValue, value, getFieldsValue(true));
}
if (names) {
// eslint-disable-next-line @typescript-eslint/no-shadow
names.forEach((namePath, index) => {
dispatch({ type: 'updateValue', namePath, value: newValue[index] });
});
} else {
dispatch({ type: 'updateValue', namePath, value: newValue });
}

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

if (originTriggerFunc) {
originTriggerFunc(...args);
Expand Down Expand Up @@ -696,11 +682,10 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
}
}

function WrapperField<Values = any>({ name, names, ...restProps }: FieldProps<Values>) {
function WrapperField<Values = any>({ name, ...restProps }: FieldProps<Values>) {
const fieldContext = React.useContext(FieldContext);
const listContext = React.useContext(ListContext);
const namePath = name !== undefined ? getNamePath(name) : undefined;
const namesPath = getNamesPath(names);

let key: string = 'keep';
if (!restProps.isListField) {
Expand All @@ -718,30 +703,14 @@ function WrapperField<Values = any>({ name, names, ...restProps }: FieldProps<Va
warning(false, '`preserve` should not apply on Form.List fields.');
}

const [firstNames, ...resetNames] = namesPath;
return (
<>
<Field
key={key}
name={names ? firstNames : namePath}
names={names ? namesPath : undefined}
isListField={!!listContext}
{...restProps}
fieldContext={fieldContext}
/>
{resetNames.map(item => (
<Field
key={key + item.toString()}
name={item}
noField
isListField={!!listContext}
{...restProps}
fieldContext={fieldContext}
>
{() => undefined}
</Field>
))}
</>
<Field
key={key}
name={namePath}
isListField={!!listContext}
{...restProps}
fieldContext={fieldContext}
/>
);
}

Expand Down
4 changes: 0 additions & 4 deletions src/utils/valueUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ export function getNamePath(path: NamePath | null): InternalNamePath {
return toArray(path);
}

export function getNamesPath(pathList: NamePath[] = []): InternalNamePath[] {
return pathList.map(path => toArray(path));
}

export function cloneByNamePathList(store: Store, namePathList: InternalNamePath[]): Store {
let newStore = {};
namePathList.forEach(namePath => {
Expand Down

0 comments on commit 16928d7

Please sign in to comment.