@seed-design/lynx-react 0.6.1 → 0.6.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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useControllableState.d.ts","sourceRoot":"","sources":["../../src/hooks/useControllableState.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,yBAAyB,CAAC,CAAC;IAC1C,KAAK,CAAC,EAAE,CAAC,CAAC;IACV,YAAY,EAAE,CAAC,CAAC;IAChB,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC;CAC/B;AAED;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CAAC,CAAC,EACpC,KAAK,EAAE,yBAAyB,CAAC,CAAC,CAAC,GAClC,CAAC,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC,
|
|
1
|
+
{"version":3,"file":"useControllableState.d.ts","sourceRoot":"","sources":["../../src/hooks/useControllableState.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,yBAAyB,CAAC,CAAC;IAC1C,KAAK,CAAC,EAAE,CAAC,CAAC;IACV,YAAY,EAAE,CAAC,CAAC;IAChB,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC;CAC/B;AAED;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CAAC,CAAC,EACpC,KAAK,EAAE,yBAAyB,CAAC,CAAC,CAAC,GAClC,CAAC,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC,CAuBzB"}
|
|
@@ -16,7 +16,11 @@ function useControllableState(props) {
|
|
|
16
16
|
const currentValue = isControlled ? value : uncontrolledValue;
|
|
17
17
|
const prevValueRef = useRef(currentValue);
|
|
18
18
|
return [currentValue, useMemoizedFn((nextValue) => {
|
|
19
|
-
if (
|
|
19
|
+
if (isControlled) {
|
|
20
|
+
if (nextValue !== value) onChange?.(nextValue);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
setUncontrolledValue(nextValue);
|
|
20
24
|
if (nextValue !== prevValueRef.current) {
|
|
21
25
|
onChange?.(nextValue);
|
|
22
26
|
prevValueRef.current = nextValue;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@seed-design/lynx-react",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.2",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/daangn/seed-design.git",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"@lynx-js/react": "^0.117.0",
|
|
41
41
|
"@lynx-js/types": "^3.7.0",
|
|
42
|
-
"@seed-design/lynx-css": "0.10.
|
|
42
|
+
"@seed-design/lynx-css": "0.10.2",
|
|
43
43
|
"@testing-library/jest-dom": "^6.9.1",
|
|
44
44
|
"jsdom": "^29.0.2",
|
|
45
45
|
"preact": "npm:@lynx-js/internal-preact@10.28.4-ee7bb26",
|
|
@@ -64,6 +64,56 @@ describe("useControllableState", () => {
|
|
|
64
64
|
});
|
|
65
65
|
|
|
66
66
|
describe("controlled mode", () => {
|
|
67
|
+
it.each([false, true])("notifies after an external change from %s", (initialValue) => {
|
|
68
|
+
const onChange = vi.fn();
|
|
69
|
+
const { result, rerender } = renderHook(
|
|
70
|
+
(props: { value: boolean }) =>
|
|
71
|
+
useControllableState({ value: props.value, defaultValue: false, onChange }),
|
|
72
|
+
{ initialProps: { value: initialValue } },
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
rerender({ value: !initialValue });
|
|
76
|
+
expect(result.current[0]).toBe(!initialValue);
|
|
77
|
+
expect(onChange).not.toHaveBeenCalled();
|
|
78
|
+
|
|
79
|
+
act(() => result.current[1](!result.current[0]));
|
|
80
|
+
|
|
81
|
+
expect(onChange).toHaveBeenCalledExactlyOnceWith(initialValue);
|
|
82
|
+
rerender({ value: initialValue });
|
|
83
|
+
act(() => result.current[1](!result.current[0]));
|
|
84
|
+
expect(onChange).toHaveBeenNthCalledWith(2, !initialValue);
|
|
85
|
+
expect(onChange).toHaveBeenCalledTimes(2);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("notifies again when the parent does not accept a change", () => {
|
|
89
|
+
const onChange = vi.fn();
|
|
90
|
+
const { result } = renderHook(() =>
|
|
91
|
+
useControllableState({ value: false, defaultValue: false, onChange }),
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
act(() => result.current[1](true));
|
|
95
|
+
act(() => result.current[1](true));
|
|
96
|
+
|
|
97
|
+
expect(result.current[0]).toBe(false);
|
|
98
|
+
expect(onChange).toHaveBeenCalledTimes(2);
|
|
99
|
+
expect(onChange).toHaveBeenNthCalledWith(1, true);
|
|
100
|
+
expect(onChange).toHaveBeenNthCalledWith(2, true);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("does not notify for the current prop after an unaccepted change", () => {
|
|
104
|
+
const onChange = vi.fn();
|
|
105
|
+
const { result } = renderHook(() =>
|
|
106
|
+
useControllableState({ value: false, defaultValue: false, onChange }),
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
act(() => result.current[1](false));
|
|
110
|
+
expect(onChange).not.toHaveBeenCalled();
|
|
111
|
+
act(() => result.current[1](true));
|
|
112
|
+
act(() => result.current[1](false));
|
|
113
|
+
|
|
114
|
+
expect(onChange).toHaveBeenCalledExactlyOnceWith(true);
|
|
115
|
+
});
|
|
116
|
+
|
|
67
117
|
it("uses the provided value", () => {
|
|
68
118
|
const { result } = renderHook(() =>
|
|
69
119
|
useControllableState({ value: "external", defaultValue: "default" }),
|
|
@@ -25,9 +25,14 @@ export function useControllableState<T>(
|
|
|
25
25
|
const prevValueRef = useRef(currentValue);
|
|
26
26
|
|
|
27
27
|
const setValue = useMemoizedFn((nextValue: T) => {
|
|
28
|
-
if (
|
|
29
|
-
|
|
28
|
+
if (isControlled) {
|
|
29
|
+
if (nextValue !== value) {
|
|
30
|
+
onChange?.(nextValue);
|
|
31
|
+
}
|
|
32
|
+
return;
|
|
30
33
|
}
|
|
34
|
+
|
|
35
|
+
setUncontrolledValue(nextValue);
|
|
31
36
|
if (nextValue !== prevValueRef.current) {
|
|
32
37
|
onChange?.(nextValue);
|
|
33
38
|
prevValueRef.current = nextValue;
|