@remit/ui 0.0.182 → 0.0.184

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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@remit/ui",
3
- "version": "0.0.182",
3
+ "version": "0.0.184",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "src"
@@ -641,7 +641,7 @@ describe("RunStepBody", () => {
641
641
  assert.match(html, /The rule itself is saved/);
642
642
  });
643
643
 
644
- it("says a stopped run was never sent rather than rejected", () => {
644
+ it("says a stopped run went unconfirmed rather than rejected", () => {
645
645
  const html = renderToString(
646
646
  createElement(RunStepBody, {
647
647
  ...runProps,
@@ -653,8 +653,8 @@ describe("RunStepBody", () => {
653
653
  }),
654
654
  );
655
655
  assert.match(html, /Stopped after 2/);
656
- assert.match(html, /nothing was sent for them/);
657
- assert.match(html, /Never sent/);
656
+ assert.match(html, /The rest were not sent/);
657
+ assert.match(html, /Not confirmed/);
658
658
  assert.doesNotMatch(html, /Server rejected/);
659
659
  });
660
660
 
@@ -998,9 +998,11 @@ export function RunStepBody(props: RunStepProps) {
998
998
  const { matched, applied, failures } = props;
999
999
  const outcome = runOutcomeOf(props);
1000
1000
  const copy = runCopy(outcome);
1001
- // A stopped run never sent these, so nothing rejected them.
1001
+ // A stopped run confirmed none of these: most were never sent, and the ones
1002
+ // on their way when the stop landed may have gone through anyway. Neither is
1003
+ // a rejection, so neither can be badged as one.
1002
1004
  const failureBadge =
1003
- props.state === "runStopped" ? "Never sent" : "Server rejected";
1005
+ props.state === "runStopped" ? "Not confirmed" : "Server rejected";
1004
1006
 
1005
1007
  return (
1006
1008
  <div className="space-y-4 pt-2">
@@ -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
- /** Returns true when the list consumed the key and the caller should stop. */
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
- const handleKeyDown = useCallback(
86
- (event: KeyboardEvent): boolean => {
87
- const action = suggestKeyAction({
88
- key: event.key,
89
- open,
90
- count,
91
- activeIndex,
92
- acceptKeys,
93
- });
94
- if (action.type === "none") return false;
95
- event.preventDefault();
96
- if (action.type === "move") setActiveIndex(action.index);
97
- if (action.type === "dismiss") setDismissed(true);
98
- if (action.type === "accept") onAccept(action.index);
99
- return true;
100
- },
101
- [open, count, activeIndex, acceptKeys, onAccept],
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
  }
@@ -588,12 +588,15 @@ describe("runCopy", () => {
588
588
 
589
589
  it("separates a run that stopped from a pass the server rejected", () => {
590
590
  // The bulk endpoints accept every id in a call that returns, so the only
591
- // failure the chunked runner can observe is a call that threw — and
592
- // everything after it was never sent. Saying the mail server rejected
593
- // those states a cause that did not happen.
591
+ // failure the chunked runner can observe is a call that threw or was
592
+ // aborted — and everything after it was never sent. Saying the mail server
593
+ // rejected those states a cause that did not happen. Stopping aborts the
594
+ // request on the wire without un-sending it (#113), so the copy stops short
595
+ // of promising that batch never landed either.
594
596
  const stopped = outcome("runStopped", "once");
595
597
  assert.equal(stopped.title, "Stopped after 10");
596
- assert.match(stopped.detail, /nothing was sent for them/);
598
+ assert.match(stopped.detail, /The rest were not sent/);
599
+ assert.match(stopped.detail, /may still have gone through/);
597
600
  assert.doesNotMatch(stopped.detail, /rejected/);
598
601
  assert.equal(stopped.retryLabel, "Retry 2");
599
602
  assert.equal(stopped.showProgress, true);
@@ -602,7 +602,7 @@ export const runCopy = ({
602
602
  return {
603
603
  ...shared,
604
604
  title: `Stopped after ${applied}`,
605
- detail: `${applied} of ${matched} ${done}. The run stopped before it reached the rest, so nothing was sent for them and nothing has happened to them.`,
605
+ detail: `${applied} of ${matched} ${done}. The rest were not sent, apart from the ones already on their way when you stopped — those may still have gone through.`,
606
606
  tone: "warning",
607
607
  dismissLabel: "Close",
608
608
  retryLabel: `Retry ${failed}`,