@codual/react-native-ui 0.0.0-stage → 0.1.0
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/LICENSE +202 -0
- package/NOTICE +2 -0
- package/README.md +18 -2
- package/dist/announce.d.ts +2 -0
- package/dist/announce.js +8 -0
- package/dist/charts/data.d.ts +32 -0
- package/dist/charts/data.js +13 -0
- package/dist/charts/labels.d.ts +54 -0
- package/dist/charts/labels.js +91 -0
- package/dist/charts/machine.d.ts +62 -0
- package/dist/charts/machine.js +144 -0
- package/dist/charts/points.d.ts +23 -0
- package/dist/charts/points.js +46 -0
- package/dist/charts/summary.d.ts +29 -0
- package/dist/charts/summary.js +45 -0
- package/dist/charts/use-chart-interaction.d.ts +62 -0
- package/dist/charts/use-chart-interaction.js +79 -0
- package/dist/forms/async-checks.d.ts +34 -0
- package/dist/forms/async-checks.js +77 -0
- package/dist/forms/field-errors.d.ts +8 -0
- package/dist/forms/field-errors.js +16 -0
- package/dist/forms/form-types.d.ts +43 -0
- package/dist/forms/form-types.js +2 -0
- package/dist/forms/machine.d.ts +63 -0
- package/dist/forms/machine.js +251 -0
- package/dist/forms/numbers.d.ts +5 -0
- package/dist/forms/numbers.js +83 -0
- package/dist/forms/rules.d.ts +47 -0
- package/dist/forms/rules.js +72 -0
- package/dist/forms/server-errors.d.ts +17 -0
- package/dist/forms/server-errors.js +22 -0
- package/dist/forms/use-form.d.ts +49 -0
- package/dist/forms/use-form.js +61 -0
- package/dist/index.d.ts +50 -0
- package/dist/index.js +94 -0
- package/dist/lists/PagedList.d.ts +47 -0
- package/dist/lists/PagedList.js +81 -0
- package/dist/lists/controller.d.ts +23 -0
- package/dist/lists/controller.js +20 -0
- package/dist/lists/inverted-list.d.ts +23 -0
- package/dist/lists/inverted-list.js +60 -0
- package/dist/lists/machine.d.ts +81 -0
- package/dist/lists/machine.js +252 -0
- package/dist/lists/newest-end.d.ts +30 -0
- package/dist/lists/newest-end.js +58 -0
- package/dist/lists/paged-content.d.ts +46 -0
- package/dist/lists/paged-content.js +65 -0
- package/dist/lists/paged-list-types.d.ts +56 -0
- package/dist/lists/paged-list-types.js +2 -0
- package/dist/lists/system-footer.d.ts +28 -0
- package/dist/lists/system-footer.js +50 -0
- package/dist/lists/use-paged-list.d.ts +25 -0
- package/dist/lists/use-paged-list.js +34 -0
- package/dist/permissions/access.d.ts +30 -0
- package/dist/permissions/access.js +2 -0
- package/dist/permissions/android-permission.d.ts +52 -0
- package/dist/permissions/android-permission.js +143 -0
- package/dist/permissions/permission.d.ts +69 -0
- package/dist/permissions/permission.js +201 -0
- package/dist/permissions/setup.d.ts +25 -0
- package/dist/permissions/setup.js +41 -0
- package/dist/permissions/status.d.ts +29 -0
- package/dist/permissions/status.js +18 -0
- package/dist/permissions/use-permission.d.ts +23 -0
- package/dist/permissions/use-permission.js +29 -0
- package/dist/purchases/Paywall.d.ts +27 -0
- package/dist/purchases/Paywall.js +28 -0
- package/dist/purchases/paywall-content.d.ts +20 -0
- package/dist/purchases/paywall-content.js +34 -0
- package/dist/purchases/purchases.d.ts +92 -0
- package/dist/purchases/purchases.js +236 -0
- package/dist/purchases/status.d.ts +62 -0
- package/dist/purchases/status.js +25 -0
- package/dist/purchases/store.d.ts +71 -0
- package/dist/purchases/store.js +2 -0
- package/dist/purchases/system-views.d.ts +49 -0
- package/dist/purchases/system-views.js +91 -0
- package/dist/purchases/use-purchases.d.ts +3 -0
- package/dist/purchases/use-purchases.js +8 -0
- package/dist/react-native-iap/iap-purchases.d.ts +24 -0
- package/dist/react-native-iap/iap-purchases.js +90 -0
- package/dist/react-native-iap/iap-store.d.ts +56 -0
- package/dist/react-native-iap/iap-store.js +249 -0
- package/dist/react-native-iap/iap-types.d.ts +12 -0
- package/dist/react-native-iap/iap-types.js +11 -0
- package/dist/react-native-iap/index.d.ts +31 -0
- package/dist/react-native-iap/index.js +24 -0
- package/dist/react-native-permissions/index.d.ts +24 -0
- package/dist/react-native-permissions/index.js +88 -0
- package/dist/screen-states/ScreenStates.d.ts +38 -0
- package/dist/screen-states/ScreenStates.js +69 -0
- package/dist/screen-states/errors.d.ts +9 -0
- package/dist/screen-states/errors.js +24 -0
- package/dist/screen-states/machine.d.ts +97 -0
- package/dist/screen-states/machine.js +172 -0
- package/dist/screen-states/system-views.d.ts +40 -0
- package/dist/screen-states/system-views.js +66 -0
- package/dist/screen-states/use-screen-states.d.ts +23 -0
- package/dist/screen-states/use-screen-states.js +41 -0
- package/dist/search/SearchResults.d.ts +22 -0
- package/dist/search/SearchResults.js +25 -0
- package/dist/search/announcements.d.ts +17 -0
- package/dist/search/announcements.js +31 -0
- package/dist/search/labels.d.ts +15 -0
- package/dist/search/labels.js +14 -0
- package/dist/search/machine.d.ts +65 -0
- package/dist/search/machine.js +241 -0
- package/dist/search/recent-search-sync.d.ts +32 -0
- package/dist/search/recent-search-sync.js +87 -0
- package/dist/search/recent-searches.d.ts +22 -0
- package/dist/search/recent-searches.js +40 -0
- package/dist/search/system-views.d.ts +11 -0
- package/dist/search/system-views.js +16 -0
- package/dist/search/types.d.ts +51 -0
- package/dist/search/types.js +2 -0
- package/dist/search/use-search.d.ts +73 -0
- package/dist/search/use-search.js +123 -0
- package/dist/selection/config.d.ts +34 -0
- package/dist/selection/config.js +13 -0
- package/dist/selection/labels.d.ts +19 -0
- package/dist/selection/labels.js +23 -0
- package/dist/selection/machine.d.ts +80 -0
- package/dist/selection/machine.js +274 -0
- package/dist/selection/snapshot.d.ts +54 -0
- package/dist/selection/snapshot.js +45 -0
- package/dist/selection/state.d.ts +25 -0
- package/dist/selection/state.js +24 -0
- package/dist/selection/system-views.d.ts +11 -0
- package/dist/selection/system-views.js +22 -0
- package/dist/selection/use-selection.d.ts +73 -0
- package/dist/selection/use-selection.js +100 -0
- package/dist/sheets/machine.d.ts +101 -0
- package/dist/sheets/machine.js +213 -0
- package/dist/sheets/use-sheet.d.ts +78 -0
- package/dist/sheets/use-sheet.js +103 -0
- package/dist/victory-native/index.d.ts +37 -0
- package/dist/victory-native/index.js +41 -0
- package/package.json +94 -4
- package/react-native-iap/package.json +4 -0
- package/react-native-permissions/package.json +4 -0
- package/victory-native/package.json +4 -0
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SelectionMachine = void 0;
|
|
4
|
+
const config_1 = require("./config");
|
|
5
|
+
const snapshot_1 = require("./snapshot");
|
|
6
|
+
const state_1 = require("./state");
|
|
7
|
+
/**
|
|
8
|
+
* The state machine behind `useSelection`: which items are selected, by key, and the selection mode. `source` is
|
|
9
|
+
* read on every change, so it can change between renders. Updates that change nothing shown publish nothing.
|
|
10
|
+
*/
|
|
11
|
+
class SelectionMachine {
|
|
12
|
+
source;
|
|
13
|
+
config;
|
|
14
|
+
state;
|
|
15
|
+
snapshot;
|
|
16
|
+
selectedSet = new Set();
|
|
17
|
+
/** The current item for each key, so an old copy of an item a row still holds never decides. */
|
|
18
|
+
itemsByKey;
|
|
19
|
+
visibleByKey;
|
|
20
|
+
/** The items became none while an action ran: the mode ends when it does. */
|
|
21
|
+
endsWhenActionEnds = false;
|
|
22
|
+
listeners = new Set();
|
|
23
|
+
constructor(source, config) {
|
|
24
|
+
this.source = source;
|
|
25
|
+
this.config = config;
|
|
26
|
+
(0, config_1.checkConfig)(config);
|
|
27
|
+
this.state = (0, state_1.initialState)(config);
|
|
28
|
+
this.snapshot = this.derive();
|
|
29
|
+
}
|
|
30
|
+
subscribe = (listener) => {
|
|
31
|
+
this.listeners.add(listener);
|
|
32
|
+
return () => this.listeners.delete(listener);
|
|
33
|
+
};
|
|
34
|
+
getSnapshot = () => this.snapshot;
|
|
35
|
+
isSelected = (item) => this.selectedSet.has(this.source().keyExtractor(item));
|
|
36
|
+
/** The items and visible items last given, or `undefined` for a list not given yet. */
|
|
37
|
+
get knownLists() {
|
|
38
|
+
return { items: this.state.items, visible: this.state.visible };
|
|
39
|
+
}
|
|
40
|
+
/** The Select action: turns the mode on with nothing selected. */
|
|
41
|
+
enter = () => {
|
|
42
|
+
if (this.config.activation === "always" || this.state.isActive)
|
|
43
|
+
return;
|
|
44
|
+
this.update({ isActive: true, ...state_1.CLEARED });
|
|
45
|
+
};
|
|
46
|
+
/** Done or Cancel: turns the mode off and clears the selection. Does nothing while an action runs. */
|
|
47
|
+
exit = () => {
|
|
48
|
+
if (!this.snapshot.handlesBack || this.state.isPerforming)
|
|
49
|
+
return;
|
|
50
|
+
this.update({ isActive: false, ...state_1.CLEARED, actionError: undefined });
|
|
51
|
+
};
|
|
52
|
+
/** Back: leaves the mode first, and returns whether it handled back, so the screen stays. */
|
|
53
|
+
back = () => {
|
|
54
|
+
if (!this.snapshot.handlesBack)
|
|
55
|
+
return false;
|
|
56
|
+
if (this.state.pending !== undefined)
|
|
57
|
+
this.cancelPendingAction();
|
|
58
|
+
else
|
|
59
|
+
this.exit();
|
|
60
|
+
return true;
|
|
61
|
+
};
|
|
62
|
+
/** A tap in the mode: selects the item, or deselects it (ending all on the server) if it is selected. */
|
|
63
|
+
toggle = (item) => {
|
|
64
|
+
if (!this.canChange() || !this.isSelectable(item))
|
|
65
|
+
return;
|
|
66
|
+
const key = this.source().keyExtractor(item);
|
|
67
|
+
const { selected } = this.state;
|
|
68
|
+
if (this.selectedSet.has(key)) {
|
|
69
|
+
this.changeSelection(selected.filter((selectedKey) => selectedKey !== key));
|
|
70
|
+
}
|
|
71
|
+
else if (this.config.kind === "single") {
|
|
72
|
+
this.changeSelection([key]);
|
|
73
|
+
}
|
|
74
|
+
else if (this.config.max !== undefined && selected.length >= this.config.max) {
|
|
75
|
+
this.source().announce({ kind: "limit", max: this.config.max });
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
this.changeSelection([...selected, key]);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
/** A long press: turns the mode on with this item selected, or toggles it in the mode. */
|
|
82
|
+
longPress = (item) => {
|
|
83
|
+
if (!this.isSelectable(item) || this.state.isPerforming)
|
|
84
|
+
return;
|
|
85
|
+
if (this.state.isActive) {
|
|
86
|
+
this.toggle(item);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
this.update({ isActive: true, ...state_1.CLEARED, selected: [this.source().keyExtractor(item)] });
|
|
90
|
+
this.announceCount();
|
|
91
|
+
};
|
|
92
|
+
/** Selects every visible item that isn't disabled, in list order, up to the maximum. Never loads more. */
|
|
93
|
+
selectAll = () => {
|
|
94
|
+
const shown = this.shownItems();
|
|
95
|
+
if (!this.canChange() || shown === undefined || this.config.kind === "single")
|
|
96
|
+
return;
|
|
97
|
+
const added = this.selectableKeys(shown).filter((key) => !this.selectedSet.has(key));
|
|
98
|
+
const room = this.config.max === undefined ? added.length : this.config.max - this.state.selected.length;
|
|
99
|
+
if (room > 0 && added.length > 0)
|
|
100
|
+
this.changeSelection([...this.state.selected, ...added.slice(0, room)]);
|
|
101
|
+
};
|
|
102
|
+
/** Clears the selection, all on the server too, and stays in the mode. */
|
|
103
|
+
deselectAll = () => {
|
|
104
|
+
if (this.canChange() && this.state.selected.length > 0)
|
|
105
|
+
this.changeSelection([]);
|
|
106
|
+
};
|
|
107
|
+
/** Makes the selection stand for every item on the server, once `canSelectAllOnServer`. */
|
|
108
|
+
selectAllOnServer = () => {
|
|
109
|
+
if (!this.snapshot.canSelectAllOnServer)
|
|
110
|
+
return;
|
|
111
|
+
this.update({ isAllOnServer: true });
|
|
112
|
+
this.announceCount();
|
|
113
|
+
};
|
|
114
|
+
/**
|
|
115
|
+
* The data, once loaded: drops every selected item that is gone or now disabled, and ends the mode when there
|
|
116
|
+
* are none (once any running action ends). All on the server, new items arrive selected.
|
|
117
|
+
*/
|
|
118
|
+
updateItems = (items) => {
|
|
119
|
+
// Only a change to none ends the mode: the same empty list again (Select on an empty list) changes nothing.
|
|
120
|
+
const becameNone = items.length === 0 && this.state.items?.length !== 0;
|
|
121
|
+
this.itemsByKey = (0, state_1.byKey)(items, this.source().keyExtractor);
|
|
122
|
+
const endsMode = becameNone && this.snapshot.handlesBack;
|
|
123
|
+
if (items.length > 0)
|
|
124
|
+
this.endsWhenActionEnds = false;
|
|
125
|
+
else if (endsMode && this.state.isPerforming)
|
|
126
|
+
this.endsWhenActionEnds = true;
|
|
127
|
+
if (endsMode && !this.state.isPerforming) {
|
|
128
|
+
this.update({ items, isActive: false, ...state_1.CLEARED });
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
const selectable = this.selectableKeys(items);
|
|
132
|
+
const kept = new Set(selectable);
|
|
133
|
+
const selected = this.state.selected.filter((key) => kept.has(key));
|
|
134
|
+
if (items.length === 0) {
|
|
135
|
+
this.update({ items, selected, isAllOnServer: false });
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
if (this.state.isAllOnServer) {
|
|
139
|
+
const already = new Set(selected);
|
|
140
|
+
selected.push(...selectable.filter((key) => !already.has(key)));
|
|
141
|
+
}
|
|
142
|
+
this.update({ items, selected });
|
|
143
|
+
};
|
|
144
|
+
/**
|
|
145
|
+
* What a filter or search shows: a selected item missing from it is hidden, not dropped. Drops only visible
|
|
146
|
+
* selected items that are now disabled, and never ends the mode. `undefined` clears the filter.
|
|
147
|
+
*/
|
|
148
|
+
updateVisibleItems = (visible) => {
|
|
149
|
+
const { keyExtractor, isDisabled } = this.source();
|
|
150
|
+
if (visible === undefined) {
|
|
151
|
+
this.visibleByKey = undefined;
|
|
152
|
+
this.update({ visible });
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
this.visibleByKey = (0, state_1.byKey)(visible, keyExtractor);
|
|
156
|
+
const disabled = new Set(visible.filter(isDisabled).map(keyExtractor));
|
|
157
|
+
this.update({ visible, selected: this.state.selected.filter((key) => !disabled.has(key)) });
|
|
158
|
+
};
|
|
159
|
+
/** Runs `action` on the selection once; clears it if it succeeds, keeps it and reports the error if it fails. */
|
|
160
|
+
perform = (action) => {
|
|
161
|
+
if (!this.canStartAction())
|
|
162
|
+
return;
|
|
163
|
+
const keys = this.state.selected;
|
|
164
|
+
const context = { isAllOnServer: this.snapshot.isAllOnServerSelected };
|
|
165
|
+
this.update({ isPerforming: true, actionError: undefined });
|
|
166
|
+
// The executor runs `action` now and turns a synchronous throw into a failure.
|
|
167
|
+
new Promise((resolve) => resolve(action(keys, context))).then(() => {
|
|
168
|
+
this.endsWhenActionEnds = false;
|
|
169
|
+
this.update({ isPerforming: false, ...state_1.CLEARED, isActive: this.config.activation === "always" });
|
|
170
|
+
}, (error) => this.actionFailed(error));
|
|
171
|
+
};
|
|
172
|
+
/** Asks for a confirmation before `action`: nothing runs until `confirmPendingAction()`. */
|
|
173
|
+
performAfterConfirmation = (action) => {
|
|
174
|
+
if (this.canStartAction())
|
|
175
|
+
this.update({ pending: action });
|
|
176
|
+
};
|
|
177
|
+
/** Runs the action waiting for confirmation on the selection as it is now; with nothing selected, runs nothing. */
|
|
178
|
+
confirmPendingAction = () => {
|
|
179
|
+
const { pending } = this.state;
|
|
180
|
+
if (pending === undefined)
|
|
181
|
+
return;
|
|
182
|
+
this.update({ pending: undefined });
|
|
183
|
+
this.perform(pending);
|
|
184
|
+
};
|
|
185
|
+
cancelPendingAction = () => {
|
|
186
|
+
if (this.state.pending !== undefined)
|
|
187
|
+
this.update({ pending: undefined });
|
|
188
|
+
};
|
|
189
|
+
dismissActionError = () => {
|
|
190
|
+
if (this.state.actionError !== undefined)
|
|
191
|
+
this.update({ actionError: undefined });
|
|
192
|
+
};
|
|
193
|
+
/** Publishes what a changed `source` (a new `serverTotal`, say) changes. */
|
|
194
|
+
sourceChanged = () => this.update({});
|
|
195
|
+
actionFailed(error) {
|
|
196
|
+
this.update({ isPerforming: false, actionError: { error } });
|
|
197
|
+
// Items that became none while the action ran end the mode now; the error stays for the app to show.
|
|
198
|
+
if (!this.endsWhenActionEnds)
|
|
199
|
+
return;
|
|
200
|
+
this.endsWhenActionEnds = false;
|
|
201
|
+
this.update({ isActive: false, ...state_1.CLEARED });
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Not disabled and in the items once they are known (they decide), or else in the visible items: a row left from
|
|
205
|
+
* a removed item selects nothing.
|
|
206
|
+
*/
|
|
207
|
+
isSelectable(item) {
|
|
208
|
+
const { keyExtractor, isDisabled } = this.source();
|
|
209
|
+
const known = this.itemsByKey ?? this.visibleByKey;
|
|
210
|
+
if (known === undefined)
|
|
211
|
+
return !isDisabled(item);
|
|
212
|
+
const current = known.get(keyExtractor(item));
|
|
213
|
+
return current !== undefined && !isDisabled(current);
|
|
214
|
+
}
|
|
215
|
+
/** What select all and the total cover: the data's copies of the visible items still in it, or else either list. */
|
|
216
|
+
shownItems() {
|
|
217
|
+
const { items, visible } = this.state;
|
|
218
|
+
const itemsByKey = this.itemsByKey;
|
|
219
|
+
if (visible === undefined || itemsByKey === undefined)
|
|
220
|
+
return visible ?? items;
|
|
221
|
+
const { keyExtractor } = this.source();
|
|
222
|
+
return visible.flatMap((item) => {
|
|
223
|
+
const current = itemsByKey.get(keyExtractor(item));
|
|
224
|
+
return current === undefined ? [] : [current];
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
selectableKeys(items) {
|
|
228
|
+
const { keyExtractor, isDisabled } = this.source();
|
|
229
|
+
return items.filter((item) => !isDisabled(item)).map(keyExtractor);
|
|
230
|
+
}
|
|
231
|
+
canChange() {
|
|
232
|
+
return this.state.isActive && !this.state.isPerforming;
|
|
233
|
+
}
|
|
234
|
+
canStartAction() {
|
|
235
|
+
return this.canChange() && this.state.pending === undefined && this.state.selected.length > 0;
|
|
236
|
+
}
|
|
237
|
+
/** A change the person makes, announced; any deselection ends all on the server. */
|
|
238
|
+
changeSelection(selected) {
|
|
239
|
+
const isDeselection = selected.length < this.state.selected.length;
|
|
240
|
+
this.update({ selected, ...(isDeselection && { isAllOnServer: false }) });
|
|
241
|
+
this.announceCount();
|
|
242
|
+
}
|
|
243
|
+
announceCount() {
|
|
244
|
+
const { count, total } = this.snapshot;
|
|
245
|
+
this.source().announce({ kind: "count", count, total });
|
|
246
|
+
}
|
|
247
|
+
update(change) {
|
|
248
|
+
this.state = { ...this.state, ...change };
|
|
249
|
+
if (change.selected !== undefined)
|
|
250
|
+
this.selectedSet = new Set(this.state.selected);
|
|
251
|
+
const next = this.derive();
|
|
252
|
+
if ((0, snapshot_1.isSameSnapshot)(this.snapshot, next))
|
|
253
|
+
return;
|
|
254
|
+
this.snapshot = next;
|
|
255
|
+
for (const listener of this.listeners)
|
|
256
|
+
listener();
|
|
257
|
+
}
|
|
258
|
+
derive() {
|
|
259
|
+
const { visible, selected } = this.state;
|
|
260
|
+
const shown = this.shownItems();
|
|
261
|
+
const visibleByKey = this.visibleByKey;
|
|
262
|
+
return (0, snapshot_1.deriveSnapshot)({
|
|
263
|
+
...this.state,
|
|
264
|
+
config: this.config,
|
|
265
|
+
serverTotal: this.source().serverTotal,
|
|
266
|
+
isFiltered: visible !== undefined,
|
|
267
|
+
hiddenCount: visible === undefined || visibleByKey === undefined
|
|
268
|
+
? 0
|
|
269
|
+
: selected.filter((key) => !visibleByKey.has(key)).length,
|
|
270
|
+
shownSelectable: shown === undefined ? undefined : this.selectableKeys(shown).length,
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
exports.SelectionMachine = SelectionMachine;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { SelectionConfig } from "./config";
|
|
2
|
+
/** The last bulk action failed; the selection is still there. */
|
|
3
|
+
export interface SelectionActionError {
|
|
4
|
+
readonly error: unknown;
|
|
5
|
+
}
|
|
6
|
+
export interface SelectionSnapshot {
|
|
7
|
+
/** The selection mode is on: a tap toggles a row. */
|
|
8
|
+
readonly isActive: boolean;
|
|
9
|
+
/** The selected items' keys, in the order they were selected: loaded items only, even all on the server. */
|
|
10
|
+
readonly selectedKeys: readonly string[];
|
|
11
|
+
/** How many are selected; all on the server, the server's total. */
|
|
12
|
+
readonly count: number;
|
|
13
|
+
/**
|
|
14
|
+
* How many can be selected: the visible items that aren't disabled, plus the hidden selected ones. Until the
|
|
15
|
+
* items are known, `count`; all on the server, the server's total.
|
|
16
|
+
*/
|
|
17
|
+
readonly total: number;
|
|
18
|
+
/** How many selected items a filter or search hides. */
|
|
19
|
+
readonly hiddenCount: number;
|
|
20
|
+
/** Nothing more can be selected: every visible item that isn't disabled is, or the maximum is reached. */
|
|
21
|
+
readonly isAllSelected: boolean;
|
|
22
|
+
/** Select all on the server is offered: every visible item is selected, and the server has more. */
|
|
23
|
+
readonly canSelectAllOnServer: boolean;
|
|
24
|
+
/** The selection stands for every item on the server; the bulk action reads this. */
|
|
25
|
+
readonly isAllOnServerSelected: boolean;
|
|
26
|
+
/** Back leaves the selection mode instead of the screen. */
|
|
27
|
+
readonly handlesBack: boolean;
|
|
28
|
+
/** A destructive action waits for the person to confirm it. */
|
|
29
|
+
readonly isConfirming: boolean;
|
|
30
|
+
/** A bulk action is running; the selection can't change. */
|
|
31
|
+
readonly isPerforming: boolean;
|
|
32
|
+
readonly actionError: SelectionActionError | undefined;
|
|
33
|
+
}
|
|
34
|
+
/** Whether two snapshots show the same thing, so an update that changes nothing publishes nothing. */
|
|
35
|
+
export declare function isSameSnapshot(before: SelectionSnapshot, after: SelectionSnapshot): boolean;
|
|
36
|
+
/** What the machine knows, for `deriveSnapshot`. */
|
|
37
|
+
interface SnapshotInput {
|
|
38
|
+
readonly config: SelectionConfig;
|
|
39
|
+
readonly serverTotal: number | undefined;
|
|
40
|
+
readonly selected: readonly string[];
|
|
41
|
+
readonly isActive: boolean;
|
|
42
|
+
readonly isAllOnServer: boolean;
|
|
43
|
+
readonly pending: unknown;
|
|
44
|
+
readonly isPerforming: boolean;
|
|
45
|
+
readonly actionError: SelectionActionError | undefined;
|
|
46
|
+
/** Visible items were given: a filter or search may hide some. */
|
|
47
|
+
readonly isFiltered: boolean;
|
|
48
|
+
readonly hiddenCount: number;
|
|
49
|
+
/** How many shown items (the visible ones, or else the items) aren't disabled; `undefined` until known. */
|
|
50
|
+
readonly shownSelectable: number | undefined;
|
|
51
|
+
}
|
|
52
|
+
/** The snapshot of a selection: counts, totals and what is offered, from what the machine knows. */
|
|
53
|
+
export declare function deriveSnapshot(input: SnapshotInput): SelectionSnapshot;
|
|
54
|
+
export {};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isSameSnapshot = isSameSnapshot;
|
|
4
|
+
exports.deriveSnapshot = deriveSnapshot;
|
|
5
|
+
/** Whether two snapshots show the same thing, so an update that changes nothing publishes nothing. */
|
|
6
|
+
function isSameSnapshot(before, after) {
|
|
7
|
+
const keys = Object.keys(before);
|
|
8
|
+
return keys.every((key) => key === "selectedKeys" ? isSameKeys(before.selectedKeys, after.selectedKeys) : before[key] === after[key]);
|
|
9
|
+
}
|
|
10
|
+
function isSameKeys(before, after) {
|
|
11
|
+
return before.length === after.length && before.every((key, index) => key === after[index]);
|
|
12
|
+
}
|
|
13
|
+
/** The snapshot of a selection: counts, totals and what is offered, from what the machine knows. */
|
|
14
|
+
function deriveSnapshot(input) {
|
|
15
|
+
const { config, serverTotal, selected, isActive, isPerforming, hiddenCount } = input;
|
|
16
|
+
const loadedCount = selected.length;
|
|
17
|
+
const loadedTotal = input.shownSelectable === undefined ? loadedCount : input.shownSelectable + hiddenCount;
|
|
18
|
+
const isAtMax = config.max !== undefined && loadedCount >= config.max;
|
|
19
|
+
const isAllLoadedSelected = loadedCount > 0 && (loadedCount >= loadedTotal || isAtMax);
|
|
20
|
+
const isAllOnServerSelected = input.isAllOnServer && serverTotal !== undefined;
|
|
21
|
+
const count = isAllOnServerSelected ? serverTotal : loadedCount;
|
|
22
|
+
const total = isAllOnServerSelected ? serverTotal : loadedTotal;
|
|
23
|
+
return {
|
|
24
|
+
isActive,
|
|
25
|
+
selectedKeys: selected,
|
|
26
|
+
count,
|
|
27
|
+
total,
|
|
28
|
+
hiddenCount,
|
|
29
|
+
isAllSelected: count > 0 && (count >= total || isAtMax),
|
|
30
|
+
canSelectAllOnServer: isActive &&
|
|
31
|
+
!isPerforming &&
|
|
32
|
+
!isAllOnServerSelected &&
|
|
33
|
+
isAllLoadedSelected &&
|
|
34
|
+
config.kind === "multiple" &&
|
|
35
|
+
config.max === undefined &&
|
|
36
|
+
!input.isFiltered &&
|
|
37
|
+
serverTotal !== undefined &&
|
|
38
|
+
serverTotal > loadedCount,
|
|
39
|
+
isAllOnServerSelected,
|
|
40
|
+
handlesBack: isActive && config.activation === "onDemand",
|
|
41
|
+
isConfirming: input.pending !== undefined,
|
|
42
|
+
isPerforming,
|
|
43
|
+
actionError: input.actionError,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { SelectionAction, SelectionConfig } from "./config";
|
|
2
|
+
import type { SelectionActionError } from "./snapshot";
|
|
3
|
+
/** What a selection machine holds. */
|
|
4
|
+
export interface State<Item> {
|
|
5
|
+
/** The data, once loaded: an item gone from it drops out. `undefined` until then. */
|
|
6
|
+
readonly items: readonly Item[] | undefined;
|
|
7
|
+
/** What a filter or search shows: an item missing from it is only hidden. `undefined`: every item is visible. */
|
|
8
|
+
readonly visible: readonly Item[] | undefined;
|
|
9
|
+
readonly selected: readonly string[];
|
|
10
|
+
readonly isActive: boolean;
|
|
11
|
+
readonly isAllOnServer: boolean;
|
|
12
|
+
readonly pending: SelectionAction | undefined;
|
|
13
|
+
readonly isPerforming: boolean;
|
|
14
|
+
readonly actionError: SelectionActionError | undefined;
|
|
15
|
+
}
|
|
16
|
+
/** A change that leaves the mode with nothing selected. */
|
|
17
|
+
export declare const CLEARED: {
|
|
18
|
+
readonly selected: readonly [];
|
|
19
|
+
readonly isAllOnServer: false;
|
|
20
|
+
readonly pending: undefined;
|
|
21
|
+
};
|
|
22
|
+
/** Nothing known or selected yet; an always active selection starts in the mode. */
|
|
23
|
+
export declare function initialState<Item>(config: SelectionConfig): State<Item>;
|
|
24
|
+
/** Each item by its key. */
|
|
25
|
+
export declare function byKey<Item>(items: readonly Item[], keyExtractor: (item: Item) => string): ReadonlyMap<string, Item>;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CLEARED = void 0;
|
|
4
|
+
exports.initialState = initialState;
|
|
5
|
+
exports.byKey = byKey;
|
|
6
|
+
/** A change that leaves the mode with nothing selected. */
|
|
7
|
+
exports.CLEARED = { selected: [], isAllOnServer: false, pending: undefined };
|
|
8
|
+
/** Nothing known or selected yet; an always active selection starts in the mode. */
|
|
9
|
+
function initialState(config) {
|
|
10
|
+
return {
|
|
11
|
+
items: undefined,
|
|
12
|
+
visible: undefined,
|
|
13
|
+
selected: [],
|
|
14
|
+
isActive: config.activation === "always",
|
|
15
|
+
isAllOnServer: false,
|
|
16
|
+
pending: undefined,
|
|
17
|
+
isPerforming: false,
|
|
18
|
+
actionError: undefined,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
/** Each item by its key. */
|
|
22
|
+
function byKey(items, keyExtractor) {
|
|
23
|
+
return new Map(items.map((item) => [keyExtractor(item), item]));
|
|
24
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type SelectionLabels } from "./labels";
|
|
2
|
+
import type { SelectionController } from "./use-selection";
|
|
3
|
+
interface SelectionButtonProps {
|
|
4
|
+
readonly selection: Pick<SelectionController<unknown>, "kind" | "activation" | "isActive" | "isPerforming" | "isAllSelected" | "enter" | "exit" | "selectAll" | "deselectAll">;
|
|
5
|
+
readonly labels?: Partial<SelectionLabels>;
|
|
6
|
+
}
|
|
7
|
+
/** Select outside the mode, Done inside it, so the mode never needs a long press. Nothing for a picker. */
|
|
8
|
+
export declare function SelectionModeButton({ selection, labels }: SelectionButtonProps): import("react").JSX.Element | null;
|
|
9
|
+
/** Select All, or Deselect All once nothing more can be selected. Nothing for a single selection. */
|
|
10
|
+
export declare function SelectAllButton({ selection, labels }: SelectionButtonProps): import("react").JSX.Element | null;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SelectionModeButton = SelectionModeButton;
|
|
4
|
+
exports.SelectAllButton = SelectAllButton;
|
|
5
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
6
|
+
const react_native_1 = require("react-native");
|
|
7
|
+
const labels_1 = require("./labels");
|
|
8
|
+
// System components only, with no styles of their own.
|
|
9
|
+
/** Select outside the mode, Done inside it, so the mode never needs a long press. Nothing for a picker. */
|
|
10
|
+
function SelectionModeButton({ selection, labels }) {
|
|
11
|
+
if (selection.activation === "always")
|
|
12
|
+
return null;
|
|
13
|
+
const { select, done } = { ...labels_1.defaultSelectionLabels, ...labels };
|
|
14
|
+
return ((0, jsx_runtime_1.jsx)(react_native_1.Button, { title: selection.isActive ? done : select, onPress: selection.isActive ? selection.exit : selection.enter, disabled: selection.isPerforming }));
|
|
15
|
+
}
|
|
16
|
+
/** Select All, or Deselect All once nothing more can be selected. Nothing for a single selection. */
|
|
17
|
+
function SelectAllButton({ selection, labels }) {
|
|
18
|
+
if (selection.kind === "single")
|
|
19
|
+
return null;
|
|
20
|
+
const { selectAll, deselectAll } = { ...labels_1.defaultSelectionLabels, ...labels };
|
|
21
|
+
return ((0, jsx_runtime_1.jsx)(react_native_1.Button, { title: selection.isAllSelected ? deselectAll : selectAll, onPress: selection.isAllSelected ? selection.deselectAll : selection.selectAll, disabled: !selection.isActive || selection.isPerforming }));
|
|
22
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { type AccessibilityActionEvent, type AccessibilityActionInfo, type AccessibilityState } from "react-native";
|
|
2
|
+
import type { SelectionAction, SelectionActivation, SelectionKind } from "./config";
|
|
3
|
+
import { type SelectionLabels } from "./labels";
|
|
4
|
+
import type { SelectionSnapshot } from "./snapshot";
|
|
5
|
+
export interface SelectionOptions<Item> {
|
|
6
|
+
/** The identity of an item: the selection keeps keys, so an item stays selected across refreshes and pages. */
|
|
7
|
+
readonly keyExtractor: (item: Item) => string;
|
|
8
|
+
/** `"multiple"` (the default) or `"single"`: at most one item. */
|
|
9
|
+
readonly kind?: SelectionKind;
|
|
10
|
+
/** The most items a multiple selection may have: a whole number, 1 or more. */
|
|
11
|
+
readonly max?: number;
|
|
12
|
+
/** `"onDemand"` (the default): a Select button or a long press turns the mode on. `"always"`: a picker. */
|
|
13
|
+
readonly activation?: SelectionActivation;
|
|
14
|
+
/** Items that can't be selected. */
|
|
15
|
+
readonly isDisabled?: (item: Item) => boolean;
|
|
16
|
+
/**
|
|
17
|
+
* The data of a list the app draws itself, kept in step as it changes: an item gone from it drops out. Pass it
|
|
18
|
+
* once loaded. `PagedList` gives its own.
|
|
19
|
+
*/
|
|
20
|
+
readonly items?: readonly Item[];
|
|
21
|
+
/** What a filter or search shows now: a selected item missing from it is hidden, and stays selected. */
|
|
22
|
+
readonly visibleItems?: readonly Item[];
|
|
23
|
+
/** How many items the server has, to offer select all on the server once every visible item is selected. */
|
|
24
|
+
readonly serverTotal?: number;
|
|
25
|
+
/** The text of the count title and the announcements. */
|
|
26
|
+
readonly labels?: Partial<SelectionLabels>;
|
|
27
|
+
}
|
|
28
|
+
/** Props for a row's `Pressable`: a tap, a long press, and its selected state for screen readers. */
|
|
29
|
+
export interface SelectionRowProps {
|
|
30
|
+
readonly onPress: () => void;
|
|
31
|
+
readonly onLongPress: () => void;
|
|
32
|
+
readonly disabled: boolean;
|
|
33
|
+
readonly accessibilityState: AccessibilityState;
|
|
34
|
+
readonly accessibilityActions: readonly AccessibilityActionInfo[];
|
|
35
|
+
readonly onAccessibilityAction: (event: AccessibilityActionEvent) => void;
|
|
36
|
+
}
|
|
37
|
+
export interface SelectionController<Item> extends SelectionSnapshot {
|
|
38
|
+
readonly kind: SelectionKind;
|
|
39
|
+
readonly activation: SelectionActivation;
|
|
40
|
+
/** "3 Selected", or "3 Selected, 1 Hidden" while a filter hides some, for the screen's title. */
|
|
41
|
+
readonly title: string;
|
|
42
|
+
readonly isSelected: (item: Item) => boolean;
|
|
43
|
+
readonly enter: () => void;
|
|
44
|
+
readonly exit: () => void;
|
|
45
|
+
readonly toggle: (item: Item) => void;
|
|
46
|
+
readonly longPress: (item: Item) => void;
|
|
47
|
+
readonly selectAll: () => void;
|
|
48
|
+
readonly deselectAll: () => void;
|
|
49
|
+
readonly selectAllOnServer: () => void;
|
|
50
|
+
/** Leaves the mode first; returns whether it handled back. Android's hardware back already calls it. */
|
|
51
|
+
readonly back: () => boolean;
|
|
52
|
+
/** The list's items, once loaded. Pass the `items` option or a `PagedList` instead where you can. */
|
|
53
|
+
readonly updateItems: (items: readonly Item[]) => void;
|
|
54
|
+
/** What a filter or search shows, or `undefined` to clear it. Pass the `visibleItems` option instead where you can. */
|
|
55
|
+
readonly updateVisibleItems: (items: readonly Item[] | undefined) => void;
|
|
56
|
+
readonly perform: (action: SelectionAction) => void;
|
|
57
|
+
readonly performAfterConfirmation: (action: SelectionAction) => void;
|
|
58
|
+
readonly confirmPendingAction: () => void;
|
|
59
|
+
readonly cancelPendingAction: () => void;
|
|
60
|
+
readonly dismissActionError: () => void;
|
|
61
|
+
/**
|
|
62
|
+
* Spread on a row's `Pressable`: in the mode a tap toggles the row, outside it `onPress` runs; a long press, or
|
|
63
|
+
* the screen reader's Select action, turns the mode on with the row selected.
|
|
64
|
+
*/
|
|
65
|
+
readonly rowProps: (item: Item, options?: {
|
|
66
|
+
readonly onPress?: () => void;
|
|
67
|
+
}) => SelectionRowProps;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Which items of a list are selected, by key, and the selection mode, for the app's own rows. Announces each
|
|
71
|
+
* change to screen readers and, while the mode is on, leaves it on Android's hardware back.
|
|
72
|
+
*/
|
|
73
|
+
export declare function useSelection<Item>(options: SelectionOptions<Item>): SelectionController<Item>;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useSelection = useSelection;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const react_native_1 = require("react-native");
|
|
6
|
+
const announce_1 = require("../announce");
|
|
7
|
+
const labels_1 = require("./labels");
|
|
8
|
+
const machine_1 = require("./machine");
|
|
9
|
+
const LONG_PRESS_ACTION = "longpress";
|
|
10
|
+
const notDisabled = () => false;
|
|
11
|
+
/**
|
|
12
|
+
* Which items of a list are selected, by key, and the selection mode, for the app's own rows. Announces each
|
|
13
|
+
* change to screen readers and, while the mode is on, leaves it on Android's hardware back.
|
|
14
|
+
*/
|
|
15
|
+
function useSelection(options) {
|
|
16
|
+
const labels = { ...labels_1.defaultSelectionLabels, ...options.labels };
|
|
17
|
+
const source = {
|
|
18
|
+
keyExtractor: options.keyExtractor,
|
|
19
|
+
isDisabled: options.isDisabled ?? notDisabled,
|
|
20
|
+
serverTotal: options.serverTotal,
|
|
21
|
+
announce: (announcement) => (0, announce_1.announce)((0, labels_1.announcementText)(announcement, labels)),
|
|
22
|
+
};
|
|
23
|
+
const latest = (0, react_1.useRef)(source);
|
|
24
|
+
(0, react_1.useLayoutEffect)(() => {
|
|
25
|
+
latest.current = source;
|
|
26
|
+
});
|
|
27
|
+
const [machine] = (0, react_1.useState)(() => new machine_1.SelectionMachine(() => latest.current, {
|
|
28
|
+
kind: options.kind ?? "multiple",
|
|
29
|
+
max: options.max,
|
|
30
|
+
activation: options.activation ?? "onDemand",
|
|
31
|
+
}));
|
|
32
|
+
const snapshot = (0, react_1.useSyncExternalStore)(machine.subscribe, machine.getSnapshot);
|
|
33
|
+
const { items, visibleItems } = options;
|
|
34
|
+
const hadVisibleItems = (0, react_1.useRef)(false);
|
|
35
|
+
// Every render, after `latest` takes the new options: a changed `isDisabled` or `serverTotal` counts as new items
|
|
36
|
+
// do. Each update publishes only what changed.
|
|
37
|
+
(0, react_1.useLayoutEffect)(() => {
|
|
38
|
+
const known = machine.knownLists;
|
|
39
|
+
const data = items ?? known.items;
|
|
40
|
+
if (data !== undefined)
|
|
41
|
+
machine.updateItems(data);
|
|
42
|
+
// Leaving the option out after giving it clears the filter; otherwise a list given to the method stays.
|
|
43
|
+
const visible = visibleItems ?? (hadVisibleItems.current ? undefined : known.visible);
|
|
44
|
+
if (visible !== undefined || hadVisibleItems.current)
|
|
45
|
+
machine.updateVisibleItems(visible);
|
|
46
|
+
hadVisibleItems.current = visibleItems !== undefined;
|
|
47
|
+
machine.sourceChanged();
|
|
48
|
+
});
|
|
49
|
+
(0, react_1.useEffect)(() => {
|
|
50
|
+
if (!snapshot.handlesBack)
|
|
51
|
+
return;
|
|
52
|
+
const subscription = react_native_1.BackHandler.addEventListener("hardwareBackPress", machine.back);
|
|
53
|
+
return () => subscription.remove();
|
|
54
|
+
}, [machine, snapshot.handlesBack]);
|
|
55
|
+
const title = snapshot.hiddenCount === 0
|
|
56
|
+
? labels.title(snapshot.count)
|
|
57
|
+
: labels.titleWithHidden(snapshot.count, snapshot.hiddenCount);
|
|
58
|
+
const selectLabel = labels.select;
|
|
59
|
+
// This render's predicate, not the one `latest` held at the last render, so rows follow it at once.
|
|
60
|
+
const { isDisabled } = source;
|
|
61
|
+
return (0, react_1.useMemo)(() => ({
|
|
62
|
+
...snapshot,
|
|
63
|
+
kind: machine.config.kind,
|
|
64
|
+
activation: machine.config.activation,
|
|
65
|
+
title,
|
|
66
|
+
isSelected: machine.isSelected,
|
|
67
|
+
enter: machine.enter,
|
|
68
|
+
exit: machine.exit,
|
|
69
|
+
toggle: machine.toggle,
|
|
70
|
+
longPress: machine.longPress,
|
|
71
|
+
selectAll: machine.selectAll,
|
|
72
|
+
deselectAll: machine.deselectAll,
|
|
73
|
+
selectAllOnServer: machine.selectAllOnServer,
|
|
74
|
+
back: machine.back,
|
|
75
|
+
updateItems: machine.updateItems,
|
|
76
|
+
updateVisibleItems: machine.updateVisibleItems,
|
|
77
|
+
perform: machine.perform,
|
|
78
|
+
performAfterConfirmation: machine.performAfterConfirmation,
|
|
79
|
+
confirmPendingAction: machine.confirmPendingAction,
|
|
80
|
+
cancelPendingAction: machine.cancelPendingAction,
|
|
81
|
+
dismissActionError: machine.dismissActionError,
|
|
82
|
+
rowProps: (item, rowOptions) => rowProps(machine, snapshot, item, selectLabel, isDisabled(item), rowOptions?.onPress),
|
|
83
|
+
}), [snapshot, machine, title, selectLabel, isDisabled]);
|
|
84
|
+
}
|
|
85
|
+
function rowProps(machine, snapshot, item, selectLabel, isDisabled, onPress) {
|
|
86
|
+
const { isActive } = snapshot;
|
|
87
|
+
const longPress = () => machine.longPress(item);
|
|
88
|
+
return {
|
|
89
|
+
// Read when pressed, so a row drawn before the mode changed still does the right thing.
|
|
90
|
+
onPress: () => (machine.getSnapshot().isActive ? machine.toggle(item) : onPress?.()),
|
|
91
|
+
onLongPress: longPress,
|
|
92
|
+
disabled: isActive && isDisabled,
|
|
93
|
+
accessibilityState: isActive ? { selected: machine.isSelected(item), disabled: isDisabled } : {},
|
|
94
|
+
accessibilityActions: isActive || isDisabled ? [] : [{ name: LONG_PRESS_ACTION, label: selectLabel }],
|
|
95
|
+
onAccessibilityAction: (event) => {
|
|
96
|
+
if (event.nativeEvent.actionName === LONG_PRESS_ACTION)
|
|
97
|
+
longPress();
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
}
|