|
1 |
| -import { get, set } from 'lodash'; |
| 1 | +import { set } from 'lodash'; |
2 | 2 | import { PopulateContext, PopulateScope, ProcessorFnSync } from 'types';
|
3 |
| -import { componentPath, getContextualRowPath, getModelType } from 'utils/formUtil'; |
| 3 | +import { getModelType } from 'utils/formUtil'; |
4 | 4 |
|
5 | 5 | // This processor ensures that a "linked" row context is provided to every component.
|
6 | 6 | export const populateProcessSync: ProcessorFnSync<PopulateScope> = (context: PopulateContext) => {
|
7 |
| - const { component, path, scope } = context; |
| 7 | + const { component, path, scope, value } = context; |
8 | 8 | const { data } = scope;
|
9 |
| - const compDataPath = componentPath(component, getContextualRowPath(component, path)); |
10 |
| - const compData: any = get(data, compDataPath); |
11 | 9 | if (!scope.populated) scope.populated = [];
|
12 | 10 | switch (getModelType(component)) {
|
13 | 11 | case 'nestedArray':
|
14 |
| - if (!compData || !compData.length) { |
15 |
| - set(data, compDataPath, [{}]); |
16 |
| - scope.row = get(data, compDataPath)[0]; |
| 12 | + if (!value || !value.length) { |
| 13 | + const newValue = [{}]; |
| 14 | + set(data, path, newValue); |
| 15 | + scope.row = newValue[0]; |
17 | 16 | scope.populated.push({
|
18 | 17 | path,
|
19 |
| - row: get(data, compDataPath)[0], |
20 | 18 | });
|
21 | 19 | }
|
22 | 20 | break;
|
23 | 21 | case 'dataObject':
|
24 | 22 | case 'object':
|
25 |
| - if (!compData || typeof compData !== 'object') { |
26 |
| - set(data, compDataPath, {}); |
27 |
| - scope.row = get(data, compDataPath); |
| 23 | + if (!value || typeof value !== 'object') { |
| 24 | + const newValue = {}; |
| 25 | + set(data, value, newValue); |
| 26 | + scope.row = newValue; |
28 | 27 | scope.populated.push({
|
29 | 28 | path,
|
30 |
| - row: get(data, compDataPath), |
31 | 29 | });
|
32 | 30 | }
|
33 | 31 | break;
|
|
0 commit comments