List.js
4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import * as React from 'react';
import warning from "rc-util/es/warning";
import FieldContext from './FieldContext';
import Field from './Field';
import { move as _move, getNamePath } from './utils/valueUtil';
var List = function List(_ref) {
var name = _ref.name,
initialValue = _ref.initialValue,
children = _ref.children,
rules = _ref.rules,
validateTrigger = _ref.validateTrigger;
var context = React.useContext(FieldContext);
var keyRef = React.useRef({
keys: [],
id: 0
});
var keyManager = keyRef.current; // User should not pass `children` as other type.
if (typeof children !== 'function') {
warning(false, 'Form.List only accepts function as children.');
return null;
}
var parentPrefixName = getNamePath(context.prefixName) || [];
var prefixName = [].concat(_toConsumableArray(parentPrefixName), _toConsumableArray(getNamePath(name)));
var shouldUpdate = function shouldUpdate(prevValue, nextValue, _ref2) {
var source = _ref2.source;
if (source === 'internal') {
return false;
}
return prevValue !== nextValue;
};
return /*#__PURE__*/React.createElement(FieldContext.Provider, {
value: _objectSpread(_objectSpread({}, context), {}, {
prefixName: prefixName
})
}, /*#__PURE__*/React.createElement(Field, {
name: [],
shouldUpdate: shouldUpdate,
rules: rules,
validateTrigger: validateTrigger,
initialValue: initialValue,
isList: true
}, function (_ref3, meta) {
var _ref3$value = _ref3.value,
value = _ref3$value === void 0 ? [] : _ref3$value,
onChange = _ref3.onChange;
var getFieldValue = context.getFieldValue;
var getNewValue = function getNewValue() {
var values = getFieldValue(prefixName || []);
return values || [];
};
/**
* Always get latest value in case user update fields by `form` api.
*/
var operations = {
add: function add(defaultValue, index) {
// Mapping keys
var newValue = getNewValue();
if (index >= 0 && index <= newValue.length) {
keyManager.keys = [].concat(_toConsumableArray(keyManager.keys.slice(0, index)), [keyManager.id], _toConsumableArray(keyManager.keys.slice(index)));
onChange([].concat(_toConsumableArray(newValue.slice(0, index)), [defaultValue], _toConsumableArray(newValue.slice(index))));
} else {
if (process.env.NODE_ENV !== 'production' && (index < 0 || index > newValue.length)) {
warning(false, 'The second parameter of the add function should be a valid positive number.');
}
keyManager.keys = [].concat(_toConsumableArray(keyManager.keys), [keyManager.id]);
onChange([].concat(_toConsumableArray(newValue), [defaultValue]));
}
keyManager.id += 1;
},
remove: function remove(index) {
var newValue = getNewValue();
var indexSet = new Set(Array.isArray(index) ? index : [index]);
if (indexSet.size <= 0) {
return;
}
keyManager.keys = keyManager.keys.filter(function (_, keysIndex) {
return !indexSet.has(keysIndex);
}); // Trigger store change
onChange(newValue.filter(function (_, valueIndex) {
return !indexSet.has(valueIndex);
}));
},
move: function move(from, to) {
if (from === to) {
return;
}
var newValue = getNewValue(); // Do not handle out of range
if (from < 0 || from >= newValue.length || to < 0 || to >= newValue.length) {
return;
}
keyManager.keys = _move(keyManager.keys, from, to); // Trigger store change
onChange(_move(newValue, from, to));
}
};
var listValue = value || [];
if (!Array.isArray(listValue)) {
listValue = [];
if (process.env.NODE_ENV !== 'production') {
warning(false, "Current value of '".concat(prefixName.join(' > '), "' is not an array type."));
}
}
return children(listValue.map(function (__, index) {
var key = keyManager.keys[index];
if (key === undefined) {
keyManager.keys[index] = keyManager.id;
key = keyManager.keys[index];
keyManager.id += 1;
}
return {
name: index,
key: key,
isListField: true
};
}), operations, meta);
}));
};
export default List;