@inseefr/lunatic 3.4.11-rc.0 → 3.5.0-rc.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. package/components/Loop/Loop.js +7 -27
  2. package/components/Loop/Loop.js.map +1 -1
  3. package/components/RosterForLoop/RosterForLoop.js +8 -37
  4. package/components/RosterForLoop/RosterForLoop.js.map +1 -1
  5. package/components/Sequence/Sequence.d.ts +1 -1
  6. package/components/Subsequence/Subsequence.d.ts +1 -1
  7. package/components/library.d.ts +2 -2
  8. package/esm/components/Loop/Loop.js +7 -27
  9. package/esm/components/Loop/Loop.js.map +1 -1
  10. package/esm/components/RosterForLoop/RosterForLoop.js +8 -34
  11. package/esm/components/RosterForLoop/RosterForLoop.js.map +1 -1
  12. package/esm/components/Sequence/Sequence.d.ts +1 -1
  13. package/esm/components/Subsequence/Subsequence.d.ts +1 -1
  14. package/esm/components/library.d.ts +2 -2
  15. package/esm/hooks/useArticulation.d.ts +58 -0
  16. package/esm/hooks/useArticulation.js +81 -0
  17. package/esm/hooks/useArticulation.js.map +1 -0
  18. package/esm/i18n/dictionary.d.ts +0 -8
  19. package/esm/i18n/dictionary.js +0 -5
  20. package/esm/i18n/dictionary.js.map +1 -1
  21. package/esm/i18n/index.d.ts +1 -1
  22. package/esm/index.d.ts +1 -0
  23. package/esm/index.js +1 -0
  24. package/esm/index.js.map +1 -1
  25. package/esm/type.source.js +1 -0
  26. package/esm/type.source.js.map +1 -1
  27. package/esm/use-lunatic/commons/variables/behaviours/resizing-behaviour.js +7 -23
  28. package/esm/use-lunatic/commons/variables/behaviours/resizing-behaviour.js.map +1 -1
  29. package/esm/use-lunatic/commons/variables/lunatic-variables-store.d.ts +1 -3
  30. package/esm/use-lunatic/commons/variables/lunatic-variables-store.js +1 -1
  31. package/esm/use-lunatic/commons/variables/lunatic-variables-store.js.map +1 -1
  32. package/esm/use-lunatic/commons/variables/lunatic-variables-store.spec.js +1 -66
  33. package/esm/use-lunatic/commons/variables/lunatic-variables-store.spec.js.map +1 -1
  34. package/esm/use-lunatic/props/getComponentTypeProps.d.ts +4 -4
  35. package/esm/use-lunatic/reducer/commons/index.d.ts +1 -0
  36. package/esm/use-lunatic/reducer/commons/index.js +1 -0
  37. package/esm/use-lunatic/reducer/commons/index.js.map +1 -1
  38. package/esm/use-lunatic/reducer/commons/resize-array-variable.d.ts +5 -0
  39. package/esm/use-lunatic/reducer/commons/resize-array-variable.js +21 -0
  40. package/esm/use-lunatic/reducer/commons/resize-array-variable.js.map +1 -0
  41. package/esm/use-lunatic/reducer/reducerInitializer.d.ts +2 -2
  42. package/esm/use-lunatic/reducer/reducerInitializer.js +31 -24
  43. package/esm/use-lunatic/reducer/reducerInitializer.js.map +1 -1
  44. package/esm/use-lunatic/type.d.ts +0 -1
  45. package/esm/utils/array.d.ts +0 -4
  46. package/esm/utils/array.js +2 -15
  47. package/esm/utils/array.js.map +1 -1
  48. package/esm/utils/array.spec.js +1 -12
  49. package/esm/utils/array.spec.js.map +1 -1
  50. package/hooks/useArticulation.d.ts +58 -0
  51. package/hooks/useArticulation.js +81 -0
  52. package/hooks/useArticulation.js.map +1 -0
  53. package/i18n/dictionary.d.ts +0 -8
  54. package/i18n/dictionary.js +0 -5
  55. package/i18n/dictionary.js.map +1 -1
  56. package/i18n/index.d.ts +1 -1
  57. package/index.d.ts +1 -0
  58. package/index.js +3 -1
  59. package/index.js.map +1 -1
  60. package/package.json +20 -4
  61. package/src/components/Loop/Loop.tsx +12 -42
  62. package/src/components/RosterForLoop/RosterForLoop.tsx +2 -42
  63. package/src/components/RosterForLoop/__snapshots__/RosterForLoop.spec.tsx.snap +0 -18
  64. package/src/hooks/useArticulation.ts +124 -0
  65. package/src/i18n/dictionary.ts +0 -5
  66. package/src/index.ts +1 -0
  67. package/src/stories/behaviour/articulation/articulation.stories.tsx +105 -0
  68. package/src/stories/behaviour/articulation/roundabout.json +348 -0
  69. package/src/stories/behaviour/performance/performance.stories.jsx +1 -1
  70. package/src/stories/pairwise/data.json +1 -1
  71. package/src/stories/pairwise/source.json +1 -3
  72. package/src/stories/utils/{SchemaValidator.jsx → SchemaValidator.tsx} +1 -1
  73. package/src/stories/utils/{orchestrator.jsx → orchestrator.tsx} +13 -9
  74. package/src/stories/utils/{overview.jsx → overview.tsx} +2 -0
  75. package/src/type.source.ts +1 -0
  76. package/src/use-lunatic/__snapshots__/use-lunatic.test.ts.snap +323 -0
  77. package/src/use-lunatic/commons/variables/behaviours/resizing-behaviour.ts +10 -35
  78. package/src/use-lunatic/commons/variables/lunatic-variables-store.spec.ts +1 -68
  79. package/src/use-lunatic/commons/variables/lunatic-variables-store.ts +2 -4
  80. package/src/use-lunatic/reducer/commons/index.ts +1 -0
  81. package/src/use-lunatic/reducer/commons/resize-array-variable.ts +28 -0
  82. package/src/use-lunatic/reducer/reducerInitializer.tsx +40 -29
  83. package/src/use-lunatic/type.ts +0 -1
  84. package/src/utils/array.spec.ts +1 -18
  85. package/src/utils/array.ts +3 -21
  86. package/tsconfig.build.tsbuildinfo +1 -1
  87. package/type.source.js +1 -0
  88. package/type.source.js.map +1 -1
  89. package/use-lunatic/commons/variables/behaviours/resizing-behaviour.js +6 -22
  90. package/use-lunatic/commons/variables/behaviours/resizing-behaviour.js.map +1 -1
  91. package/use-lunatic/commons/variables/lunatic-variables-store.d.ts +1 -3
  92. package/use-lunatic/commons/variables/lunatic-variables-store.js +1 -1
  93. package/use-lunatic/commons/variables/lunatic-variables-store.js.map +1 -1
  94. package/use-lunatic/commons/variables/lunatic-variables-store.spec.js +1 -66
  95. package/use-lunatic/commons/variables/lunatic-variables-store.spec.js.map +1 -1
  96. package/use-lunatic/props/getComponentTypeProps.d.ts +4 -4
  97. package/use-lunatic/reducer/commons/index.d.ts +1 -0
  98. package/use-lunatic/reducer/commons/index.js +6 -0
  99. package/use-lunatic/reducer/commons/index.js.map +1 -1
  100. package/use-lunatic/reducer/commons/resize-array-variable.d.ts +5 -0
  101. package/use-lunatic/reducer/commons/resize-array-variable.js +23 -0
  102. package/use-lunatic/reducer/commons/resize-array-variable.js.map +1 -0
  103. package/use-lunatic/reducer/reducerInitializer.d.ts +2 -2
  104. package/use-lunatic/reducer/reducerInitializer.js +31 -24
  105. package/use-lunatic/reducer/reducerInitializer.js.map +1 -1
  106. package/use-lunatic/type.d.ts +0 -1
  107. package/utils/array.d.ts +0 -4
  108. package/utils/array.js +2 -16
  109. package/utils/array.js.map +1 -1
  110. package/utils/array.spec.js +0 -11
  111. package/utils/array.spec.js.map +1 -1
@@ -38,6 +38,8 @@ const baseState = {
38
38
  options: { disableFilters: false },
39
39
  } satisfies LunaticReducerState;
40
40
 
41
+ const onChange = { current: () => {} };
42
+
41
43
  export function reducerInitializer({
42
44
  source,
43
45
  data,
@@ -47,8 +49,8 @@ export function reducerInitializer({
47
49
  withOverview = false,
48
50
  disableFilters = false,
49
51
  getReferentiel,
50
- onVariableChange,
51
- logger,
52
+ onVariableChange = onChange,
53
+ logger = console.error,
52
54
  }: {
53
55
  source: LunaticSource;
54
56
  data: LunaticData;
@@ -58,8 +60,8 @@ export function reducerInitializer({
58
60
  withOverview?: LunaticOptions['withOverview'];
59
61
  disableFilters?: LunaticOptions['disableFilters'];
60
62
  getReferentiel?: LunaticOptions['getReferentiel'];
61
- onVariableChange: RefObject<LunaticOptions['onVariableChange']>;
62
- logger: LunaticLogger;
63
+ onVariableChange?: RefObject<LunaticOptions['onVariableChange']>;
64
+ logger?: LunaticLogger;
63
65
  }): LunaticReducerState {
64
66
  const variables = LunaticVariablesStore.makeFromSource(
65
67
  source,
@@ -110,11 +112,13 @@ export function reducerInitializer({
110
112
  }
111
113
  return result as any;
112
114
  } catch (e) {
113
- // If there is an error interpreting a variable, return the raw expression
114
- logger({
115
- type: 'ERROR',
116
- error: e as Error,
117
- });
115
+ if (logger) {
116
+ // If there is an error interpreting a variable, return the raw expression
117
+ logger({
118
+ type: 'ERROR',
119
+ error: e as Error,
120
+ });
121
+ }
118
122
  return expressionString;
119
123
  }
120
124
  };
@@ -131,9 +135,9 @@ export function reducerInitializer({
131
135
  const pager = {
132
136
  page: initialPager?.page ?? 1,
133
137
  maxPage: source.maxPage ? parseInt(source.maxPage, 10) : 1,
134
- subPage: undefined,
138
+ subPage: initialPager?.subPage,
135
139
  nbSubPages: undefined,
136
- iteration: undefined,
140
+ iteration: initialPager?.iteration,
137
141
  nbIterations: undefined,
138
142
  lastReachedPage: lastReachedPage ?? initialPage,
139
143
  };
@@ -143,7 +147,7 @@ export function reducerInitializer({
143
147
  pager,
144
148
  previousPager: pager,
145
149
  pages,
146
- isInLoop: false,
150
+ isInLoop: pager.subPage !== undefined,
147
151
  updatedAt: Date.now(),
148
152
  overview: withOverview ? buildOverview(source) : [],
149
153
  updateBindings,
@@ -161,22 +165,29 @@ function fillPagerForLoop(state: LunaticReducerState): LunaticReducerState {
161
165
  return state;
162
166
  }
163
167
  const { isLoop, subPages, iterations, loopDependencies } = pages[pager.page];
164
- if (!isLoop) {
165
- return state;
168
+
169
+ if (
170
+ // For loop, jump at the first page
171
+ isLoop ||
172
+ // For roundabout, jump at the desired iteration / subpage (only if defined)
173
+ (pager?.iteration !== undefined && subPages)
174
+ ) {
175
+ return {
176
+ ...state,
177
+ isInLoop: true,
178
+ pager: {
179
+ ...pager,
180
+ subPage: pager?.subPage ?? 1,
181
+ nbSubPages: (subPages ?? []).length,
182
+ iteration: pager?.iteration ?? 0,
183
+ nbIterations: forceInt(
184
+ state.executeExpression(iterations, {
185
+ deps: loopDependencies,
186
+ })
187
+ ),
188
+ },
189
+ };
166
190
  }
167
- return {
168
- ...state,
169
- isInLoop: true,
170
- pager: {
171
- ...pager,
172
- subPage: pager?.subPage ?? 0,
173
- nbSubPages: (subPages ?? []).length,
174
- iteration: pager?.iteration ?? 0,
175
- nbIterations: forceInt(
176
- state.executeExpression(iterations, {
177
- deps: loopDependencies,
178
- })
179
- ),
180
- },
181
- };
191
+
192
+ return state;
182
193
  }
@@ -349,6 +349,5 @@ export type LunaticChangesHandler = (
349
349
  name: string;
350
350
  value: any;
351
351
  iteration?: number[];
352
- removedIndex?: number;
353
352
  }[]
354
353
  ) => void;
@@ -1,10 +1,5 @@
1
1
  import { describe, it, expect } from 'vitest';
2
- import {
3
- firstValueItem,
4
- resizeArray,
5
- resizeDownArrayWithIndex,
6
- setAtIndex,
7
- } from './array';
2
+ import { firstValueItem, resizeArray, setAtIndex } from './array';
8
3
 
9
4
  describe('array', () => {
10
5
  describe('resizeArray()', () => {
@@ -50,16 +45,4 @@ describe('array', () => {
50
45
  expect(firstValueItem([null, 1, 2])).toBe(1);
51
46
  expect(firstValueItem([null, undefined, false])).toBe(false);
52
47
  });
53
- describe('resizeDownArrayWithIndex()', () => {
54
- it('should remove an element of array', () => {
55
- expect(resizeDownArrayWithIndex([1, 2, 3, 4], 2)).toEqual([1, 2, 4]);
56
- expect(resizeDownArrayWithIndex([1, 2, 3, 4], 0)).toEqual([2, 3, 4]);
57
- expect(resizeDownArrayWithIndex([1, 2, 3, 4], 3)).toEqual([1, 2, 3]);
58
- });
59
-
60
- it('should not remove element (out of index)', () => {
61
- expect(resizeDownArrayWithIndex([1, 2, 3, 4], -1)).toEqual([1, 2, 3, 4]);
62
- expect(resizeDownArrayWithIndex([1, 2, 3, 4], 4)).toEqual([1, 2, 3, 4]);
63
- });
64
- });
65
48
  });
@@ -51,9 +51,6 @@ export function getAtIndex(arr: unknown, indexes: number[]): unknown {
51
51
  return current;
52
52
  }
53
53
 
54
- /**
55
- * Cast the variable into an array and adjust the length if necessary
56
- */
57
54
  export function resizeArray<T = unknown>(
58
55
  array: unknown,
59
56
  newLength: number,
@@ -66,29 +63,14 @@ export function resizeArray<T = unknown>(
66
63
  if (array.length === newLength) {
67
64
  return array;
68
65
  }
69
- return new Array(newLength).fill(defaultValue ?? null).reduce(function (
70
- step,
71
- current,
66
+ return new Array(newLength).fill(defaultValue ?? null).map(function (
67
+ value,
72
68
  index
73
69
  ) {
74
- if (index < array.length) {
75
- return [...step, array[index]];
76
- }
77
- return [...step, current];
70
+ return index < array.length ? array[index] : value;
78
71
  }, []);
79
72
  }
80
73
 
81
- export function resizeDownArrayWithIndex<T = unknown>(
82
- array: T[],
83
- removedIndex: number
84
- ): T[] {
85
- // the removedIndex is not in array
86
- if (0 > removedIndex || array.length <= removedIndex) {
87
- return array;
88
- }
89
- return [...array].filter((_, i) => i !== removedIndex);
90
- }
91
-
92
74
  /**
93
75
  * Return the first non-null/undefined value of an array
94
76
  */