Skip to content

Commit c65c73e

Browse files
committed
refactor: provide more info
1 parent 5360744 commit c65c73e

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

src/List.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const List: React.FunctionComponent<ListProps> = ({
5858
getKey: (namePath: InternalNamePath) => {
5959
const len = prefixName.length;
6060
const pathName = namePath[len];
61-
return keyManager.keys[pathName];
61+
return [keyManager.keys[pathName], namePath.slice(len + 1)];
6262
},
6363
}),
6464
[prefixName],

src/ListContext.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as React from 'react';
22
import type { InternalNamePath } from './interface';
33

44
export interface ListContextProps {
5-
getKey: (namePath: InternalNamePath) => React.Key;
5+
getKey: (namePath: InternalNamePath) => [React.Key, InternalNamePath];
66
}
77

88
const ListContext = React.createContext<ListContextProps | null>(null);

tests/list.test.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -751,11 +751,14 @@ describe('Form.List', () => {
751751
it('ListContext', () => {
752752
const Hooker = ({ field }: any) => {
753753
const { getKey } = React.useContext(ListContext);
754+
const [key, restPath] = getKey(['list', field.name, 'user']);
754755

755756
return (
756757
<>
757-
<span className="internal-name">{getKey(['list', field.name])}</span>
758-
<Field {...field}>
758+
<span className="internal-key">{key}</span>
759+
<span className="internal-rest">{restPath.join('_')}</span>
760+
761+
<Field {...field} name={[field.name, 'user']}>
759762
<Input />
760763
</Field>
761764
</>
@@ -772,11 +775,13 @@ describe('Form.List', () => {
772775
),
773776
{
774777
initialValues: {
775-
list: [''],
778+
list: [{ user: 'bamboo' }],
776779
},
777780
},
778781
);
779782

780-
expect(wrapper.find('.internal-name').text()).toEqual('0');
783+
expect(wrapper.find('.internal-key').text()).toEqual('0');
784+
expect(wrapper.find('.internal-rest').text()).toEqual('user');
785+
expect(wrapper.find('input').prop('value')).toEqual('bamboo');
781786
});
782787
});

0 commit comments

Comments
 (0)