@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.
- package/esm/type.source.d.ts +2 -1
- package/esm/use-lunatic/commons/fill-components/fill-component-expressions.d.ts +1 -1
- package/esm/use-lunatic/commons/fill-components/fill-component-expressions.js +1 -1
- package/esm/use-lunatic/commons/fill-components/fill-component-expressions.js.map +1 -1
- package/esm/use-lunatic/commons/fill-components/fill-component.spec.js +42 -0
- package/esm/use-lunatic/commons/fill-components/fill-component.spec.js.map +1 -1
- package/esm/use-lunatic/commons/fill-components/fill-components.d.ts +2 -2
- package/esm/use-lunatic/commons/fill-components/fill-components.js +15 -3
- package/esm/use-lunatic/commons/fill-components/fill-components.js.map +1 -1
- package/esm/use-lunatic/hooks/useCallbackOnNextRender.d.ts +4 -0
- package/esm/use-lunatic/hooks/useCallbackOnNextRender.js +28 -0
- package/esm/use-lunatic/hooks/useCallbackOnNextRender.js.map +1 -0
- package/esm/use-lunatic/props/getComponentTypeProps.js +1 -1
- package/esm/use-lunatic/props/getComponentTypeProps.js.map +1 -1
- package/esm/use-lunatic/use-lunatic.js +4 -2
- package/esm/use-lunatic/use-lunatic.js.map +1 -1
- package/package.json +9 -1
- package/src/stories/utils/Orchestrator.tsx +3 -3
- package/src/type.source.ts +2 -1
- package/src/use-lunatic/commons/fill-components/fill-component-expressions.ts +2 -1
- package/src/use-lunatic/commons/fill-components/fill-component.spec.ts +52 -0
- package/src/use-lunatic/commons/fill-components/fill-components.ts +19 -3
- package/src/use-lunatic/hooks/useCallbackOnNextRender.test.tsx +50 -0
- package/src/use-lunatic/hooks/useCallbackOnNextRender.ts +31 -0
- package/src/use-lunatic/props/getComponentTypeProps.ts +2 -1
- package/src/use-lunatic/use-lunatic.ts +5 -2
- package/tsconfig.build.tsbuildinfo +1 -1
- package/type.source.d.ts +2 -1
- package/use-lunatic/commons/fill-components/fill-component-expressions.d.ts +1 -1
- package/use-lunatic/commons/fill-components/fill-component-expressions.js +1 -1
- package/use-lunatic/commons/fill-components/fill-component-expressions.js.map +1 -1
- package/use-lunatic/commons/fill-components/fill-component.spec.js +42 -0
- package/use-lunatic/commons/fill-components/fill-component.spec.js.map +1 -1
- package/use-lunatic/commons/fill-components/fill-components.d.ts +2 -2
- package/use-lunatic/commons/fill-components/fill-components.js +15 -3
- package/use-lunatic/commons/fill-components/fill-components.js.map +1 -1
- package/use-lunatic/hooks/useCallbackOnNextRender.d.ts +4 -0
- package/use-lunatic/hooks/useCallbackOnNextRender.js +31 -0
- package/use-lunatic/hooks/useCallbackOnNextRender.js.map +1 -0
- package/use-lunatic/props/getComponentTypeProps.js +1 -1
- package/use-lunatic/props/getComponentTypeProps.js.map +1 -1
- package/use-lunatic/use-lunatic.js +4 -2
- 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
|
+
}
|
|
@@ -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
|
-
|
|
178
|
+
onChangeAfterRender(responses);
|
|
176
179
|
},
|
|
177
|
-
[dispatch,
|
|
180
|
+
[dispatch, onChangeAfterRender]
|
|
178
181
|
);
|
|
179
182
|
|
|
180
183
|
const getData: LunaticState['getData'] = (
|