Skip to content

Commit 8ba3a0d

Browse files
authored
fix: Change name should rest field (#102)
* Add wrapper component * Add key for Field diff * add ignore * test case
1 parent bc56d70 commit 8ba3a0d

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ lib/
88
~*
99
yarn.lock
1010
package-lock.json
11-
.idea/
11+
.idea/
12+
examples/debug.tsx

src/Field.tsx

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ interface ChildProps {
4949
[name: string]: any;
5050
}
5151

52-
export interface FieldProps {
52+
export interface InternalFieldProps {
5353
children?:
5454
| React.ReactElement
5555
| ((control: ChildProps, meta: Meta, form: FormInstance) => React.ReactNode);
@@ -60,7 +60,7 @@ export interface FieldProps {
6060
*/
6161
dependencies?: NamePath[];
6262
getValueFromEvent?: (...args: EventArgs) => StoreValue;
63-
name?: NamePath;
63+
name?: InternalNamePath;
6464
normalize?: (value: StoreValue, prevValue: StoreValue, allValues: Store) => StoreValue;
6565
rules?: Rule[];
6666
shouldUpdate?: ShouldUpdate;
@@ -72,12 +72,16 @@ export interface FieldProps {
7272
onReset?: () => void;
7373
}
7474

75+
export interface FieldProps extends Omit<InternalFieldProps, 'name'> {
76+
name?: NamePath;
77+
}
78+
7579
export interface FieldState {
7680
resetCount: number;
7781
}
7882

7983
// We use Class instead of Hooks here since it will cost much code by using Hooks.
80-
class Field extends React.Component<FieldProps, FieldState> implements FieldEntity {
84+
class Field extends React.Component<InternalFieldProps, FieldState> implements FieldEntity {
8185
public static contextType = FieldContext;
8286

8387
public static defaultProps = {
@@ -129,9 +133,8 @@ class Field extends React.Component<FieldProps, FieldState> implements FieldEnti
129133
public getNamePath = (): InternalNamePath => {
130134
const { name } = this.props;
131135
const { prefixName = [] }: InternalFormInstance = this.context;
132-
const namePath = getNamePath(name);
133136

134-
return 'name' in this.props ? [...prefixName, ...namePath] : [];
137+
return name !== undefined ? [...prefixName, ...name] : [];
135138
};
136139

137140
public getRules = (): RuleObject[] => {
@@ -456,4 +459,9 @@ class Field extends React.Component<FieldProps, FieldState> implements FieldEnti
456459
}
457460
}
458461

459-
export default Field;
462+
const WrapperField: React.FC<FieldProps> = ({ name, ...restProps }) => {
463+
const namePath = name !== undefined ? getNamePath(name) : undefined;
464+
return <Field key={`_${(namePath || []).join('_')}`} name={namePath} {...restProps} />;
465+
};
466+
467+
export default WrapperField;

tests/validate.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,5 +439,44 @@ describe('Form.Validate', () => {
439439
matchError(wrapper, false);
440440
expect(onFinish).toHaveBeenCalledWith({ username: 'test' });
441441
});
442+
443+
it('switch to remove errors', async () => {
444+
const Demo = () => {
445+
const [checked, setChecked] = React.useState(true);
446+
447+
return (
448+
<Form>
449+
<button
450+
type="button"
451+
onClick={() => {
452+
setChecked(!checked);
453+
}}
454+
/>
455+
<InfoField
456+
name={checked ? 'username' : 'age'}
457+
rules={
458+
checked
459+
? [
460+
{
461+
validator(rule, value, callback) {
462+
callback('Integer number only!');
463+
},
464+
},
465+
]
466+
: []
467+
}
468+
/>
469+
</Form>
470+
);
471+
};
472+
const wrapper = mount(<Demo />);
473+
474+
await changeValue(wrapper, '233');
475+
matchError(wrapper, true);
476+
477+
wrapper.find('button').simulate('click');
478+
wrapper.update();
479+
matchError(wrapper, false);
480+
});
442481
});
443482
/* eslint-enable no-template-curly-in-string */

0 commit comments

Comments
 (0)