Repository navigation
Commit d4017fe
authored
EPBDS-16211 A: the last seven class components as hooks (#132)
## What changes
The last seven class components in `src` become function components:
| component | where | was |
|---|---|---|
| `ToggleField` | `modules/form/inputs` | `PureComponent` |
| `Upload` | `modules/upload/views` | `PureComponent`, `contextType`,
state, a `Dropzone` ref |
| `LocalDraftTableRow` | `engine/components` | `PureComponent` with
state |
| `Data` | `engine` | `Component`, render only |
| `PopupContent` (template factory) |
`engine/components/PopupContent.js` | a `PureComponent` class per call |
| `PopupContent` (static popups) | `engine/mapper.js` | a
`PureComponent` class per popup node |
| `Examples` | `demo/pages` | `Component` with state |
The one class component left in `src` is `Render.js`'s error boundary.
React offers no hook for `componentDidCatch`, so it stays a class.
Every `PureComponent` became `memo` (a shallow props compare, as
before). Every `defaultProps` became a default parameter, which React 19
requires of a function component. Hooks only: TypeScript for the same
seven is the next PR, as §9.2 asks (one moving part per PR).
## Four semantics the obvious conversion would have changed
1. **The default `translate` of `LocalDraftTableRow` and `Upload`.**
Both defaulted it to `Active.translate` as read *when the module
loaded*: `defaultProps` is evaluated once. The engine reassigns
`Active.translate` in every document's constructor (`rules.js`), so a
render-time read would take the translator of whichever document was
built last. The default is a module constant.
2. **`Data`'s default `data` and `meta`.** They were two shared objects.
The nested document compares `data` by reference, so the defaults are
module constants, not a new `{}` per render.
3. **When `Upload` tells the host about a drag.** The class called
`onFocus`/`onBlur` from `setState`'s callback, which runs:
- after the commit;
- with that commit's props;
- even when the state was unchanged and `PureComponent` skipped the
render.
A hook skips the commit in that last case. So the callbacks queue, a
layout effect flushes them after every commit (reading the committed
props), and a counter forces the commit that runs them. The React 16
`persist()` handling is unchanged.
4. **What `ToggleField` hands Field.** The class gave Field
`this.input`, one function per instance, so Field's component type never
changed. That function read the *latest* `this.props`: all of them,
including `onChange` and `instance`, which Field is deliberately not
given, and `value`, which overrides the field's own. A module-level
component keeps the type stable, and a context gives it the props
without passing `instance` or `onChange` through Field. The existing
leak test still holds.
`PopupContent` keeps its fresh type per popup, which
`rules.popup-content.test.js` pins, because the memoized function is
still created per call.
## Measured
Through the published entry, over the 38 examples, before → after:
| | before | after |
|---|---|---|
| commits at mount | 107 | **107** |
| `translate` calls at mount | 1013 | **1013** |
| commits on an edit | 27 | **27** |
| `translate` calls on an edit | 64 | **64** |
| examples whose DOM differs, after mount / after an edit | — | **0 /
0** |
`dist/index.js` is 328,053 → 327,402 bytes.
## Tests
- **Two tests reached into the classes** and now go through the rendered
component:
- "updates the form input before invoking the external callback" called
`new ToggleField(...).input(...)`. It now clicks the rendered toggle and
records what the form holds when the callback runs. It was
**mutation-checked**: with the two calls swapped, it fails.
- "handleKeyPress opens dropzone on Enter" passed an unused `ref` to
`Upload` through a wrapper class; on a function component that ref would
warn. The wrapper and the ref are gone; the assertion is unchanged.
- **Checked in the running demo:**
- both toggles in `inputToggle` flip;
- a draft row in `nestedDataKind` holds its input, "Add line" appends
`DES-003 | Prototype | 2 | 300`, and the draft clears;
- "Open Popup 1" opens with its content;
- a drag toggles the upload's `active` class;
- a title click expands its example and moves the hash;
- no console error or React warning at any step.
## Gates
All exited 0:
- `lint:js`, `lint:css`, `typecheck`;
- `docs:props:check`, `docs:views:check`, `css:fixture:check`;
- `test:env-flags`, `test:coverage` (thresholds included), `build`;
- `test:pack`, `test:pack:peers`, `test:types`;
- `test:react17` and `test:react19`, 2907 tests each;
- `test:e2e`, 41 tests;
- the manifest contract.
`test:react16` lost a worker to `SIGSEGV` on its first two runs:
`StandaloneTabs.test.js`, then `fields.test.js`, 12 tests each, hence
2895. That is the Node 24 V8 crash
([nodejs/node#62393](nodejs/node#62393)). The
third run passed 2907/2907.
🤖 Generated with [Claude Code](https://claude.com/claude-code)1 parent 5ede721 commit d4017fe
10 files changed
Lines changed: 472 additions & 459 deletions
File tree
- docs
- src
- core
- engine
- components
- modules
- form/inputs
- __tests__
- upload/views
- __tests__
- demo/pages
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
431 | 431 | | |
432 | 432 | | |
433 | 433 | | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
434 | 442 | | |
435 | 443 | | |
436 | 444 | | |
| |||
1773 | 1781 | | |
1774 | 1782 | | |
1775 | 1783 | | |
1776 | | - | |
| 1784 | + | |
1777 | 1785 | | |
1778 | 1786 | | |
1779 | 1787 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
7 | 13 | | |
8 | 14 | | |
9 | 15 | | |
| |||
23 | 29 | | |
24 | 30 | | |
25 | 31 | | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
49 | 38 | | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
54 | 45 | | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
83 | 61 | | |
| 62 | + | |
84 | 63 | | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
102 | 67 | | |
103 | | - | |
| 68 | + | |
| 69 | + | |
104 | 70 | | |
105 | 71 | | |
106 | 72 | | |
| |||
112 | 78 | | |
113 | 79 | | |
114 | 80 | | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
115 | 116 | | |
116 | 117 | | |
117 | 118 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| |||
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
54 | 61 | | |
55 | 62 | | |
56 | 63 | | |
57 | 64 | | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
67 | 69 | | |
68 | | - | |
| 70 | + | |
69 | 71 | | |
70 | | - | |
| 72 | + | |
71 | 73 | | |
72 | 74 | | |
73 | 75 | | |
74 | 76 | | |
75 | 77 | | |
76 | | - | |
77 | | - | |
| 78 | + | |
78 | 79 | | |
79 | | - | |
| 80 | + | |
80 | 81 | | |
81 | 82 | | |
82 | 83 | | |
| |||
111 | 112 | | |
112 | 113 | | |
113 | 114 | | |
114 | | - | |
| 115 | + | |
115 | 116 | | |
116 | 117 | | |
117 | 118 | | |
| |||
122 | 123 | | |
123 | 124 | | |
124 | 125 | | |
125 | | - | |
| 126 | + | |
126 | 127 | | |
127 | 128 | | |
128 | 129 | | |
| |||
134 | 135 | | |
135 | 136 | | |
136 | 137 | | |
137 | | - | |
138 | | - | |
| 138 | + | |
139 | 139 | | |
140 | | - | |
| 140 | + | |
141 | 141 | | |
142 | 142 | | |
143 | 143 | | |
144 | 144 | | |
145 | 145 | | |
146 | 146 | | |
147 | 147 | | |
148 | | - | |
| 148 | + | |
149 | 149 | | |
150 | 150 | | |
151 | 151 | | |
| |||
159 | 159 | | |
160 | 160 | | |
161 | 161 | | |
162 | | - | |
| 162 | + | |
163 | 163 | | |
164 | | - | |
| 164 | + | |
165 | 165 | | |
166 | 166 | | |
167 | | - | |
| 167 | + | |
168 | 168 | | |
169 | 169 | | |
170 | 170 | | |
| |||
173 | 173 | | |
174 | 174 | | |
175 | 175 | | |
176 | | - | |
| 176 | + | |
177 | 177 | | |
178 | 178 | | |
179 | 179 | | |
| |||
182 | 182 | | |
183 | 183 | | |
184 | 184 | | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
195 | 192 | | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
0 commit comments