@inseefr/lunatic 3.5.8 → 3.6.1

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 (43) hide show
  1. package/esm/type.source.d.ts +2 -1
  2. package/esm/use-lunatic/commons/fill-components/fill-component-expressions.d.ts +1 -1
  3. package/esm/use-lunatic/commons/fill-components/fill-component-expressions.js +1 -1
  4. package/esm/use-lunatic/commons/fill-components/fill-component-expressions.js.map +1 -1
  5. package/esm/use-lunatic/commons/fill-components/fill-component.spec.js +42 -0
  6. package/esm/use-lunatic/commons/fill-components/fill-component.spec.js.map +1 -1
  7. package/esm/use-lunatic/commons/fill-components/fill-components.d.ts +2 -2
  8. package/esm/use-lunatic/commons/fill-components/fill-components.js +15 -3
  9. package/esm/use-lunatic/commons/fill-components/fill-components.js.map +1 -1
  10. package/esm/use-lunatic/hooks/useCallbackOnNextRender.d.ts +4 -0
  11. package/esm/use-lunatic/hooks/useCallbackOnNextRender.js +28 -0
  12. package/esm/use-lunatic/hooks/useCallbackOnNextRender.js.map +1 -0
  13. package/esm/use-lunatic/props/getComponentTypeProps.js +1 -1
  14. package/esm/use-lunatic/props/getComponentTypeProps.js.map +1 -1
  15. package/esm/use-lunatic/use-lunatic.js +4 -2
  16. package/esm/use-lunatic/use-lunatic.js.map +1 -1
  17. package/package.json +9 -1
  18. package/src/stories/utils/Orchestrator.tsx +3 -3
  19. package/src/type.source.ts +2 -1
  20. package/src/use-lunatic/commons/fill-components/fill-component-expressions.ts +2 -1
  21. package/src/use-lunatic/commons/fill-components/fill-component.spec.ts +52 -0
  22. package/src/use-lunatic/commons/fill-components/fill-components.ts +19 -3
  23. package/src/use-lunatic/hooks/useCallbackOnNextRender.test.tsx +50 -0
  24. package/src/use-lunatic/hooks/useCallbackOnNextRender.ts +31 -0
  25. package/src/use-lunatic/props/getComponentTypeProps.ts +2 -1
  26. package/src/use-lunatic/use-lunatic.ts +5 -2
  27. package/tsconfig.build.tsbuildinfo +1 -1
  28. package/type.source.d.ts +2 -1
  29. package/use-lunatic/commons/fill-components/fill-component-expressions.d.ts +1 -1
  30. package/use-lunatic/commons/fill-components/fill-component-expressions.js +1 -1
  31. package/use-lunatic/commons/fill-components/fill-component-expressions.js.map +1 -1
  32. package/use-lunatic/commons/fill-components/fill-component.spec.js +42 -0
  33. package/use-lunatic/commons/fill-components/fill-component.spec.js.map +1 -1
  34. package/use-lunatic/commons/fill-components/fill-components.d.ts +2 -2
  35. package/use-lunatic/commons/fill-components/fill-components.js +15 -3
  36. package/use-lunatic/commons/fill-components/fill-components.js.map +1 -1
  37. package/use-lunatic/hooks/useCallbackOnNextRender.d.ts +4 -0
  38. package/use-lunatic/hooks/useCallbackOnNextRender.js +31 -0
  39. package/use-lunatic/hooks/useCallbackOnNextRender.js.map +1 -0
  40. package/use-lunatic/props/getComponentTypeProps.js +1 -1
  41. package/use-lunatic/props/getComponentTypeProps.js.map +1 -1
  42. package/use-lunatic/use-lunatic.js +4 -2
  43. package/use-lunatic/use-lunatic.js.map +1 -1
@@ -0,0 +1,50 @@
1
+ import { useState } from 'react';
2
+ import { describe, expect, it, vi } from 'vitest';
3
+ import { useCallbackOnNextRender } from './useCallbackOnNextRender.js';
4
+ import { act, fireEvent, render } from '@testing-library/react';
5
+
6
+ describe('useCallbackOnNextRender', () => {
7
+ // Since we want to test when the callback is called, we need to keep track of the count state inside the component
8
+ let countSpy = 0;
9
+
10
+ const FakeComponent = (props: { onChange: () => void }) => {
11
+ const onChange = useCallbackOnNextRender(props.onChange);
12
+ const [count, setCount] = useState(0);
13
+ countSpy = count;
14
+
15
+ const onClick = () => {
16
+ setCount((v) => v + 1);
17
+ onChange();
18
+ };
19
+
20
+ return <button onClick={onClick}>{count}</button>;
21
+ };
22
+
23
+ it('should call the callback on the next render', () => {
24
+ let resolveSpy: (e?: unknown) => void;
25
+ const spyPromise = new Promise((resolve) => {
26
+ resolveSpy = resolve;
27
+ });
28
+ const onChange = vi.fn(() => {
29
+ try {
30
+ expect(countSpy).toBe(1);
31
+ resolveSpy();
32
+ } catch (e) {
33
+ resolveSpy(e);
34
+ }
35
+ });
36
+ const { getByRole } = render(<FakeComponent onChange={onChange} />);
37
+ act(() => {
38
+ fireEvent.click(getByRole('button'));
39
+ });
40
+
41
+ expect(onChange).toHaveBeenCalledTimes(1);
42
+
43
+ return spyPromise.then((r) => {
44
+ if (r instanceof Error) {
45
+ throw r;
46
+ }
47
+ expect(countSpy).toBe(1);
48
+ });
49
+ });
50
+ });
@@ -0,0 +1,31 @@
1
+ import { useCallback, useEffect, useRef } from 'react';
2
+
3
+ /**
4
+ * Run a callback on that will be run on the next render
5
+ */
6
+ export function useCallbackOnNextRender<T extends (...args: unknown[]) => void>(
7
+ cb: T
8
+ ): T {
9
+ const callOnNextRender = useRef<Parameters<T> | null>(null);
10
+ const cbRef = useRef(cb);
11
+ cbRef.current = cb;
12
+
13
+ // use useEffect to run the callback at the end of the next render
14
+ useEffect(() => {
15
+ if (callOnNextRender.current === null) {
16
+ return;
17
+ }
18
+ try {
19
+ cbRef.current(...callOnNextRender.current);
20
+ } catch (e) {
21
+ console.error(e);
22
+ }
23
+ callOnNextRender.current = null;
24
+ // eslint-disable-next-line react-hooks/exhaustive-deps -- We want to run the callback when the value of the ref changed on the next render
25
+ }, [callOnNextRender.current]);
26
+
27
+ // When the function is called, we store the arguments to be called on the next render (when state was really updated)
28
+ return useCallback((...args: Parameters<T>) => {
29
+ callOnNextRender.current = args;
30
+ }, []) as T;
31
+ }
@@ -49,7 +49,8 @@ function getChildComponents(
49
49
  component.components,
50
50
  state,
51
51
  undefined,
52
- component.conditionFilter
52
+ component.conditionFilter,
53
+ component.conditionReadOnly
53
54
  ),
54
55
  };
55
56
  }
@@ -32,6 +32,7 @@ import { mergeDefault } from '../utils/object';
32
32
  import { useRefSync } from '../hooks/useRefSync';
33
33
  import { ConsoleLogger } from './logger/ConsoleLogger';
34
34
  import { useWarnDepChange } from './hooks/useWarnDepChange';
35
+ import { useCallbackOnNextRender } from './hooks/useCallbackOnNextRender';
35
36
 
36
37
  const empty = {}; // Keep the same empty object (to avoid problem with useEffect dependencies)
37
38
  const DEFAULT_DATA = empty as LunaticData;
@@ -169,12 +170,14 @@ export function useLunatic(
169
170
  [dispatch]
170
171
  );
171
172
 
173
+ const onChangeAfterRender = useCallbackOnNextRender(onChange);
174
+
172
175
  const handleChanges = useCallback<LunaticChangesHandler>(
173
176
  (responses) => {
174
177
  dispatch(handleChangesAction(responses));
175
- onChange(responses);
178
+ onChangeAfterRender(responses);
176
179
  },
177
- [dispatch, onChange]
180
+ [dispatch, onChangeAfterRender]
178
181
  );
179
182
 
180
183
  const getData: LunaticState['getData'] = (