@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,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.nearestIndex = nearestIndex;
|
|
4
|
+
exports.selectionAt = selectionAt;
|
|
5
|
+
exports.selectionNearest = selectionNearest;
|
|
6
|
+
/** The index of the value in `sorted` nearest `target`, the earlier on a tie, or `undefined` when there are none. */
|
|
7
|
+
function nearestIndex(sorted, target) {
|
|
8
|
+
if (sorted.length === 0)
|
|
9
|
+
return undefined;
|
|
10
|
+
let low = 0;
|
|
11
|
+
let high = sorted.length;
|
|
12
|
+
while (low < high) {
|
|
13
|
+
const middle = Math.floor((low + high) / 2);
|
|
14
|
+
if ((sorted[middle] ?? 0) < target)
|
|
15
|
+
low = middle + 1;
|
|
16
|
+
else
|
|
17
|
+
high = middle;
|
|
18
|
+
}
|
|
19
|
+
// `low` is the first value at or after the target; the one before it may be nearer.
|
|
20
|
+
if (low === sorted.length)
|
|
21
|
+
return low - 1;
|
|
22
|
+
const before = sorted[low - 1];
|
|
23
|
+
const after = sorted[low] ?? 0;
|
|
24
|
+
if (before !== undefined && target - before <= after - target)
|
|
25
|
+
return low - 1;
|
|
26
|
+
return low;
|
|
27
|
+
}
|
|
28
|
+
function selectionAt(data, index) {
|
|
29
|
+
const x = data.xValues[index];
|
|
30
|
+
if (x === undefined)
|
|
31
|
+
return undefined;
|
|
32
|
+
const points = data.series.flatMap((series) => {
|
|
33
|
+
const nearest = nearestIndex(series.points.map((point) => point.x), x);
|
|
34
|
+
const point = nearest === undefined ? undefined : series.points[nearest];
|
|
35
|
+
return point === undefined ? [] : [{ seriesId: series.id, seriesName: series.name, point }];
|
|
36
|
+
});
|
|
37
|
+
return { x, points, index };
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The selection at the x nearest `x`: across every series, the earlier point on a tie. For a gesture handler that
|
|
41
|
+
* already has an x (Victory Native's press state, say); the hook's `scrub` does this and ticks.
|
|
42
|
+
*/
|
|
43
|
+
function selectionNearest(data, x) {
|
|
44
|
+
const index = nearestIndex(data.xValues, x);
|
|
45
|
+
return index === undefined ? undefined : selectionAt(data, index);
|
|
46
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { ChartPoint, PlottedData } from "./data";
|
|
2
|
+
/** Which value compares a period with the one before: the last value (a price, a weight) or the total (steps). */
|
|
3
|
+
export type ChartComparisonMeasure = "last" | "total";
|
|
4
|
+
/** A period compared with the one before it, by the app's measure. */
|
|
5
|
+
export interface ChartComparison {
|
|
6
|
+
/** The previous period's measure. */
|
|
7
|
+
readonly previous: number;
|
|
8
|
+
/** This period's measure minus the previous one's. */
|
|
9
|
+
readonly change: number;
|
|
10
|
+
/** The change as a fraction of the previous measure, or `undefined` when that is 0. */
|
|
11
|
+
readonly relativeChange: number | undefined;
|
|
12
|
+
}
|
|
13
|
+
/** A series' figures, for a screen reader and for the app's own callouts. */
|
|
14
|
+
export interface ChartSeriesSummary {
|
|
15
|
+
readonly seriesId: string;
|
|
16
|
+
readonly name: string;
|
|
17
|
+
readonly first: ChartPoint;
|
|
18
|
+
readonly last: ChartPoint;
|
|
19
|
+
/** The lowest and highest points, the earliest when two tie. */
|
|
20
|
+
readonly low: ChartPoint;
|
|
21
|
+
readonly high: ChartPoint;
|
|
22
|
+
readonly total: number;
|
|
23
|
+
/** `last.y - first.y`. */
|
|
24
|
+
readonly change: number;
|
|
25
|
+
/** The change as a fraction of the first value (0.2 for 20%), or `undefined` when the first value is 0. */
|
|
26
|
+
readonly relativeChange: number | undefined;
|
|
27
|
+
readonly comparison: ChartComparison | undefined;
|
|
28
|
+
}
|
|
29
|
+
export declare function summaries(data: PlottedData, measure: ChartComparisonMeasure): readonly ChartSeriesSummary[];
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.summaries = summaries;
|
|
4
|
+
const total = (series) => series.points.reduce((sum, point) => sum + point.y, 0);
|
|
5
|
+
const relative = (change, base) => base === 0 ? undefined : change / Math.abs(base);
|
|
6
|
+
function measured(series, measure) {
|
|
7
|
+
const last = series.points.at(-1);
|
|
8
|
+
if (last === undefined)
|
|
9
|
+
return undefined;
|
|
10
|
+
return measure === "last" ? last.y : total(series);
|
|
11
|
+
}
|
|
12
|
+
function comparison(current, previous, measure) {
|
|
13
|
+
const now = measured(current, measure);
|
|
14
|
+
const before = previous === undefined ? undefined : measured(previous, measure);
|
|
15
|
+
if (now === undefined || before === undefined)
|
|
16
|
+
return undefined;
|
|
17
|
+
return { previous: before, change: now - before, relativeChange: relative(now - before, before) };
|
|
18
|
+
}
|
|
19
|
+
function summary(series, previous, measure) {
|
|
20
|
+
const first = series.points[0];
|
|
21
|
+
const last = series.points.at(-1);
|
|
22
|
+
if (first === undefined || last === undefined)
|
|
23
|
+
return undefined;
|
|
24
|
+
// Strict comparisons keep the first of equal values.
|
|
25
|
+
const low = series.points.reduce((lowest, point) => (point.y < lowest.y ? point : lowest));
|
|
26
|
+
const high = series.points.reduce((highest, point) => (point.y > highest.y ? point : highest));
|
|
27
|
+
return {
|
|
28
|
+
seriesId: series.id,
|
|
29
|
+
name: series.name,
|
|
30
|
+
first,
|
|
31
|
+
last,
|
|
32
|
+
low,
|
|
33
|
+
high,
|
|
34
|
+
total: total(series),
|
|
35
|
+
change: last.y - first.y,
|
|
36
|
+
relativeChange: relative(last.y - first.y, first.y),
|
|
37
|
+
comparison: comparison(series, previous, measure),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function summaries(data, measure) {
|
|
41
|
+
return data.series.flatMap((series) => {
|
|
42
|
+
const previous = data.previousPeriod.find((each) => each.id === series.id);
|
|
43
|
+
return summary(series, previous, measure) ?? [];
|
|
44
|
+
});
|
|
45
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { AccessibilityActionEvent, AccessibilityProps } from "react-native";
|
|
2
|
+
import { type ChartLabels, type ChartXKind } from "./labels";
|
|
3
|
+
import { type ChartInteractionSnapshot, type LoadChart } from "./machine";
|
|
4
|
+
import { type ChartComparisonMeasure, type ChartSeriesSummary } from "./summary";
|
|
5
|
+
export interface ChartInteractionOptions<Range> {
|
|
6
|
+
/** What the chart shows ("Apple share price"), read out first. */
|
|
7
|
+
readonly title: string;
|
|
8
|
+
/** The first range to load: one of the app's own values (`"1D"`, say). */
|
|
9
|
+
readonly initialRange: Range;
|
|
10
|
+
readonly comparesToPreviousPeriod?: boolean;
|
|
11
|
+
readonly load: LoadChart<Range>;
|
|
12
|
+
/** Whether the selection stays when the touch ends, until the app clears it or new series arrive. */
|
|
13
|
+
readonly keepsSelectionOnRelease?: boolean;
|
|
14
|
+
/** Which value compares a period with the previous one: the last (by default), or the total. */
|
|
15
|
+
readonly comparisonMeasure?: ChartComparisonMeasure;
|
|
16
|
+
/** What the x values are: numbers (by default), or times as epoch milliseconds. */
|
|
17
|
+
readonly xKind?: ChartXKind;
|
|
18
|
+
/** How an x value is read out. By default, in the person's locale, as `xKind` says. */
|
|
19
|
+
readonly formatX?: (x: number) => string;
|
|
20
|
+
/** How a y value is read out. By default, a number in the person's locale. */
|
|
21
|
+
readonly formatY?: (y: number) => string;
|
|
22
|
+
/** The locale of the default formatters; by default, the device's. */
|
|
23
|
+
readonly locale?: string;
|
|
24
|
+
/** Plays the selection tick: React Native has none of its own (`Haptics.selectionAsync`, say). */
|
|
25
|
+
readonly haptic?: () => void;
|
|
26
|
+
readonly isOffline?: (error: unknown) => boolean;
|
|
27
|
+
readonly labels?: Partial<ChartLabels>;
|
|
28
|
+
}
|
|
29
|
+
/** Props for the view around the chart: one adjustable element for VoiceOver and TalkBack. */
|
|
30
|
+
export interface ChartAccessibilityProps extends Pick<AccessibilityProps, "accessible" | "accessibilityRole" | "accessibilityLabel" | "accessibilityValue" | "accessibilityActions"> {
|
|
31
|
+
readonly onAccessibilityAction: (event: AccessibilityActionEvent) => void;
|
|
32
|
+
}
|
|
33
|
+
export interface ChartInteractionController<Range> extends ChartInteractionSnapshot<Range> {
|
|
34
|
+
readonly range: Range;
|
|
35
|
+
readonly comparesToPreviousPeriod: boolean;
|
|
36
|
+
/** Each series' first, last, low, high and change, for the series on screen. */
|
|
37
|
+
readonly summaries: readonly ChartSeriesSummary[];
|
|
38
|
+
/** The title, then each series' change, low and high, and comparison, as a screen reader hears it. */
|
|
39
|
+
readonly spokenSummary: string;
|
|
40
|
+
/** The selected point's text: its x and each series' value. */
|
|
41
|
+
readonly selectionDescription: string | undefined;
|
|
42
|
+
readonly accessibilityProps: ChartAccessibilityProps;
|
|
43
|
+
readonly select: (range: Range) => void;
|
|
44
|
+
readonly compareToPreviousPeriod: (isOn: boolean) => void;
|
|
45
|
+
/** From the app's gesture handler: selects the point nearest `x`, ticking when the point changes. */
|
|
46
|
+
readonly scrub: (x: number) => void;
|
|
47
|
+
/** From the app's gesture handler, when the touch ends. */
|
|
48
|
+
readonly endScrub: () => void;
|
|
49
|
+
readonly clearSelection: () => void;
|
|
50
|
+
readonly selectNextPoint: () => void;
|
|
51
|
+
readonly selectPreviousPoint: () => void;
|
|
52
|
+
readonly retry: () => void;
|
|
53
|
+
readonly refresh: () => Promise<void>;
|
|
54
|
+
readonly dismissRefreshError: () => void;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* The behavior around a chart the app draws itself (with Victory Native, say): the selected range's series as a
|
|
58
|
+
* screen state, range switching that keeps the old series on screen while the new ones load, a touch that snaps to
|
|
59
|
+
* the nearest point, and a summary and point-by-point values for screen readers. Loads on mount, aborts on unmount,
|
|
60
|
+
* and always uses the latest options.
|
|
61
|
+
*/
|
|
62
|
+
export declare function useChartInteraction<Range>(options: ChartInteractionOptions<Range>): ChartInteractionController<Range>;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useChartInteraction = useChartInteraction;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const errors_1 = require("../screen-states/errors");
|
|
6
|
+
const labels_1 = require("./labels");
|
|
7
|
+
const machine_1 = require("./machine");
|
|
8
|
+
const summary_1 = require("./summary");
|
|
9
|
+
const ADJUSTABLE_ACTIONS = [{ name: "increment" }, { name: "decrement" }];
|
|
10
|
+
/**
|
|
11
|
+
* The behavior around a chart the app draws itself (with Victory Native, say): the selected range's series as a
|
|
12
|
+
* screen state, range switching that keeps the old series on screen while the new ones load, a touch that snaps to
|
|
13
|
+
* the nearest point, and a summary and point-by-point values for screen readers. Loads on mount, aborts on unmount,
|
|
14
|
+
* and always uses the latest options.
|
|
15
|
+
*/
|
|
16
|
+
function useChartInteraction(options) {
|
|
17
|
+
const source = {
|
|
18
|
+
load: options.load,
|
|
19
|
+
isOffline: options.isOffline ?? errors_1.isOfflineError,
|
|
20
|
+
haptic: options.haptic,
|
|
21
|
+
keepsSelectionOnRelease: options.keepsSelectionOnRelease ?? false,
|
|
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.ChartInteractionMachine(() => latest.current, {
|
|
28
|
+
range: options.initialRange,
|
|
29
|
+
comparesToPreviousPeriod: options.comparesToPreviousPeriod ?? false,
|
|
30
|
+
}));
|
|
31
|
+
const snapshot = (0, react_1.useSyncExternalStore)(machine.subscribe, machine.getSnapshot);
|
|
32
|
+
(0, react_1.useEffect)(() => {
|
|
33
|
+
machine.appear();
|
|
34
|
+
return machine.disappear;
|
|
35
|
+
}, [machine]);
|
|
36
|
+
const { title, formatX, formatY, xKind, locale, comparisonMeasure = "last" } = options;
|
|
37
|
+
const labels = { ...labels_1.defaultChartLabels, ...options.labels };
|
|
38
|
+
const format = (0, react_1.useMemo)(() => (0, labels_1.chartFormat)({ formatX, formatY, xKind, locale }), [formatX, formatY, xKind, locale]);
|
|
39
|
+
const { state, isStale, selection } = snapshot;
|
|
40
|
+
const data = state.status === "content" ? state.value : undefined;
|
|
41
|
+
const seriesSummaries = (0, react_1.useMemo)(() => (data === undefined ? [] : (0, summary_1.summaries)(data, comparisonMeasure)), [data, comparisonMeasure]);
|
|
42
|
+
const summary = (0, labels_1.spokenSummary)(title, seriesSummaries, isStale, labels, format);
|
|
43
|
+
const description = selection === undefined || data === undefined
|
|
44
|
+
? undefined
|
|
45
|
+
: (0, labels_1.pointText)(selection, data.series.length, labels, format);
|
|
46
|
+
return {
|
|
47
|
+
...snapshot,
|
|
48
|
+
...queryOf(snapshot.query),
|
|
49
|
+
summaries: seriesSummaries,
|
|
50
|
+
spokenSummary: summary,
|
|
51
|
+
selectionDescription: description,
|
|
52
|
+
accessibilityProps: {
|
|
53
|
+
accessible: true,
|
|
54
|
+
accessibilityRole: "adjustable",
|
|
55
|
+
accessibilityLabel: summary,
|
|
56
|
+
accessibilityValue: description === undefined ? {} : { text: description },
|
|
57
|
+
accessibilityActions: ADJUSTABLE_ACTIONS,
|
|
58
|
+
onAccessibilityAction: (event) => {
|
|
59
|
+
if (event.nativeEvent.actionName === "increment")
|
|
60
|
+
machine.selectNextPoint();
|
|
61
|
+
if (event.nativeEvent.actionName === "decrement")
|
|
62
|
+
machine.selectPreviousPoint();
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
select: machine.select,
|
|
66
|
+
compareToPreviousPeriod: machine.compareToPreviousPeriod,
|
|
67
|
+
scrub: machine.scrub,
|
|
68
|
+
endScrub: machine.endScrub,
|
|
69
|
+
clearSelection: machine.clearSelection,
|
|
70
|
+
selectNextPoint: machine.selectNextPoint,
|
|
71
|
+
selectPreviousPoint: machine.selectPreviousPoint,
|
|
72
|
+
retry: machine.retry,
|
|
73
|
+
refresh: machine.refresh,
|
|
74
|
+
dismissRefreshError: machine.dismissRefreshError,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
function queryOf(query) {
|
|
78
|
+
return { range: query.range, comparesToPreviousPeriod: query.comparesToPreviousPeriod };
|
|
79
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { AsyncFormRule } from "./rules";
|
|
2
|
+
/** What `AsyncChecks` needs to know about the form. */
|
|
3
|
+
export interface AsyncCheckTarget<F extends string> {
|
|
4
|
+
readonly asyncRule: (field: F) => AsyncFormRule | undefined;
|
|
5
|
+
/** Whether the field's value passes its rules, differs from its starting value, and has no answer yet. */
|
|
6
|
+
readonly needsCheck: (field: F) => boolean;
|
|
7
|
+
readonly checkedValue: (field: F) => string;
|
|
8
|
+
readonly answer: (field: F, value: string, message: string | undefined) => void;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* The async rules' checks, at most one per field: each waits for typing to pause, a change cancels it, and only
|
|
12
|
+
* the answer of a field's latest check is kept, even from a check that ignores its signal.
|
|
13
|
+
*/
|
|
14
|
+
export declare class AsyncChecks<F extends string> {
|
|
15
|
+
private readonly target;
|
|
16
|
+
private readonly waiting;
|
|
17
|
+
private readonly running;
|
|
18
|
+
private readonly generations;
|
|
19
|
+
constructor(target: AsyncCheckTarget<F>);
|
|
20
|
+
/** The field's value changed: cancels its check, and starts one after the debounce if it needs one. */
|
|
21
|
+
changed(field: F): void;
|
|
22
|
+
/**
|
|
23
|
+
* Runs a check still waiting for its debounce, or one the field needs, at once; resolves when it answers or is
|
|
24
|
+
* canceled. A field that no longer needs one has its check canceled.
|
|
25
|
+
*/
|
|
26
|
+
now(field: F): Promise<void>;
|
|
27
|
+
/** Waiting for its debounce or running. */
|
|
28
|
+
isPending(field: F): boolean;
|
|
29
|
+
cancel(field: F): void;
|
|
30
|
+
cancelAll(fields: readonly F[]): void;
|
|
31
|
+
private start;
|
|
32
|
+
private finish;
|
|
33
|
+
private generation;
|
|
34
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AsyncChecks = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* The async rules' checks, at most one per field: each waits for typing to pause, a change cancels it, and only
|
|
6
|
+
* the answer of a field's latest check is kept, even from a check that ignores its signal.
|
|
7
|
+
*/
|
|
8
|
+
class AsyncChecks {
|
|
9
|
+
target;
|
|
10
|
+
waiting = new Map();
|
|
11
|
+
running = new Map();
|
|
12
|
+
generations = new Map();
|
|
13
|
+
constructor(target) {
|
|
14
|
+
this.target = target;
|
|
15
|
+
}
|
|
16
|
+
/** The field's value changed: cancels its check, and starts one after the debounce if it needs one. */
|
|
17
|
+
changed(field) {
|
|
18
|
+
this.cancel(field);
|
|
19
|
+
const rule = this.target.asyncRule(field);
|
|
20
|
+
if (rule === undefined || !this.target.needsCheck(field))
|
|
21
|
+
return;
|
|
22
|
+
this.waiting.set(field, setTimeout(() => void this.start(field), rule.debounceMs));
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Runs a check still waiting for its debounce, or one the field needs, at once; resolves when it answers or is
|
|
26
|
+
* canceled. A field that no longer needs one has its check canceled.
|
|
27
|
+
*/
|
|
28
|
+
now(field) {
|
|
29
|
+
if (!this.target.needsCheck(field)) {
|
|
30
|
+
if (this.isPending(field))
|
|
31
|
+
this.cancel(field);
|
|
32
|
+
return Promise.resolve();
|
|
33
|
+
}
|
|
34
|
+
return this.running.get(field)?.done ?? this.start(field);
|
|
35
|
+
}
|
|
36
|
+
/** Waiting for its debounce or running. */
|
|
37
|
+
isPending(field) {
|
|
38
|
+
return this.waiting.has(field) || this.running.has(field);
|
|
39
|
+
}
|
|
40
|
+
cancel(field) {
|
|
41
|
+
clearTimeout(this.waiting.get(field));
|
|
42
|
+
this.waiting.delete(field);
|
|
43
|
+
this.running.get(field)?.controller.abort();
|
|
44
|
+
this.running.delete(field);
|
|
45
|
+
this.generations.set(field, this.generation(field) + 1);
|
|
46
|
+
}
|
|
47
|
+
cancelAll(fields) {
|
|
48
|
+
for (const field of fields)
|
|
49
|
+
this.cancel(field);
|
|
50
|
+
}
|
|
51
|
+
start(field) {
|
|
52
|
+
const rule = this.target.asyncRule(field);
|
|
53
|
+
if (rule === undefined)
|
|
54
|
+
return Promise.resolve();
|
|
55
|
+
this.cancel(field);
|
|
56
|
+
const generation = this.generation(field);
|
|
57
|
+
const value = this.target.checkedValue(field);
|
|
58
|
+
const controller = new AbortController();
|
|
59
|
+
// A check that fails, or throws before its promise, gives no message: the server checks again on submit.
|
|
60
|
+
const answered = new Promise((resolve) => resolve(rule.check(value, controller.signal))).then((message) => this.finish(field, generation, value, message), () => this.finish(field, generation, value, undefined));
|
|
61
|
+
// A canceled check that ignores its signal never holds up whoever waits for it.
|
|
62
|
+
const aborted = new Promise((resolve) => controller.signal.addEventListener("abort", () => resolve()));
|
|
63
|
+
const done = Promise.race([answered, aborted]);
|
|
64
|
+
this.running.set(field, { controller, done });
|
|
65
|
+
return done;
|
|
66
|
+
}
|
|
67
|
+
finish(field, generation, value, message) {
|
|
68
|
+
if (generation !== this.generation(field))
|
|
69
|
+
return;
|
|
70
|
+
this.running.delete(field);
|
|
71
|
+
this.target.answer(field, value, message);
|
|
72
|
+
}
|
|
73
|
+
generation(field) {
|
|
74
|
+
return this.generations.get(field) ?? 0;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
exports.AsyncChecks = AsyncChecks;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Throw from a submit's action to show the server's errors under their fields: `new FormFieldErrors({ email:
|
|
3
|
+
* "That email is taken" })`. Each shows at once and clears when its field changes.
|
|
4
|
+
*/
|
|
5
|
+
export declare class FormFieldErrors<F extends string> extends Error {
|
|
6
|
+
readonly messages: Readonly<Partial<Record<F, string>>>;
|
|
7
|
+
constructor(messages: Readonly<Partial<Record<F, string>>>);
|
|
8
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FormFieldErrors = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Throw from a submit's action to show the server's errors under their fields: `new FormFieldErrors({ email:
|
|
6
|
+
* "That email is taken" })`. Each shows at once and clears when its field changes.
|
|
7
|
+
*/
|
|
8
|
+
class FormFieldErrors extends Error {
|
|
9
|
+
messages;
|
|
10
|
+
constructor(messages) {
|
|
11
|
+
super(Object.values(messages).join(" "));
|
|
12
|
+
this.messages = messages;
|
|
13
|
+
this.name = "FormFieldErrors";
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.FormFieldErrors = FormFieldErrors;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { AsyncFormRule, FormRule, FormValues } from "./rules";
|
|
2
|
+
export interface FormFieldOptions<F extends string> {
|
|
3
|
+
/** Whether the value is checked and submitted without leading and trailing spaces; turn it off for passwords. */
|
|
4
|
+
readonly trim?: boolean;
|
|
5
|
+
/** Checked in order; the first one the value breaks gives the field's error. */
|
|
6
|
+
readonly rules?: readonly FormRule<F>[];
|
|
7
|
+
readonly asyncRule?: AsyncFormRule;
|
|
8
|
+
/** False while the field is hidden: it isn't checked, never blocks a submit, and Next skips it. True by default. */
|
|
9
|
+
readonly active?: boolean;
|
|
10
|
+
}
|
|
11
|
+
/** Every field, in keyboard order: the object's key order. */
|
|
12
|
+
export type FormFields<F extends string> = {
|
|
13
|
+
readonly [K in F]: FormFieldOptions<NoInfer<F>>;
|
|
14
|
+
};
|
|
15
|
+
/** The action of a submit that failed in a way that isn't for a field. */
|
|
16
|
+
export interface SubmitError {
|
|
17
|
+
readonly error: unknown;
|
|
18
|
+
}
|
|
19
|
+
export interface AsyncAnswer {
|
|
20
|
+
readonly value: string;
|
|
21
|
+
readonly message: string | undefined;
|
|
22
|
+
}
|
|
23
|
+
export interface FormSnapshot<F extends string> {
|
|
24
|
+
/** What is in each field, as typed. */
|
|
25
|
+
readonly values: Readonly<Record<F, string>>;
|
|
26
|
+
/** The initial values, then those of the last successful submit. */
|
|
27
|
+
readonly startingValues: Readonly<Record<F, string>>;
|
|
28
|
+
readonly touched: ReadonlySet<F>;
|
|
29
|
+
readonly isSubmitAttempted: boolean;
|
|
30
|
+
readonly isSubmitting: boolean;
|
|
31
|
+
readonly submitError: SubmitError | undefined;
|
|
32
|
+
readonly serverErrors: ReadonlyMap<F, string>;
|
|
33
|
+
readonly answers: ReadonlyMap<F, AsyncAnswer>;
|
|
34
|
+
}
|
|
35
|
+
export interface FormSource<F extends string> {
|
|
36
|
+
readonly fields: FormFields<F>;
|
|
37
|
+
readonly announce: (message: string) => void;
|
|
38
|
+
}
|
|
39
|
+
/** What the form moves focus to: a `TextInput`. */
|
|
40
|
+
export interface Focusable {
|
|
41
|
+
focus(): void;
|
|
42
|
+
}
|
|
43
|
+
export type SubmitAction<F extends string> = (values: FormValues<F>) => Promise<void>;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { Focusable, FormSnapshot, FormSource, SubmitAction } from "./form-types";
|
|
2
|
+
/**
|
|
3
|
+
* The state machine behind `useForm`. `source` is read whenever a rule runs, so rules can change between renders;
|
|
4
|
+
* the fields and their order can't.
|
|
5
|
+
*/
|
|
6
|
+
export declare class FormMachine<F extends string> {
|
|
7
|
+
private readonly source;
|
|
8
|
+
readonly order: readonly F[];
|
|
9
|
+
private snapshot;
|
|
10
|
+
private readonly listeners;
|
|
11
|
+
private readonly focused;
|
|
12
|
+
private readonly inputs;
|
|
13
|
+
private readonly refs;
|
|
14
|
+
private readonly checks;
|
|
15
|
+
/** Counts submits and resets, so a reset drops the answer of a submit still running. */
|
|
16
|
+
private submits;
|
|
17
|
+
constructor(source: () => FormSource<F>, order: readonly F[], initialValues: Readonly<Partial<Record<F, string>>> | undefined);
|
|
18
|
+
readonly subscribe: (listener: () => void) => (() => void);
|
|
19
|
+
readonly getSnapshot: () => FormSnapshot<F>;
|
|
20
|
+
/** Stops every async check, as the form unmounts. A running submit carries on. */
|
|
21
|
+
readonly disappear: () => void;
|
|
22
|
+
readonly setValue: (field: F, value: string) => void;
|
|
23
|
+
/**
|
|
24
|
+
* Starts the checks other fields now need (a rule matching this one may pass now) and cancels the ones they no
|
|
25
|
+
* longer need (an inactive field). Call after anything that changes which fields need a check.
|
|
26
|
+
*/
|
|
27
|
+
readonly syncChecks: () => void;
|
|
28
|
+
/** The error to show under the field now, or undefined. */
|
|
29
|
+
readonly error: (field: F) => string | undefined;
|
|
30
|
+
/** Whether the field's async rule hasn't answered for its value yet: waiting for typing to pause, or running. */
|
|
31
|
+
readonly isChecking: (field: F) => boolean;
|
|
32
|
+
readonly isValid: () => boolean;
|
|
33
|
+
readonly isDirty: () => boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Checks every field, waiting for async rules, and runs `action` with the values as checked if they all pass.
|
|
36
|
+
* Does nothing while a submit runs. Resolves with whether the action ran and succeeded.
|
|
37
|
+
*/
|
|
38
|
+
readonly submit: (action: SubmitAction<F>) => Promise<boolean>;
|
|
39
|
+
/** Puts the starting values back and clears every error, check and the submit error. */
|
|
40
|
+
readonly reset: () => void;
|
|
41
|
+
focusChanged(field: F, isFocused: boolean): void;
|
|
42
|
+
/** Return on the field: Next focuses the next field; Done, on the last, runs `onDone`. Both leave the field. */
|
|
43
|
+
submitEditing(field: F, onDone: (() => void) | undefined): void;
|
|
44
|
+
/** The next active field in keyboard order, or undefined for the last. */
|
|
45
|
+
fieldAfter(field: F): F | undefined;
|
|
46
|
+
/** The field's ref callback, the same one on every render. */
|
|
47
|
+
ref(field: F): (input: Focusable | null) => void;
|
|
48
|
+
/** Runs every check a field needs now, again while edits replace them, until none is needed. */
|
|
49
|
+
private finishChecks;
|
|
50
|
+
private leave;
|
|
51
|
+
private failure;
|
|
52
|
+
private focusFirstServerError;
|
|
53
|
+
private focusAndAnnounce;
|
|
54
|
+
private firstInvalid;
|
|
55
|
+
private isActive;
|
|
56
|
+
private ruleError;
|
|
57
|
+
private asyncError;
|
|
58
|
+
private needsCheck;
|
|
59
|
+
private checkedValues;
|
|
60
|
+
private checkedValue;
|
|
61
|
+
private options;
|
|
62
|
+
private update;
|
|
63
|
+
}
|