@remit/ui 0.0.182 → 0.0.183
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/package.json
CHANGED
|
@@ -17,6 +17,8 @@ let container: HTMLElement;
|
|
|
17
17
|
let root: Root;
|
|
18
18
|
|
|
19
19
|
const accepted: string[] = [];
|
|
20
|
+
const comboboxPropsRenders: unknown[] = [];
|
|
21
|
+
const handleKeyDownRenders: unknown[] = [];
|
|
20
22
|
|
|
21
23
|
function Field(props: { options: string[] }) {
|
|
22
24
|
const [value, setValue] = useState("typed");
|
|
@@ -24,6 +26,8 @@ function Field(props: { options: string[] }) {
|
|
|
24
26
|
count: props.options.length,
|
|
25
27
|
onAccept: (index) => accepted.push(props.options[index]),
|
|
26
28
|
});
|
|
29
|
+
comboboxPropsRenders.push(suggest.comboboxProps);
|
|
30
|
+
handleKeyDownRenders.push(suggest.handleKeyDown);
|
|
27
31
|
return createElement("input", {
|
|
28
32
|
id: "field",
|
|
29
33
|
value,
|
|
@@ -38,6 +42,17 @@ function Field(props: { options: string[] }) {
|
|
|
38
42
|
});
|
|
39
43
|
}
|
|
40
44
|
|
|
45
|
+
const typeInto = (field: Element, value: string) => {
|
|
46
|
+
const setter = Object.getOwnPropertyDescriptor(
|
|
47
|
+
HTMLInputElement.prototype,
|
|
48
|
+
"value",
|
|
49
|
+
)?.set;
|
|
50
|
+
act(() => {
|
|
51
|
+
setter?.call(field, value);
|
|
52
|
+
field.dispatchEvent(new Event("input", { bubbles: true }));
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
|
|
41
56
|
function mount(options: string[]) {
|
|
42
57
|
act(() => {
|
|
43
58
|
root.render(createElement(Field, { options }));
|
|
@@ -66,6 +81,8 @@ function press(field: Element, key: string) {
|
|
|
66
81
|
|
|
67
82
|
beforeEach(() => {
|
|
68
83
|
accepted.length = 0;
|
|
84
|
+
comboboxPropsRenders.length = 0;
|
|
85
|
+
handleKeyDownRenders.length = 0;
|
|
69
86
|
container = document.getElementById("root") as unknown as HTMLElement;
|
|
70
87
|
container.innerHTML = "";
|
|
71
88
|
root = createRoot(container);
|
|
@@ -142,4 +159,56 @@ describe("useSuggestList", () => {
|
|
|
142
159
|
assert.equal(field.getAttribute("data-open"), "true");
|
|
143
160
|
assert.equal(field.getAttribute("data-active"), "-1");
|
|
144
161
|
});
|
|
162
|
+
|
|
163
|
+
it("keeps the same comboboxProps object across a keystroke that changes nothing about the list", () => {
|
|
164
|
+
const field = mount(OPTIONS);
|
|
165
|
+
const rendersBefore = comboboxPropsRenders.length;
|
|
166
|
+
const before = comboboxPropsRenders.at(-1);
|
|
167
|
+
typeInto(field, "typed!");
|
|
168
|
+
assert.equal(
|
|
169
|
+
(field as HTMLInputElement).value,
|
|
170
|
+
"typed!",
|
|
171
|
+
"the keystroke landed",
|
|
172
|
+
);
|
|
173
|
+
assert.ok(
|
|
174
|
+
comboboxPropsRenders.length > rendersBefore,
|
|
175
|
+
"the keystroke did not re-render, so stability was never put to the test",
|
|
176
|
+
);
|
|
177
|
+
assert.equal(
|
|
178
|
+
comboboxPropsRenders.at(-1),
|
|
179
|
+
before,
|
|
180
|
+
"comboboxProps should be referentially stable across an idle re-render",
|
|
181
|
+
);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it("keeps the same handleKeyDown across a changed onAccept and a changed list", () => {
|
|
185
|
+
const field = mount(OPTIONS);
|
|
186
|
+
const first = handleKeyDownRenders.at(-1);
|
|
187
|
+
typeInto(field, "typed!");
|
|
188
|
+
press(field, "ArrowDown");
|
|
189
|
+
act(() => {
|
|
190
|
+
root.render(createElement(Field, { options: ["only"] }));
|
|
191
|
+
});
|
|
192
|
+
assert.ok(
|
|
193
|
+
handleKeyDownRenders.length > 1,
|
|
194
|
+
"nothing re-rendered, so stability was never put to the test",
|
|
195
|
+
);
|
|
196
|
+
assert.ok(
|
|
197
|
+
handleKeyDownRenders.every((handler) => handler === first),
|
|
198
|
+
"handleKeyDown should be referentially stable, so an inline onAccept at the call site cannot defeat a memo downstream",
|
|
199
|
+
);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
// The handler is stable, so it must read the state a key arrives at rather
|
|
203
|
+
// than the state it was bound in.
|
|
204
|
+
it("still accepts from the list it is looking at, not the one it was bound to", () => {
|
|
205
|
+
const field = mount(OPTIONS);
|
|
206
|
+
act(() => {
|
|
207
|
+
root.render(createElement(Field, { options: ["alpha", "beta"] }));
|
|
208
|
+
});
|
|
209
|
+
press(field, "ArrowDown");
|
|
210
|
+
press(field, "ArrowDown");
|
|
211
|
+
press(field, "Enter");
|
|
212
|
+
assert.deepEqual(accepted, ["beta"]);
|
|
213
|
+
});
|
|
145
214
|
});
|
|
@@ -3,6 +3,8 @@ import {
|
|
|
3
3
|
useCallback,
|
|
4
4
|
useEffect,
|
|
5
5
|
useId,
|
|
6
|
+
useMemo,
|
|
7
|
+
useRef,
|
|
6
8
|
useState,
|
|
7
9
|
} from "react";
|
|
8
10
|
import { suggestKeyAction } from "./suggest-keys.js";
|
|
@@ -40,7 +42,10 @@ export interface SuggestListState {
|
|
|
40
42
|
dismiss: () => void;
|
|
41
43
|
/** Offer the list again — what a fresh keystroke does after a dismissal. */
|
|
42
44
|
reopen: () => void;
|
|
43
|
-
/**
|
|
45
|
+
/**
|
|
46
|
+
* Returns true when the list consumed the key and the caller should stop.
|
|
47
|
+
* The same function for the life of the field, whatever the list is showing.
|
|
48
|
+
*/
|
|
44
49
|
handleKeyDown: (event: KeyboardEvent) => boolean;
|
|
45
50
|
listId: string;
|
|
46
51
|
optionId: (index: number) => string;
|
|
@@ -82,23 +87,45 @@ export function useSuggestList({
|
|
|
82
87
|
|
|
83
88
|
const open = count > 0 && !dismissed;
|
|
84
89
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
event.
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
}
|
|
101
|
-
|
|
90
|
+
// What the handler reads when a key arrives, rather than what it closed over
|
|
91
|
+
// when it was bound. Every call site passes an inline `onAccept`, so a handler
|
|
92
|
+
// listing it as a dependency is a new function on every render, and that
|
|
93
|
+
// defeats every memo the props it sits in belong to (#506).
|
|
94
|
+
const latest = useRef({ open, count, activeIndex, acceptKeys, onAccept });
|
|
95
|
+
latest.current = { open, count, activeIndex, acceptKeys, onAccept };
|
|
96
|
+
|
|
97
|
+
const handleKeyDown = useCallback((event: KeyboardEvent): boolean => {
|
|
98
|
+
const current = latest.current;
|
|
99
|
+
const action = suggestKeyAction({
|
|
100
|
+
key: event.key,
|
|
101
|
+
open: current.open,
|
|
102
|
+
count: current.count,
|
|
103
|
+
activeIndex: current.activeIndex,
|
|
104
|
+
acceptKeys: current.acceptKeys,
|
|
105
|
+
});
|
|
106
|
+
if (action.type === "none") return false;
|
|
107
|
+
event.preventDefault();
|
|
108
|
+
if (action.type === "move") setActiveIndex(action.index);
|
|
109
|
+
if (action.type === "dismiss") setDismissed(true);
|
|
110
|
+
if (action.type === "accept") current.onAccept(action.index);
|
|
111
|
+
return true;
|
|
112
|
+
}, []);
|
|
113
|
+
|
|
114
|
+
// Stable while open, listId and activeIndex don't change: this is spread onto
|
|
115
|
+
// the field on every render, and a fresh object here would defeat every memo
|
|
116
|
+
// downstream that takes it as a dependency (#506).
|
|
117
|
+
const comboboxProps = useMemo<ComboboxProps>(
|
|
118
|
+
() => ({
|
|
119
|
+
role: "combobox",
|
|
120
|
+
"aria-expanded": open,
|
|
121
|
+
"aria-controls": listId,
|
|
122
|
+
"aria-autocomplete": "list",
|
|
123
|
+
...(activeIndex >= 0
|
|
124
|
+
? { "aria-activedescendant": optionId(activeIndex) }
|
|
125
|
+
: {}),
|
|
126
|
+
...(open ? { "data-escape-owner": "" as const } : {}),
|
|
127
|
+
}),
|
|
128
|
+
[open, listId, activeIndex, optionId],
|
|
102
129
|
);
|
|
103
130
|
|
|
104
131
|
return {
|
|
@@ -110,15 +137,6 @@ export function useSuggestList({
|
|
|
110
137
|
handleKeyDown,
|
|
111
138
|
listId,
|
|
112
139
|
optionId,
|
|
113
|
-
comboboxProps
|
|
114
|
-
role: "combobox",
|
|
115
|
-
"aria-expanded": open,
|
|
116
|
-
"aria-controls": listId,
|
|
117
|
-
"aria-autocomplete": "list",
|
|
118
|
-
...(activeIndex >= 0
|
|
119
|
-
? { "aria-activedescendant": optionId(activeIndex) }
|
|
120
|
-
: {}),
|
|
121
|
-
...(open ? { "data-escape-owner": "" as const } : {}),
|
|
122
|
-
},
|
|
140
|
+
comboboxProps,
|
|
123
141
|
};
|
|
124
142
|
}
|