Skip to content

Commit dbd2e90

Browse files
kagawagaozombieJ
authored andcommitted
feat: add defaultValue for add operation (#52)
1 parent 56f162d commit dbd2e90

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/List.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface ListField {
1111
}
1212

1313
interface ListOperations {
14-
add: () => void;
14+
add: (defaultValue?: StoreValue) => void;
1515
remove: (index: number) => void;
1616
move: (from: number, to: number) => void;
1717
}
@@ -58,13 +58,13 @@ const List: React.FunctionComponent<ListProps> = ({ name, children }) => {
5858
* Always get latest value in case user update fields by `form` api.
5959
*/
6060
const operations: ListOperations = {
61-
add: () => {
61+
add: defaultValue => {
6262
// Mapping keys
6363
keyManager.keys = [...keyManager.keys, keyManager.id];
6464
keyManager.id += 1;
6565

6666
const newValue = getNewValue();
67-
onChange([...newValue, undefined]);
67+
onChange([...newValue, defaultValue]);
6868
},
6969
remove: (index: number) => {
7070
const newValue = getNewValue();

tests/list.test.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,23 +95,28 @@ describe('Form.List', () => {
9595
act(() => {
9696
operation.add();
9797
});
98+
// Add default value
9899
act(() => {
99-
operation.add();
100+
operation.add('2');
100101
});
102+
101103
act(() => {
102104
operation.add();
103105
});
104106

105107
wrapper.update();
106108
expect(getList().find(Field).length).toEqual(3);
109+
expect(form.getFieldsValue()).toEqual({
110+
list: [undefined, '2', undefined],
111+
});
107112

108113
matchKey(0, '0');
109114
matchKey(1, '1');
110115
matchKey(2, '2');
111116

112117
// Move
113118
act(() => {
114-
operation.move(2, 0);
119+
operation.move(2, 0);
115120
});
116121
wrapper.update();
117122
matchKey(0, '2');

0 commit comments

Comments
 (0)