@10x-media/fields 0.1.0-beta.1 → 0.1.0-beta.10
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/CHANGELOG.md +120 -0
- package/README.md +7 -3
- package/dist/exports/client.d.ts +10 -1
- package/dist/exports/client.js +9 -1
- package/dist/exports/color-utils.d.ts +2 -1
- package/dist/exports/color-utils.js +2 -1
- package/dist/exports/color.d.ts +2 -1
- package/dist/exports/color.js +2 -1
- package/dist/exports/encrypted.d.ts +7 -4
- package/dist/exports/encrypted.js +6 -3
- package/dist/exports/measurement-utils.d.ts +9 -0
- package/dist/exports/measurement-utils.js +8 -0
- package/dist/exports/measurement.d.ts +11 -0
- package/dist/exports/measurement.js +10 -0
- package/dist/exports/phone-utils.d.ts +4 -0
- package/dist/exports/phone-utils.js +4 -0
- package/dist/exports/phone.d.ts +6 -0
- package/dist/exports/phone.js +3 -0
- package/dist/exports/rsc.d.ts +5 -1
- package/dist/exports/rsc.js +5 -1
- package/dist/exports/types.d.ts +3 -2
- package/dist/fields/color/client/ColorField.d.ts +2 -0
- package/dist/fields/color/client/ColorField.js +35 -293
- package/dist/fields/color/client/ColorField.js.map +1 -1
- package/dist/fields/color/client/ColorInput.d.ts +44 -0
- package/dist/fields/color/client/ColorInput.js +374 -0
- package/dist/fields/color/client/ColorInput.js.map +1 -0
- package/dist/fields/color/client/ColorPickerPanel.d.ts +3 -1
- package/dist/fields/color/client/ColorPickerPanel.js +29 -4
- package/dist/fields/color/client/ColorPickerPanel.js.map +1 -1
- package/dist/fields/color/client/colorField.css +30 -0
- package/dist/fields/color/colorField.js +36 -8
- package/dist/fields/color/colorField.js.map +1 -1
- package/dist/fields/color/presetChip.js +10 -9
- package/dist/fields/color/presetChip.js.map +1 -1
- package/dist/fields/color/presetReference.d.ts +13 -0
- package/dist/fields/color/presetReference.js +94 -0
- package/dist/fields/color/presetReference.js.map +1 -0
- package/dist/fields/color/server/ColorCell.js +7 -1
- package/dist/fields/color/server/ColorCell.js.map +1 -1
- package/dist/fields/color/server/colorCell.css +5 -0
- package/dist/fields/color/valueIssue.js +23 -0
- package/dist/fields/color/valueIssue.js.map +1 -0
- package/dist/fields/encrypted/boot.js +2 -1
- package/dist/fields/encrypted/boot.js.map +1 -1
- package/dist/fields/encrypted/components/EncryptedInput.js +4 -1
- package/dist/fields/encrypted/components/EncryptedInput.js.map +1 -1
- package/dist/fields/encrypted/components/EncryptedTextarea.js +6 -2
- package/dist/fields/encrypted/components/EncryptedTextarea.js.map +1 -1
- package/dist/fields/encrypted/components/ProtectedCell.d.ts +15 -6
- package/dist/fields/encrypted/components/ProtectedCell.js +37 -8
- package/dist/fields/encrypted/components/ProtectedCell.js.map +1 -1
- package/dist/fields/encrypted/components/ProtectedField.css +134 -39
- package/dist/fields/encrypted/components/ProtectedField.d.ts +3 -0
- package/dist/fields/encrypted/components/ProtectedField.js +14 -0
- package/dist/fields/encrypted/components/ProtectedField.js.map +1 -1
- package/dist/fields/encrypted/components/WriteOnlyActions.js +65 -0
- package/dist/fields/encrypted/components/WriteOnlyActions.js.map +1 -0
- package/dist/fields/encrypted/components/WriteOnlyField.js +286 -0
- package/dist/fields/encrypted/components/WriteOnlyField.js.map +1 -0
- package/dist/fields/encrypted/components/writeOnlyIntent.js +69 -0
- package/dist/fields/encrypted/components/writeOnlyIntent.js.map +1 -0
- package/dist/fields/encrypted/crypto/wire.d.ts +10 -1
- package/dist/fields/encrypted/crypto/wire.js +1 -1
- package/dist/fields/encrypted/encryptedField.js +63 -6
- package/dist/fields/encrypted/encryptedField.js.map +1 -1
- package/dist/fields/encrypted/generateSecret.d.ts +10 -0
- package/dist/fields/encrypted/generateSecret.js +41 -0
- package/dist/fields/encrypted/generateSecret.js.map +1 -0
- package/dist/fields/encrypted/hint.js +67 -0
- package/dist/fields/encrypted/hint.js.map +1 -0
- package/dist/fields/encrypted/hooks.js +59 -31
- package/dist/fields/encrypted/hooks.js.map +1 -1
- package/dist/fields/encrypted/queryRewrite.d.ts +10 -2
- package/dist/fields/encrypted/queryRewrite.js +34 -8
- package/dist/fields/encrypted/queryRewrite.js.map +1 -1
- package/dist/fields/encrypted/rawRead.d.ts +24 -0
- package/dist/fields/encrypted/rawRead.js +40 -0
- package/dist/fields/encrypted/rawRead.js.map +1 -0
- package/dist/fields/encrypted/scan.js +21 -4
- package/dist/fields/encrypted/scan.js.map +1 -1
- package/dist/fields/encrypted/types.d.ts +67 -2
- package/dist/fields/encrypted/types.js +8 -1
- package/dist/fields/encrypted/types.js.map +1 -1
- package/dist/fields/encrypted/utils/decryptAllData.js +2 -2
- package/dist/fields/encrypted/utils/decryptAllData.js.map +1 -1
- package/dist/fields/encrypted/utils/decryptFieldValue.d.ts +30 -0
- package/dist/fields/encrypted/utils/decryptFieldValue.js +56 -0
- package/dist/fields/encrypted/utils/decryptFieldValue.js.map +1 -0
- package/dist/fields/encrypted/utils/readEncryptedField.d.ts +51 -0
- package/dist/fields/encrypted/utils/readEncryptedField.js +96 -0
- package/dist/fields/encrypted/utils/readEncryptedField.js.map +1 -0
- package/dist/fields/icon/adapters/lucide/generated/manifest.js.map +1 -1
- package/dist/fields/icon/adapters/lucide/generated/nodes.js.map +1 -1
- package/dist/fields/icon/adapters/radix/generated/manifest.js.map +1 -1
- package/dist/fields/icon/adapters/radix/generated/nodes.js.map +1 -1
- package/dist/fields/icon/adapters/tabler/generated/manifest.js.map +1 -1
- package/dist/fields/icon/adapters/tabler/generated/nodes.js.map +1 -1
- package/dist/fields/icon/client/icon-field.css +4 -2
- package/dist/fields/icon/codegen/emit.js +2 -2
- package/dist/fields/icon/codegen/emit.js.map +1 -1
- package/dist/fields/icon/rsc/IconFieldServer.js +1 -1
- package/dist/fields/icon/rsc/IconFieldServer.js.map +1 -1
- package/dist/fields/icon/rsc/IconLibrarySelectServer.js +1 -1
- package/dist/fields/icon/rsc/IconLibrarySelectServer.js.map +1 -1
- package/dist/fields/icon/shared/iconLabel.js +1 -1
- package/dist/fields/icon/shared/iconLabel.js.map +1 -1
- package/dist/fields/measurement/client/MeasurementCell.d.ts +18 -0
- package/dist/fields/measurement/client/MeasurementCell.js +47 -0
- package/dist/fields/measurement/client/MeasurementCell.js.map +1 -0
- package/dist/fields/measurement/client/MeasurementField.d.ts +13 -0
- package/dist/fields/measurement/client/MeasurementField.js +70 -0
- package/dist/fields/measurement/client/MeasurementField.js.map +1 -0
- package/dist/fields/measurement/client/MeasurementInput.d.ts +42 -0
- package/dist/fields/measurement/client/MeasurementInput.js +327 -0
- package/dist/fields/measurement/client/MeasurementInput.js.map +1 -0
- package/dist/fields/measurement/client/MeasurementUnitsProvider.d.ts +24 -0
- package/dist/fields/measurement/client/MeasurementUnitsProvider.js +61 -0
- package/dist/fields/measurement/client/MeasurementUnitsProvider.js.map +1 -0
- package/dist/fields/measurement/client/editModel.js +124 -0
- package/dist/fields/measurement/client/editModel.js.map +1 -0
- package/dist/fields/measurement/client/measurementCell.css +4 -0
- package/dist/fields/measurement/client/measurementField.css +215 -0
- package/dist/fields/measurement/client/useMeasurementModel.d.ts +19 -0
- package/dist/fields/measurement/client/useMeasurementModel.js +85 -0
- package/dist/fields/measurement/client/useMeasurementModel.js.map +1 -0
- package/dist/fields/measurement/engine/convert.d.ts +8 -0
- package/dist/fields/measurement/engine/convert.js +21 -0
- package/dist/fields/measurement/engine/convert.js.map +1 -0
- package/dist/fields/measurement/engine/format.d.ts +37 -0
- package/dist/fields/measurement/engine/format.js +156 -0
- package/dist/fields/measurement/engine/format.js.map +1 -0
- package/dist/fields/measurement/engine/locale.d.ts +25 -0
- package/dist/fields/measurement/engine/locale.js +79 -0
- package/dist/fields/measurement/engine/locale.js.map +1 -0
- package/dist/fields/measurement/engine/precision.d.ts +34 -0
- package/dist/fields/measurement/engine/precision.js +63 -0
- package/dist/fields/measurement/engine/precision.js.map +1 -0
- package/dist/fields/measurement/engine/registry.d.ts +55 -0
- package/dist/fields/measurement/engine/registry.js +117 -0
- package/dist/fields/measurement/engine/registry.js.map +1 -0
- package/dist/fields/measurement/engine/units.d.ts +178 -0
- package/dist/fields/measurement/engine/units.js +198 -0
- package/dist/fields/measurement/engine/units.js.map +1 -0
- package/dist/fields/measurement/measurementField.d.ts +17 -0
- package/dist/fields/measurement/measurementField.js +139 -0
- package/dist/fields/measurement/measurementField.js.map +1 -0
- package/dist/fields/measurement/options.d.ts +76 -0
- package/dist/fields/measurement/options.js +9 -0
- package/dist/fields/measurement/options.js.map +1 -0
- package/dist/fields/measurement/presets.d.ts +117 -0
- package/dist/fields/measurement/presets.js +136 -0
- package/dist/fields/measurement/presets.js.map +1 -0
- package/dist/fields/measurement/server/MeasurementCellServer.d.ts +21 -0
- package/dist/fields/measurement/server/MeasurementCellServer.js +53 -0
- package/dist/fields/measurement/server/MeasurementCellServer.js.map +1 -0
- package/dist/fields/measurement/server/MeasurementFieldServer.d.ts +16 -0
- package/dist/fields/measurement/server/MeasurementFieldServer.js +45 -0
- package/dist/fields/measurement/server/MeasurementFieldServer.js.map +1 -0
- package/dist/fields/measurement/server/getUserUnits.js +39 -0
- package/dist/fields/measurement/server/getUserUnits.js.map +1 -0
- package/dist/fields/measurement/server/resolvePrecisionSafe.js +24 -0
- package/dist/fields/measurement/server/resolvePrecisionSafe.js.map +1 -0
- package/dist/fields/measurement/server/resolveRegistryDefault.js +26 -0
- package/dist/fields/measurement/server/resolveRegistryDefault.js.map +1 -0
- package/dist/fields/phoneNumber/client/CountryFlag.d.ts +16 -0
- package/dist/fields/phoneNumber/client/CountryFlag.js +40 -0
- package/dist/fields/phoneNumber/client/CountryFlag.js.map +1 -0
- package/dist/fields/phoneNumber/client/CountryPicker.d.ts +21 -0
- package/dist/fields/phoneNumber/client/CountryPicker.js +255 -0
- package/dist/fields/phoneNumber/client/CountryPicker.js.map +1 -0
- package/dist/fields/phoneNumber/client/PhoneNumberField.d.ts +26 -0
- package/dist/fields/phoneNumber/client/PhoneNumberField.js +318 -0
- package/dist/fields/phoneNumber/client/PhoneNumberField.js.map +1 -0
- package/dist/fields/phoneNumber/client/editModel.js +86 -0
- package/dist/fields/phoneNumber/client/editModel.js.map +1 -0
- package/dist/fields/phoneNumber/client/phoneNumberField.css +305 -0
- package/dist/fields/phoneNumber/engine/countries.d.ts +36 -0
- package/dist/fields/phoneNumber/engine/countries.js +68 -0
- package/dist/fields/phoneNumber/engine/countries.js.map +1 -0
- package/dist/fields/phoneNumber/engine/draft.d.ts +13 -0
- package/dist/fields/phoneNumber/engine/draft.js +72 -0
- package/dist/fields/phoneNumber/engine/draft.js.map +1 -0
- package/dist/fields/phoneNumber/engine/metadata.d.ts +11 -0
- package/dist/fields/phoneNumber/engine/metadata.js +32 -0
- package/dist/fields/phoneNumber/engine/metadata.js.map +1 -0
- package/dist/fields/phoneNumber/engine/phone.d.ts +37 -0
- package/dist/fields/phoneNumber/engine/phone.js +59 -0
- package/dist/fields/phoneNumber/engine/phone.js.map +1 -0
- package/dist/fields/phoneNumber/options.d.ts +72 -0
- package/dist/fields/phoneNumber/options.js +37 -0
- package/dist/fields/phoneNumber/options.js.map +1 -0
- package/dist/fields/phoneNumber/phoneNumberField.d.ts +14 -0
- package/dist/fields/phoneNumber/phoneNumberField.js +167 -0
- package/dist/fields/phoneNumber/phoneNumberField.js.map +1 -0
- package/dist/fields/phoneNumber/server/PhoneNumberCellServer.d.ts +19 -0
- package/dist/fields/phoneNumber/server/PhoneNumberCellServer.js +73 -0
- package/dist/fields/phoneNumber/server/PhoneNumberCellServer.js.map +1 -0
- package/dist/fields/phoneNumber/server/PhoneNumberFieldServer.d.ts +16 -0
- package/dist/fields/phoneNumber/server/PhoneNumberFieldServer.js +57 -0
- package/dist/fields/phoneNumber/server/PhoneNumberFieldServer.js.map +1 -0
- package/dist/fields/phoneNumber/server/flagsEndpoint.js +51 -0
- package/dist/fields/phoneNumber/server/flagsEndpoint.js.map +1 -0
- package/dist/fields/phoneNumber/server/readPhoneOptions.js +18 -0
- package/dist/fields/phoneNumber/server/readPhoneOptions.js.map +1 -0
- package/dist/fields/phoneNumber/server/resolvePhoneOptionsSafe.js +33 -0
- package/dist/fields/phoneNumber/server/resolvePhoneOptionsSafe.js.map +1 -0
- package/dist/index.d.ts +5 -3
- package/dist/index.js +35 -1
- package/dist/index.js.map +1 -1
- package/dist/translations/ar.js +62 -0
- package/dist/translations/ar.js.map +1 -0
- package/dist/translations/de.js +19 -1
- package/dist/translations/de.js.map +1 -1
- package/dist/translations/en.js +19 -1
- package/dist/translations/en.js.map +1 -1
- package/dist/translations/es.js +62 -0
- package/dist/translations/es.js.map +1 -0
- package/dist/translations/fr.js +62 -0
- package/dist/translations/fr.js.map +1 -0
- package/dist/translations/id.js +62 -0
- package/dist/translations/id.js.map +1 -0
- package/dist/translations/index.d.ts +10 -1
- package/dist/translations/index.js +19 -1
- package/dist/translations/index.js.map +1 -1
- package/dist/translations/keys.d.ts +19 -1
- package/dist/translations/keys.js +20 -2
- package/dist/translations/keys.js.map +1 -1
- package/dist/translations/ko.js +62 -0
- package/dist/translations/ko.js.map +1 -0
- package/dist/translations/pt.js +62 -0
- package/dist/translations/pt.js.map +1 -0
- package/dist/translations/ru.js +62 -0
- package/dist/translations/ru.js.map +1 -0
- package/dist/translations/uk.js +62 -0
- package/dist/translations/uk.js.map +1 -0
- package/dist/translations/zh.js +62 -0
- package/dist/translations/zh.js.map +1 -0
- package/dist/types.d.ts +41 -1
- package/dist/utils/resolveStaticLabel.js +13 -0
- package/dist/utils/resolveStaticLabel.js.map +1 -0
- package/package.json +28 -6
- package/dist/fields/icon/shared/resolveStaticLabel.js +0 -16
- package/dist/fields/icon/shared/resolveStaticLabel.js.map +0 -1
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { COMPOUNDS } from "../engine/units.js";
|
|
3
|
+
import { keys } from "../../../translations/keys.js";
|
|
4
|
+
import { useTranslation as useTranslation$1 } from "../../../translations/useTranslation.js";
|
|
5
|
+
import { commitDrafts, draftsFor } from "./editModel.js";
|
|
6
|
+
import { useMeasurementModel } from "./useMeasurementModel.js";
|
|
7
|
+
import { FieldDescription, FieldError, FieldLabel, Popup, RenderCustomComponent, fieldBaseClass } from "@payloadcms/ui";
|
|
8
|
+
import { useCallback, useEffect, useId, useMemo, useRef, useState } from "react";
|
|
9
|
+
import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
|
|
10
|
+
import "./measurementField.css";
|
|
11
|
+
//#region src/fields/measurement/client/MeasurementInput.tsx
|
|
12
|
+
const baseClass = "fields-measurement";
|
|
13
|
+
const CLEAN = {
|
|
14
|
+
minor: false,
|
|
15
|
+
primary: false
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Every value input sizes to its content so "5 ft 11 in" (or a lone scalar)
|
|
19
|
+
* reads as one left-aligned phrase. The half-ch keeps the caret off the edge;
|
|
20
|
+
* the clamp keeps an empty input clickable and a runaway draft from pushing
|
|
21
|
+
* the row into overflow.
|
|
22
|
+
*/
|
|
23
|
+
const draftWidth = (draft) => ({ width: `${Math.min(Math.max(draft.length, 2), 12) + .5}ch` });
|
|
24
|
+
const unitKebab = /* @__PURE__ */ jsxs("svg", {
|
|
25
|
+
"aria-hidden": "true",
|
|
26
|
+
className: `${baseClass}__kebab`,
|
|
27
|
+
focusable: "false",
|
|
28
|
+
viewBox: "0 0 4 16",
|
|
29
|
+
xmlns: "http://www.w3.org/2000/svg",
|
|
30
|
+
children: [
|
|
31
|
+
/* @__PURE__ */ jsx("circle", {
|
|
32
|
+
cx: "2",
|
|
33
|
+
cy: "2",
|
|
34
|
+
fill: "currentColor",
|
|
35
|
+
r: "1.6"
|
|
36
|
+
}),
|
|
37
|
+
/* @__PURE__ */ jsx("circle", {
|
|
38
|
+
cx: "2",
|
|
39
|
+
cy: "8",
|
|
40
|
+
fill: "currentColor",
|
|
41
|
+
r: "1.6"
|
|
42
|
+
}),
|
|
43
|
+
/* @__PURE__ */ jsx("circle", {
|
|
44
|
+
cx: "2",
|
|
45
|
+
cy: "14",
|
|
46
|
+
fill: "currentColor",
|
|
47
|
+
r: "1.6"
|
|
48
|
+
})
|
|
49
|
+
]
|
|
50
|
+
});
|
|
51
|
+
/** The row itself, shared by `MeasurementInput` and the form-bound `MeasurementField`. */
|
|
52
|
+
const MeasurementInputView = (props) => {
|
|
53
|
+
const { AfterInput, BeforeInput, className, Description, description, Error: ErrorComponent, error, Label, label, localized, max, min, model, onChange, path, placeholder, readOnly, required, showError: showErrorFromProps, style, value } = props;
|
|
54
|
+
const { displayPrecision, displayUnit, engine, fmtBound, locale, resolvedPrecision, selectUnit: selectModelUnit, storageUnit, units } = model;
|
|
55
|
+
const { t } = useTranslation$1();
|
|
56
|
+
const isReadOnly = Boolean(readOnly);
|
|
57
|
+
const numericValue = typeof value === "number" && !Number.isNaN(value) ? value : null;
|
|
58
|
+
const draftOpts = useMemo(() => ({
|
|
59
|
+
displayUnit,
|
|
60
|
+
draft: resolvedPrecision.draft,
|
|
61
|
+
engine,
|
|
62
|
+
precision: displayPrecision,
|
|
63
|
+
storageDigits: resolvedPrecision.storage,
|
|
64
|
+
storageUnit
|
|
65
|
+
}), [
|
|
66
|
+
displayUnit,
|
|
67
|
+
engine,
|
|
68
|
+
displayPrecision,
|
|
69
|
+
resolvedPrecision,
|
|
70
|
+
storageUnit
|
|
71
|
+
]);
|
|
72
|
+
const boundsMessage = useMemo(() => {
|
|
73
|
+
if (numericValue === null) return void 0;
|
|
74
|
+
if (typeof min === "number" && numericValue < min) return t(keys.measurementBelowMin, { min: fmtBound(min) });
|
|
75
|
+
if (typeof max === "number" && numericValue > max) return t(keys.measurementAboveMax, { max: fmtBound(max) });
|
|
76
|
+
}, [
|
|
77
|
+
numericValue,
|
|
78
|
+
min,
|
|
79
|
+
max,
|
|
80
|
+
fmtBound,
|
|
81
|
+
t
|
|
82
|
+
]);
|
|
83
|
+
const errorMessage = error ?? boundsMessage;
|
|
84
|
+
const showError = showErrorFromProps ?? errorMessage !== void 0;
|
|
85
|
+
const editingRef = useRef(false);
|
|
86
|
+
const [drafts, setDrafts] = useState(() => draftsFor(numericValue, draftOpts));
|
|
87
|
+
const paintedDraftsRef = useRef(drafts);
|
|
88
|
+
const paintedStoredRef = useRef(numericValue);
|
|
89
|
+
const [dirty, setDirty] = useState(CLEAN);
|
|
90
|
+
useEffect(() => {
|
|
91
|
+
if (!editingRef.current) {
|
|
92
|
+
const resynced = draftsFor(numericValue, draftOpts);
|
|
93
|
+
setDrafts(resynced);
|
|
94
|
+
paintedDraftsRef.current = resynced;
|
|
95
|
+
paintedStoredRef.current = numericValue;
|
|
96
|
+
setDirty(CLEAN);
|
|
97
|
+
}
|
|
98
|
+
}, [numericValue, draftOpts]);
|
|
99
|
+
const onDraftChange = useCallback((part) => (event) => {
|
|
100
|
+
editingRef.current = true;
|
|
101
|
+
const next = {
|
|
102
|
+
...drafts,
|
|
103
|
+
[part]: event.target.value
|
|
104
|
+
};
|
|
105
|
+
const nextDirty = {
|
|
106
|
+
...dirty,
|
|
107
|
+
[part]: true
|
|
108
|
+
};
|
|
109
|
+
setDrafts(next);
|
|
110
|
+
setDirty(nextDirty);
|
|
111
|
+
onChange(commitDrafts(next, {
|
|
112
|
+
...draftOpts,
|
|
113
|
+
dirty: nextDirty,
|
|
114
|
+
entry: resolvedPrecision.entry,
|
|
115
|
+
paintedDrafts: paintedDraftsRef.current,
|
|
116
|
+
storedValue: paintedStoredRef.current
|
|
117
|
+
}));
|
|
118
|
+
}, [
|
|
119
|
+
dirty,
|
|
120
|
+
drafts,
|
|
121
|
+
draftOpts,
|
|
122
|
+
onChange,
|
|
123
|
+
resolvedPrecision.entry
|
|
124
|
+
]);
|
|
125
|
+
const onBlur = useCallback(() => {
|
|
126
|
+
editingRef.current = false;
|
|
127
|
+
const resynced = draftsFor(numericValue, draftOpts);
|
|
128
|
+
setDrafts(resynced);
|
|
129
|
+
paintedDraftsRef.current = resynced;
|
|
130
|
+
paintedStoredRef.current = numericValue;
|
|
131
|
+
setDirty(CLEAN);
|
|
132
|
+
}, [numericValue, draftOpts]);
|
|
133
|
+
const selectUnit = useCallback((unit) => {
|
|
134
|
+
editingRef.current = false;
|
|
135
|
+
setDirty(CLEAN);
|
|
136
|
+
selectModelUnit(unit);
|
|
137
|
+
}, [selectModelUnit]);
|
|
138
|
+
const isCompound = engine.isCompoundUnit(displayUnit);
|
|
139
|
+
const compoundDef = isCompound ? COMPOUNDS[displayUnit] : null;
|
|
140
|
+
const generatedId = useId();
|
|
141
|
+
const inputId = path ? `field-${path.replace(/\./g, "__")}` : generatedId;
|
|
142
|
+
const primaryInputRef = useRef(null);
|
|
143
|
+
const minorInputRef = useRef(null);
|
|
144
|
+
const focusNearestInput = useCallback((event) => {
|
|
145
|
+
const target = event.target;
|
|
146
|
+
const isUnitSuffix = target.closest(`.${baseClass}__unit-static`) !== null;
|
|
147
|
+
if (target !== event.currentTarget && !isUnitSuffix) return;
|
|
148
|
+
event.preventDefault();
|
|
149
|
+
const active = document.activeElement;
|
|
150
|
+
if (active === primaryInputRef.current || active === minorInputRef.current) return;
|
|
151
|
+
const [first, ...rest] = [primaryInputRef.current, minorInputRef.current].filter((input) => input !== null);
|
|
152
|
+
if (!first) return;
|
|
153
|
+
const distanceFrom = (input) => {
|
|
154
|
+
const rect = input.getBoundingClientRect();
|
|
155
|
+
return Math.abs(event.clientX - (rect.left + rect.width / 2));
|
|
156
|
+
};
|
|
157
|
+
let nearest = first;
|
|
158
|
+
let nearestDistance = distanceFrom(first);
|
|
159
|
+
for (const input of rest) {
|
|
160
|
+
const distance = distanceFrom(input);
|
|
161
|
+
if (distance < nearestDistance) {
|
|
162
|
+
nearestDistance = distance;
|
|
163
|
+
nearest = input;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
nearest.focus();
|
|
167
|
+
}, []);
|
|
168
|
+
const isUnitInteractive = units.length > 1 && !isReadOnly;
|
|
169
|
+
const renderUnitPanel = ({ close }) => /* @__PURE__ */ jsx("div", {
|
|
170
|
+
"aria-label": t(keys.selectUnit),
|
|
171
|
+
className: `${baseClass}__unit-panel`,
|
|
172
|
+
role: "listbox",
|
|
173
|
+
children: units.map((unit) => /* @__PURE__ */ jsxs("button", {
|
|
174
|
+
"aria-selected": unit === displayUnit,
|
|
175
|
+
className: `${baseClass}__unit-option`,
|
|
176
|
+
onClick: () => {
|
|
177
|
+
selectUnit(unit);
|
|
178
|
+
close();
|
|
179
|
+
},
|
|
180
|
+
role: "option",
|
|
181
|
+
type: "button",
|
|
182
|
+
children: [/* @__PURE__ */ jsx("span", { children: engine.unitLabel(unit, locale, "long") }), /* @__PURE__ */ jsx("span", {
|
|
183
|
+
className: `${baseClass}__unit-symbol`,
|
|
184
|
+
children: engine.unitLabel(unit, locale, "short")
|
|
185
|
+
})]
|
|
186
|
+
}, unit))
|
|
187
|
+
});
|
|
188
|
+
const renderUnitSuffix = (unit) => /* @__PURE__ */ jsx("span", {
|
|
189
|
+
className: `${baseClass}__unit-static`,
|
|
190
|
+
children: engine.unitLabel(unit, locale, "short")
|
|
191
|
+
});
|
|
192
|
+
const renderMenuTrigger = () => {
|
|
193
|
+
if (!isUnitInteractive) return null;
|
|
194
|
+
const shortLabel = engine.unitLabel(displayUnit, locale, "short");
|
|
195
|
+
return /* @__PURE__ */ jsx(Popup, {
|
|
196
|
+
button: /* @__PURE__ */ jsxs(Fragment$1, { children: [unitKebab, /* @__PURE__ */ jsxs("span", {
|
|
197
|
+
className: `${baseClass}__sr-only`,
|
|
198
|
+
children: [
|
|
199
|
+
t(keys.selectUnit),
|
|
200
|
+
": ",
|
|
201
|
+
shortLabel
|
|
202
|
+
]
|
|
203
|
+
})] }),
|
|
204
|
+
buttonClassName: `${baseClass}__unit-trigger`,
|
|
205
|
+
buttonType: "default",
|
|
206
|
+
caret: false,
|
|
207
|
+
className: `${baseClass}__popup`,
|
|
208
|
+
horizontalAlign: "right",
|
|
209
|
+
render: renderUnitPanel,
|
|
210
|
+
size: "fit-content",
|
|
211
|
+
verticalAlign: "bottom"
|
|
212
|
+
});
|
|
213
|
+
};
|
|
214
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
215
|
+
className: [
|
|
216
|
+
fieldBaseClass,
|
|
217
|
+
baseClass,
|
|
218
|
+
className,
|
|
219
|
+
showError && "error",
|
|
220
|
+
isReadOnly && "read-only"
|
|
221
|
+
].filter(Boolean).join(" "),
|
|
222
|
+
style,
|
|
223
|
+
children: [/* @__PURE__ */ jsx(RenderCustomComponent, {
|
|
224
|
+
CustomComponent: Label,
|
|
225
|
+
Fallback: /* @__PURE__ */ jsx(FieldLabel, {
|
|
226
|
+
htmlFor: path ? void 0 : inputId,
|
|
227
|
+
label,
|
|
228
|
+
localized,
|
|
229
|
+
path,
|
|
230
|
+
required
|
|
231
|
+
})
|
|
232
|
+
}), /* @__PURE__ */ jsxs("div", {
|
|
233
|
+
className: `${fieldBaseClass}__wrap`,
|
|
234
|
+
children: [
|
|
235
|
+
/* @__PURE__ */ jsx(RenderCustomComponent, {
|
|
236
|
+
CustomComponent: ErrorComponent,
|
|
237
|
+
Fallback: /* @__PURE__ */ jsx(FieldError, {
|
|
238
|
+
message: errorMessage,
|
|
239
|
+
path,
|
|
240
|
+
showError
|
|
241
|
+
})
|
|
242
|
+
}),
|
|
243
|
+
BeforeInput,
|
|
244
|
+
/* @__PURE__ */ jsxs("div", {
|
|
245
|
+
className: `${baseClass}__container`,
|
|
246
|
+
onMouseDown: focusNearestInput,
|
|
247
|
+
children: [isCompound && compoundDef ? /* @__PURE__ */ jsxs("span", {
|
|
248
|
+
className: `${baseClass}__compound`,
|
|
249
|
+
onMouseDown: focusNearestInput,
|
|
250
|
+
children: [/* @__PURE__ */ jsxs("span", {
|
|
251
|
+
className: `${baseClass}__part`,
|
|
252
|
+
children: [/* @__PURE__ */ jsx("input", {
|
|
253
|
+
className: `${baseClass}__input`,
|
|
254
|
+
id: inputId,
|
|
255
|
+
inputMode: "decimal",
|
|
256
|
+
name: path,
|
|
257
|
+
onBlur,
|
|
258
|
+
onChange: onDraftChange("primary"),
|
|
259
|
+
readOnly: isReadOnly,
|
|
260
|
+
ref: primaryInputRef,
|
|
261
|
+
step: "any",
|
|
262
|
+
style: draftWidth(drafts.primary),
|
|
263
|
+
type: "number",
|
|
264
|
+
value: drafts.primary
|
|
265
|
+
}), renderUnitSuffix(compoundDef.major)]
|
|
266
|
+
}), /* @__PURE__ */ jsxs("span", {
|
|
267
|
+
className: `${baseClass}__part`,
|
|
268
|
+
children: [/* @__PURE__ */ jsx("input", {
|
|
269
|
+
"aria-label": engine.unitLabel(compoundDef.minor, locale, "long"),
|
|
270
|
+
className: `${baseClass}__input`,
|
|
271
|
+
inputMode: "decimal",
|
|
272
|
+
max: compoundDef.ratio - .001,
|
|
273
|
+
min: 0,
|
|
274
|
+
onBlur,
|
|
275
|
+
onChange: onDraftChange("minor"),
|
|
276
|
+
readOnly: isReadOnly,
|
|
277
|
+
ref: minorInputRef,
|
|
278
|
+
step: "any",
|
|
279
|
+
style: draftWidth(drafts.minor),
|
|
280
|
+
type: "number",
|
|
281
|
+
value: drafts.minor
|
|
282
|
+
}), renderUnitSuffix(compoundDef.minor)]
|
|
283
|
+
})]
|
|
284
|
+
}) : /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx("input", {
|
|
285
|
+
className: `${baseClass}__input`,
|
|
286
|
+
id: inputId,
|
|
287
|
+
inputMode: "decimal",
|
|
288
|
+
name: path,
|
|
289
|
+
onBlur,
|
|
290
|
+
onChange: onDraftChange("primary"),
|
|
291
|
+
placeholder,
|
|
292
|
+
readOnly: isReadOnly,
|
|
293
|
+
ref: primaryInputRef,
|
|
294
|
+
step: "any",
|
|
295
|
+
style: draftWidth(drafts.primary || placeholder || ""),
|
|
296
|
+
type: "number",
|
|
297
|
+
value: drafts.primary
|
|
298
|
+
}), renderUnitSuffix(displayUnit)] }), renderMenuTrigger()]
|
|
299
|
+
}),
|
|
300
|
+
AfterInput,
|
|
301
|
+
/* @__PURE__ */ jsx(RenderCustomComponent, {
|
|
302
|
+
CustomComponent: Description,
|
|
303
|
+
Fallback: /* @__PURE__ */ jsx(FieldDescription, {
|
|
304
|
+
description,
|
|
305
|
+
path: path ?? ""
|
|
306
|
+
})
|
|
307
|
+
})
|
|
308
|
+
]
|
|
309
|
+
})]
|
|
310
|
+
});
|
|
311
|
+
};
|
|
312
|
+
/**
|
|
313
|
+
* The measurement field's row, unit menu and conversion pipeline with no form behind
|
|
314
|
+
* it, for custom admin views and components. Inside the admin the display unit is the
|
|
315
|
+
* viewer's saved preference for `preferenceKey`, shared with every measurement field
|
|
316
|
+
* on that key; `MeasurementField` is this plus `useField`.
|
|
317
|
+
*/
|
|
318
|
+
const MeasurementInput = ({ options, ...props }) => {
|
|
319
|
+
return /* @__PURE__ */ jsx(MeasurementInputView, {
|
|
320
|
+
model: useMeasurementModel(options),
|
|
321
|
+
...props
|
|
322
|
+
});
|
|
323
|
+
};
|
|
324
|
+
//#endregion
|
|
325
|
+
export { MeasurementInput, MeasurementInputView };
|
|
326
|
+
|
|
327
|
+
//# sourceMappingURL=MeasurementInput.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MeasurementInput.js","names":["useTranslation"],"sources":["../../../../src/fields/measurement/client/MeasurementInput.tsx"],"sourcesContent":["'use client'\nimport {\n\tFieldDescription,\n\tFieldError,\n\tFieldLabel,\n\tfieldBaseClass,\n\tPopup,\n\tRenderCustomComponent,\n} from '@payloadcms/ui'\nimport type { StaticDescription, StaticLabel } from 'payload'\nimport type React from 'react'\nimport { useCallback, useEffect, useId, useMemo, useRef, useState } from 'react'\nimport { keys } from '../../../translations/keys'\nimport { useTranslation } from '../../../translations/useTranslation'\nimport { COMPOUNDS, type MeasurementUnitId } from '../engine/units'\nimport { commitDrafts, type DirtyDrafts, draftsFor, type MeasurementDrafts } from './editModel'\nimport {\n\ttype MeasurementInputOptions,\n\ttype MeasurementModel,\n\tuseMeasurementModel,\n} from './useMeasurementModel'\nimport './measurementField.css'\n\nconst baseClass = 'fields-measurement'\n\nconst CLEAN: DirtyDrafts = { minor: false, primary: false }\n\n/**\n * Every value input sizes to its content so \"5 ft 11 in\" (or a lone scalar)\n * reads as one left-aligned phrase. The half-ch keeps the caret off the edge;\n * the clamp keeps an empty input clickable and a runaway draft from pushing\n * the row into overflow.\n */\nconst draftWidth = (draft: string): React.CSSProperties => ({\n\twidth: `${Math.min(Math.max(draft.length, 2), 12) + 0.5}ch`,\n})\n\nconst unitKebab = (\n\t<svg\n\t\taria-hidden=\"true\"\n\t\tclassName={`${baseClass}__kebab`}\n\t\tfocusable=\"false\"\n\t\tviewBox=\"0 0 4 16\"\n\t\txmlns=\"http://www.w3.org/2000/svg\"\n\t>\n\t\t<circle cx=\"2\" cy=\"2\" fill=\"currentColor\" r=\"1.6\" />\n\t\t<circle cx=\"2\" cy=\"8\" fill=\"currentColor\" r=\"1.6\" />\n\t\t<circle cx=\"2\" cy=\"14\" fill=\"currentColor\" r=\"1.6\" />\n\t</svg>\n)\n\n/**\n * Props for the form-free measurement input. `value` is the number in\n * `options.storageUnit`, exactly as `measurementField` stores it; the viewer reads and\n * types in their display unit.\n */\nexport type MeasurementInputProps = {\n\tvalue: null | number | undefined\n\t/** Fires on every edit with the value converted back to the storage unit. */\n\tonChange: (value: null | number) => void\n\toptions: MeasurementInputOptions\n\t/** Bounds in the storage unit; a value outside them shows a message in the display unit. */\n\tmin?: number\n\tmax?: number\n\t/** Names the input (`id`, `name`, label `htmlFor`), as on a native field. */\n\tpath?: string\n\tlabel?: StaticLabel\n\tdescription?: StaticDescription\n\tplaceholder?: string\n\trequired?: boolean\n\tlocalized?: boolean\n\treadOnly?: boolean\n\t/** Message shown in the error tooltip. Left unset, the input reports its own bound errors. */\n\terror?: string\n\t/** Forces the error state on or off. Left unset, it follows `error` and the bound check. */\n\tshowError?: boolean\n\tclassName?: string\n\tstyle?: React.CSSProperties\n\tLabel?: React.ReactNode\n\tError?: React.ReactNode\n\tDescription?: React.ReactNode\n\tBeforeInput?: React.ReactNode\n\tAfterInput?: React.ReactNode\n}\n\ntype MeasurementInputViewProps = { model: MeasurementModel } & Omit<\n\tMeasurementInputProps,\n\t'options'\n>\n\n/** The row itself, shared by `MeasurementInput` and the form-bound `MeasurementField`. */\nexport const MeasurementInputView: React.FC<MeasurementInputViewProps> = (props) => {\n\tconst {\n\t\tAfterInput,\n\t\tBeforeInput,\n\t\tclassName,\n\t\tDescription,\n\t\tdescription,\n\t\tError: ErrorComponent,\n\t\terror,\n\t\tLabel,\n\t\tlabel,\n\t\tlocalized,\n\t\tmax,\n\t\tmin,\n\t\tmodel,\n\t\tonChange,\n\t\tpath,\n\t\tplaceholder,\n\t\treadOnly,\n\t\trequired,\n\t\tshowError: showErrorFromProps,\n\t\tstyle,\n\t\tvalue,\n\t} = props\n\tconst {\n\t\tdisplayPrecision,\n\t\tdisplayUnit,\n\t\tengine,\n\t\tfmtBound,\n\t\tlocale,\n\t\tresolvedPrecision,\n\t\tselectUnit: selectModelUnit,\n\t\tstorageUnit,\n\t\tunits,\n\t} = model\n\tconst { t } = useTranslation()\n\tconst isReadOnly = Boolean(readOnly)\n\n\tconst numericValue = typeof value === 'number' && !Number.isNaN(value) ? value : null\n\tconst draftOpts = useMemo(\n\t\t() => ({\n\t\t\tdisplayUnit,\n\t\t\tdraft: resolvedPrecision.draft,\n\t\t\tengine,\n\t\t\tprecision: displayPrecision,\n\t\t\tstorageDigits: resolvedPrecision.storage,\n\t\t\tstorageUnit,\n\t\t}),\n\t\t[displayUnit, engine, displayPrecision, resolvedPrecision, storageUnit]\n\t)\n\n\t// Payload's server-driven form-state refresh (conditional-logic revalidation on\n\t// every change) can replace this field's errorMessage with the server's raw,\n\t// storage-unit message before our own throttled client validate lands. Compute\n\t// the unit-aware bound message independently and pass it straight to FieldError\n\t// so display never depends on winning that race.\n\tconst boundsMessage = useMemo(() => {\n\t\tif (numericValue === null) return undefined\n\t\tif (typeof min === 'number' && numericValue < min) {\n\t\t\treturn t(keys.measurementBelowMin, { min: fmtBound(min) })\n\t\t}\n\t\tif (typeof max === 'number' && numericValue > max) {\n\t\t\treturn t(keys.measurementAboveMax, { max: fmtBound(max) })\n\t\t}\n\t\treturn undefined\n\t}, [numericValue, min, max, fmtBound, t])\n\tconst errorMessage = error ?? boundsMessage\n\tconst showError = showErrorFromProps ?? errorMessage !== undefined\n\n\tconst editingRef = useRef(false)\n\tconst [drafts, setDrafts] = useState<MeasurementDrafts>(() => draftsFor(numericValue, draftOpts))\n\t// The drafts as painted on screen immediately before the current edit, refreshed\n\t// only at a resync (mount, external value, blur, unit switch) and never per\n\t// keystroke. A carry-rounded display (182.5 cm storage paints as 6 ft 0 in even\n\t// though the exact split is 5 ft 11.850394 in) must never be mistaken for the\n\t// exact stored value, so commits diff the typed value against this baseline\n\t// rather than against the exact decompose.\n\tconst paintedDraftsRef = useRef<MeasurementDrafts>(drafts)\n\t// The stored value the painted drafts were derived from, frozen at the same\n\t// resync points as paintedDraftsRef. A mid-edit commit updates the live value\n\t// (and so `numericValue`) before the viewer blurs; diffing the next keystroke's\n\t// delta against that advanced value, instead of this frozen snapshot, would\n\t// re-apply an already-committed delta on top of itself.\n\tconst paintedStoredRef = useRef<number | null>(numericValue)\n\t// Which part the viewer has actually typed into since the last resync: an\n\t// untouched part must commit the value it already holds, never whatever the\n\t// display-policy draft happens to show for it.\n\tconst [dirty, setDirty] = useState<DirtyDrafts>(CLEAN)\n\tuseEffect(() => {\n\t\tif (!editingRef.current) {\n\t\t\tconst resynced = draftsFor(numericValue, draftOpts)\n\t\t\tsetDrafts(resynced)\n\t\t\tpaintedDraftsRef.current = resynced\n\t\t\tpaintedStoredRef.current = numericValue\n\t\t\tsetDirty(CLEAN)\n\t\t}\n\t}, [numericValue, draftOpts])\n\n\tconst onDraftChange = useCallback(\n\t\t(part: keyof MeasurementDrafts) => (event: React.ChangeEvent<HTMLInputElement>) => {\n\t\t\teditingRef.current = true\n\t\t\tconst next = { ...drafts, [part]: event.target.value }\n\t\t\tconst nextDirty = { ...dirty, [part]: true }\n\t\t\tsetDrafts(next)\n\t\t\tsetDirty(nextDirty)\n\t\t\tonChange(\n\t\t\t\tcommitDrafts(next, {\n\t\t\t\t\t...draftOpts,\n\t\t\t\t\tdirty: nextDirty,\n\t\t\t\t\tentry: resolvedPrecision.entry,\n\t\t\t\t\tpaintedDrafts: paintedDraftsRef.current,\n\t\t\t\t\tstoredValue: paintedStoredRef.current,\n\t\t\t\t})\n\t\t\t)\n\t\t},\n\t\t[dirty, drafts, draftOpts, onChange, resolvedPrecision.entry]\n\t)\n\n\tconst onBlur = useCallback(() => {\n\t\teditingRef.current = false\n\t\tconst resynced = draftsFor(numericValue, draftOpts)\n\t\tsetDrafts(resynced)\n\t\tpaintedDraftsRef.current = resynced\n\t\tpaintedStoredRef.current = numericValue\n\t\tsetDirty(CLEAN)\n\t}, [numericValue, draftOpts])\n\n\tconst selectUnit = useCallback(\n\t\t(unit: MeasurementUnitId) => {\n\t\t\teditingRef.current = false\n\t\t\tsetDirty(CLEAN)\n\t\t\tselectModelUnit(unit)\n\t\t},\n\t\t[selectModelUnit]\n\t)\n\n\tconst isCompound = engine.isCompoundUnit(displayUnit)\n\tconst compoundDef = isCompound ? COMPOUNDS[displayUnit] : null\n\t// Without a path there is no field id to derive, so the label pairs with a generated one\n\tconst generatedId = useId()\n\tconst inputId = path ? `field-${path.replace(/\\./g, '__')}` : generatedId\n\n\tconst primaryInputRef = useRef<HTMLInputElement>(null)\n\tconst minorInputRef = useRef<HTMLInputElement>(null)\n\n\t// Dead-space (or the passive unit suffix, e.g. \"in\") mousedown focuses the nearest\n\t// input like a native control; preventDefault stops an already-focused input from\n\t// blurring, so we never steal focus from it. The suffix is never inside the unit\n\t// trigger button, so this never intercepts opening the unit popup.\n\tconst focusNearestInput = useCallback((event: React.MouseEvent<HTMLElement>) => {\n\t\tconst target = event.target as HTMLElement\n\t\tconst isUnitSuffix = target.closest(`.${baseClass}__unit-static`) !== null\n\t\tif (target !== event.currentTarget && !isUnitSuffix) return\n\t\tevent.preventDefault()\n\t\tconst active = document.activeElement\n\t\tif (active === primaryInputRef.current || active === minorInputRef.current) return\n\t\tconst inputs = [primaryInputRef.current, minorInputRef.current].filter(\n\t\t\t(input): input is HTMLInputElement => input !== null\n\t\t)\n\t\tconst [first, ...rest] = inputs\n\t\tif (!first) return\n\t\tconst distanceFrom = (input: HTMLInputElement) => {\n\t\t\tconst rect = input.getBoundingClientRect()\n\t\t\treturn Math.abs(event.clientX - (rect.left + rect.width / 2))\n\t\t}\n\t\tlet nearest = first\n\t\tlet nearestDistance = distanceFrom(first)\n\t\tfor (const input of rest) {\n\t\t\tconst distance = distanceFrom(input)\n\t\t\tif (distance < nearestDistance) {\n\t\t\t\tnearestDistance = distance\n\t\t\t\tnearest = input\n\t\t\t}\n\t\t}\n\t\tnearest.focus()\n\t}, [])\n\n\tconst isUnitInteractive = units.length > 1 && !isReadOnly\n\n\tconst renderUnitPanel = ({ close }: { close: () => void }) => (\n\t\t<div aria-label={t(keys.selectUnit)} className={`${baseClass}__unit-panel`} role=\"listbox\">\n\t\t\t{units.map((unit) => (\n\t\t\t\t<button\n\t\t\t\t\taria-selected={unit === displayUnit}\n\t\t\t\t\tclassName={`${baseClass}__unit-option`}\n\t\t\t\t\tkey={unit}\n\t\t\t\t\tonClick={() => {\n\t\t\t\t\t\tselectUnit(unit)\n\t\t\t\t\t\tclose()\n\t\t\t\t\t}}\n\t\t\t\t\trole=\"option\"\n\t\t\t\t\ttype=\"button\"\n\t\t\t\t>\n\t\t\t\t\t<span>{engine.unitLabel(unit, locale, 'long')}</span>\n\t\t\t\t\t<span className={`${baseClass}__unit-symbol`}>\n\t\t\t\t\t\t{engine.unitLabel(unit, locale, 'short')}\n\t\t\t\t\t</span>\n\t\t\t\t</button>\n\t\t\t))}\n\t\t</div>\n\t)\n\n\tconst renderUnitSuffix = (unit: MeasurementUnitId) => (\n\t\t<span className={`${baseClass}__unit-static`}>{engine.unitLabel(unit, locale, 'short')}</span>\n\t)\n\n\t// One kebab per field, pinned to the row's right edge; the unit text itself\n\t// stays a passive suffix so value and unit read as a single phrase.\n\tconst renderMenuTrigger = () => {\n\t\tif (!isUnitInteractive) return null\n\t\tconst shortLabel = engine.unitLabel(displayUnit, locale, 'short')\n\t\treturn (\n\t\t\t<Popup\n\t\t\t\tbutton={\n\t\t\t\t\t<>\n\t\t\t\t\t\t{unitKebab}\n\t\t\t\t\t\t<span className={`${baseClass}__sr-only`}>\n\t\t\t\t\t\t\t{t(keys.selectUnit)}: {shortLabel}\n\t\t\t\t\t\t</span>\n\t\t\t\t\t</>\n\t\t\t\t}\n\t\t\t\tbuttonClassName={`${baseClass}__unit-trigger`}\n\t\t\t\tbuttonType=\"default\"\n\t\t\t\tcaret={false}\n\t\t\t\tclassName={`${baseClass}__popup`}\n\t\t\t\thorizontalAlign=\"right\"\n\t\t\t\trender={renderUnitPanel}\n\t\t\t\tsize=\"fit-content\"\n\t\t\t\tverticalAlign=\"bottom\"\n\t\t\t/>\n\t\t)\n\t}\n\n\treturn (\n\t\t<div\n\t\t\tclassName={[\n\t\t\t\tfieldBaseClass,\n\t\t\t\tbaseClass,\n\t\t\t\tclassName,\n\t\t\t\tshowError && 'error',\n\t\t\t\tisReadOnly && 'read-only',\n\t\t\t]\n\t\t\t\t.filter(Boolean)\n\t\t\t\t.join(' ')}\n\t\t\tstyle={style}\n\t\t>\n\t\t\t<RenderCustomComponent\n\t\t\t\tCustomComponent={Label}\n\t\t\t\tFallback={\n\t\t\t\t\t<FieldLabel\n\t\t\t\t\t\thtmlFor={path ? undefined : inputId}\n\t\t\t\t\t\tlabel={label}\n\t\t\t\t\t\tlocalized={localized}\n\t\t\t\t\t\tpath={path}\n\t\t\t\t\t\trequired={required}\n\t\t\t\t\t/>\n\t\t\t\t}\n\t\t\t/>\n\t\t\t<div className={`${fieldBaseClass}__wrap`}>\n\t\t\t\t<RenderCustomComponent\n\t\t\t\t\tCustomComponent={ErrorComponent}\n\t\t\t\t\tFallback={<FieldError message={errorMessage} path={path} showError={showError} />}\n\t\t\t\t/>\n\t\t\t\t{BeforeInput}\n\t\t\t\t{/* biome-ignore lint/a11y/noStaticElementInteractions: mousedown only refocuses an inner input, it adds no control and changes no semantics */}\n\t\t\t\t<div className={`${baseClass}__container`} onMouseDown={focusNearestInput}>\n\t\t\t\t\t{isCompound && compoundDef ? (\n\t\t\t\t\t\t// biome-ignore lint/a11y/noStaticElementInteractions: same as the container, refocuses whichever inner input is nearest\n\t\t\t\t\t\t<span className={`${baseClass}__compound`} onMouseDown={focusNearestInput}>\n\t\t\t\t\t\t\t<span className={`${baseClass}__part`}>\n\t\t\t\t\t\t\t\t<input\n\t\t\t\t\t\t\t\t\tclassName={`${baseClass}__input`}\n\t\t\t\t\t\t\t\t\tid={inputId}\n\t\t\t\t\t\t\t\t\tinputMode=\"decimal\"\n\t\t\t\t\t\t\t\t\tname={path}\n\t\t\t\t\t\t\t\t\tonBlur={onBlur}\n\t\t\t\t\t\t\t\t\tonChange={onDraftChange('primary')}\n\t\t\t\t\t\t\t\t\treadOnly={isReadOnly}\n\t\t\t\t\t\t\t\t\tref={primaryInputRef}\n\t\t\t\t\t\t\t\t\tstep=\"any\"\n\t\t\t\t\t\t\t\t\tstyle={draftWidth(drafts.primary)}\n\t\t\t\t\t\t\t\t\ttype=\"number\"\n\t\t\t\t\t\t\t\t\tvalue={drafts.primary}\n\t\t\t\t\t\t\t\t/>\n\t\t\t\t\t\t\t\t{renderUnitSuffix(compoundDef.major)}\n\t\t\t\t\t\t\t</span>\n\t\t\t\t\t\t\t<span className={`${baseClass}__part`}>\n\t\t\t\t\t\t\t\t<input\n\t\t\t\t\t\t\t\t\taria-label={engine.unitLabel(compoundDef.minor, locale, 'long')}\n\t\t\t\t\t\t\t\t\tclassName={`${baseClass}__input`}\n\t\t\t\t\t\t\t\t\tinputMode=\"decimal\"\n\t\t\t\t\t\t\t\t\tmax={compoundDef.ratio - 0.001}\n\t\t\t\t\t\t\t\t\tmin={0}\n\t\t\t\t\t\t\t\t\tonBlur={onBlur}\n\t\t\t\t\t\t\t\t\tonChange={onDraftChange('minor')}\n\t\t\t\t\t\t\t\t\treadOnly={isReadOnly}\n\t\t\t\t\t\t\t\t\tref={minorInputRef}\n\t\t\t\t\t\t\t\t\tstep=\"any\"\n\t\t\t\t\t\t\t\t\tstyle={draftWidth(drafts.minor)}\n\t\t\t\t\t\t\t\t\ttype=\"number\"\n\t\t\t\t\t\t\t\t\tvalue={drafts.minor}\n\t\t\t\t\t\t\t\t/>\n\t\t\t\t\t\t\t\t{renderUnitSuffix(compoundDef.minor)}\n\t\t\t\t\t\t\t</span>\n\t\t\t\t\t\t</span>\n\t\t\t\t\t) : (\n\t\t\t\t\t\t<>\n\t\t\t\t\t\t\t<input\n\t\t\t\t\t\t\t\tclassName={`${baseClass}__input`}\n\t\t\t\t\t\t\t\tid={inputId}\n\t\t\t\t\t\t\t\tinputMode=\"decimal\"\n\t\t\t\t\t\t\t\tname={path}\n\t\t\t\t\t\t\t\tonBlur={onBlur}\n\t\t\t\t\t\t\t\tonChange={onDraftChange('primary')}\n\t\t\t\t\t\t\t\tplaceholder={placeholder}\n\t\t\t\t\t\t\t\treadOnly={isReadOnly}\n\t\t\t\t\t\t\t\tref={primaryInputRef}\n\t\t\t\t\t\t\t\tstep=\"any\"\n\t\t\t\t\t\t\t\tstyle={draftWidth(drafts.primary || placeholder || '')}\n\t\t\t\t\t\t\t\ttype=\"number\"\n\t\t\t\t\t\t\t\tvalue={drafts.primary}\n\t\t\t\t\t\t\t/>\n\t\t\t\t\t\t\t{renderUnitSuffix(displayUnit)}\n\t\t\t\t\t\t</>\n\t\t\t\t\t)}\n\t\t\t\t\t{renderMenuTrigger()}\n\t\t\t\t</div>\n\t\t\t\t{AfterInput}\n\t\t\t\t<RenderCustomComponent\n\t\t\t\t\tCustomComponent={Description}\n\t\t\t\t\tFallback={<FieldDescription description={description} path={path ?? ''} />}\n\t\t\t\t/>\n\t\t\t</div>\n\t\t</div>\n\t)\n}\n\n/**\n * The measurement field's row, unit menu and conversion pipeline with no form behind\n * it, for custom admin views and components. Inside the admin the display unit is the\n * viewer's saved preference for `preferenceKey`, shared with every measurement field\n * on that key; `MeasurementField` is this plus `useField`.\n */\nexport const MeasurementInput: React.FC<MeasurementInputProps> = ({ options, ...props }) => {\n\tconst model = useMeasurementModel(options)\n\treturn <MeasurementInputView model={model} {...props} />\n}\n"],"mappings":";;;;;;;;;;;AAuBA,MAAM,YAAY;AAElB,MAAM,QAAqB;CAAE,OAAO;CAAO,SAAS;AAAM;;;;;;;AAQ1D,MAAM,cAAc,WAAwC,EAC3D,OAAO,GAAG,KAAK,IAAI,KAAK,IAAI,MAAM,QAAQ,CAAC,GAAG,EAAE,IAAI,GAAI,IACzD;AAEA,MAAM,YACL,qBAAC,OAAD;CACC,eAAY;CACZ,WAAW,GAAG,UAAU;CACxB,WAAU;CACV,SAAQ;CACR,OAAM;WALP;EAOC,oBAAC,UAAD;GAAQ,IAAG;GAAI,IAAG;GAAI,MAAK;GAAe,GAAE;EAAO,CAAA;EACnD,oBAAC,UAAD;GAAQ,IAAG;GAAI,IAAG;GAAI,MAAK;GAAe,GAAE;EAAO,CAAA;EACnD,oBAAC,UAAD;GAAQ,IAAG;GAAI,IAAG;GAAK,MAAK;GAAe,GAAE;EAAO,CAAA;CAChD;;;AA2CN,MAAa,wBAA6D,UAAU;CACnF,MAAM,EACL,YACA,aACA,WACA,aACA,aACA,OAAO,gBACP,OACA,OACA,OACA,WACA,KACA,KACA,OACA,UACA,MACA,aACA,UACA,UACA,WAAW,oBACX,OACA,UACG;CACJ,MAAM,EACL,kBACA,aACA,QACA,UACA,QACA,mBACA,YAAY,iBACZ,aACA,UACG;CACJ,MAAM,EAAE,MAAMA,iBAAe;CAC7B,MAAM,aAAa,QAAQ,QAAQ;CAEnC,MAAM,eAAe,OAAO,UAAU,YAAY,CAAC,OAAO,MAAM,KAAK,IAAI,QAAQ;CACjF,MAAM,YAAY,eACV;EACN;EACA,OAAO,kBAAkB;EACzB;EACA,WAAW;EACX,eAAe,kBAAkB;EACjC;CACD,IACA;E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AAK;MACL,OAAO,WAAW,OAAO,WAAW,eAAe,EAAE;MACrD,MAAK;MACL,OAAO,OAAO;KACd,CAAA,GACA,iBAAiB,WAAW,CAC5B,EAAA,CAAA,GAEF,kBAAkB,CACf;;IACJ;IACD,oBAAC,uBAAD;KACC,iBAAiB;KACjB,UAAU,oBAAC,kBAAD;MAA+B;MAAa,MAAM,QAAQ;KAAK,CAAA;IACzE,CAAA;GACG;IACD;;AAEP;;;;;;;AAQA,MAAa,oBAAqD,EAAE,SAAS,GAAG,YAAY;CAE3F,OAAO,oBAAC,sBAAD;EAAsB,OADf,oBAAoB,OACM;EAAG,GAAI;CAAQ,CAAA;AACxD"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { MeasurementUnitId } from "../engine/units.js";
|
|
2
|
+
import { MeasurementUnitsPreference } from "../options.js";
|
|
3
|
+
import React from "react";
|
|
4
|
+
|
|
5
|
+
//#region src/fields/measurement/client/MeasurementUnitsProvider.d.ts
|
|
6
|
+
type MeasurementUnitsContextValue = {
|
|
7
|
+
ready: boolean;
|
|
8
|
+
units: MeasurementUnitsPreference;
|
|
9
|
+
setUnit: (preferenceKey: string, unit: MeasurementUnitId) => void;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Reactive layer over payload-preferences for the per-bucket display units.
|
|
13
|
+
* Payload's own usePreferences is ref-backed and never re-renders consumers,
|
|
14
|
+
* so this provider holds the map in state: one toggle re-renders every
|
|
15
|
+
* measurement field and cell on the page, then persists with merge mode.
|
|
16
|
+
*/
|
|
17
|
+
declare const MeasurementUnitsProvider: React.FC<{
|
|
18
|
+
children: React.ReactNode; /** Kiosk/shared-account mode: skips both the preference fetch and writes, state stays session-only. Defaults to true. */
|
|
19
|
+
persist?: boolean;
|
|
20
|
+
}>;
|
|
21
|
+
declare const useMeasurementUnits: () => MeasurementUnitsContextValue | null;
|
|
22
|
+
//#endregion
|
|
23
|
+
export { MeasurementUnitsProvider, useMeasurementUnits };
|
|
24
|
+
//# sourceMappingURL=MeasurementUnitsProvider.d.ts.map
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { MEASUREMENT_PREFERENCE_KEY } from "../options.js";
|
|
3
|
+
import { usePreferences } from "@payloadcms/ui";
|
|
4
|
+
import { createContext, useCallback, useContext, useEffect, useMemo, useState } from "react";
|
|
5
|
+
import { jsx } from "react/jsx-runtime";
|
|
6
|
+
//#region src/fields/measurement/client/MeasurementUnitsProvider.tsx
|
|
7
|
+
const Context = createContext(null);
|
|
8
|
+
const isPreference = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
9
|
+
/**
|
|
10
|
+
* Reactive layer over payload-preferences for the per-bucket display units.
|
|
11
|
+
* Payload's own usePreferences is ref-backed and never re-renders consumers,
|
|
12
|
+
* so this provider holds the map in state: one toggle re-renders every
|
|
13
|
+
* measurement field and cell on the page, then persists with merge mode.
|
|
14
|
+
*/
|
|
15
|
+
const MeasurementUnitsProvider = ({ children, persist = true }) => {
|
|
16
|
+
const { getPreference, setPreference } = usePreferences();
|
|
17
|
+
const [units, setUnits] = useState({});
|
|
18
|
+
const [ready, setReady] = useState(() => !persist);
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
if (!persist) return;
|
|
21
|
+
let cancelled = false;
|
|
22
|
+
getPreference(MEASUREMENT_PREFERENCE_KEY).then((stored) => {
|
|
23
|
+
if (cancelled) return;
|
|
24
|
+
if (isPreference(stored)) setUnits((current) => ({
|
|
25
|
+
...stored,
|
|
26
|
+
...current
|
|
27
|
+
}));
|
|
28
|
+
setReady(true);
|
|
29
|
+
}).catch(() => {
|
|
30
|
+
if (!cancelled) setReady(true);
|
|
31
|
+
});
|
|
32
|
+
return () => {
|
|
33
|
+
cancelled = true;
|
|
34
|
+
};
|
|
35
|
+
}, [getPreference, persist]);
|
|
36
|
+
const setUnit = useCallback((preferenceKey, unit) => {
|
|
37
|
+
setUnits((current) => ({
|
|
38
|
+
...current,
|
|
39
|
+
[preferenceKey]: unit
|
|
40
|
+
}));
|
|
41
|
+
if (persist) setPreference(MEASUREMENT_PREFERENCE_KEY, { [preferenceKey]: unit }, true);
|
|
42
|
+
}, [persist, setPreference]);
|
|
43
|
+
const value = useMemo(() => ({
|
|
44
|
+
ready,
|
|
45
|
+
setUnit,
|
|
46
|
+
units
|
|
47
|
+
}), [
|
|
48
|
+
ready,
|
|
49
|
+
setUnit,
|
|
50
|
+
units
|
|
51
|
+
]);
|
|
52
|
+
return /* @__PURE__ */ jsx(Context.Provider, {
|
|
53
|
+
value,
|
|
54
|
+
children
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
const useMeasurementUnits = () => useContext(Context);
|
|
58
|
+
//#endregion
|
|
59
|
+
export { MeasurementUnitsProvider, useMeasurementUnits };
|
|
60
|
+
|
|
61
|
+
//# sourceMappingURL=MeasurementUnitsProvider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MeasurementUnitsProvider.js","names":[],"sources":["../../../../src/fields/measurement/client/MeasurementUnitsProvider.tsx"],"sourcesContent":["'use client'\nimport { usePreferences } from '@payloadcms/ui'\nimport type React from 'react'\nimport { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react'\nimport type { MeasurementUnitId } from '../engine/units'\nimport { MEASUREMENT_PREFERENCE_KEY, type MeasurementUnitsPreference } from '../options'\n\nexport type MeasurementUnitsContextValue = {\n\tready: boolean\n\tunits: MeasurementUnitsPreference\n\tsetUnit: (preferenceKey: string, unit: MeasurementUnitId) => void\n}\n\nconst Context = createContext<MeasurementUnitsContextValue | null>(null)\n\nconst isPreference = (value: unknown): value is MeasurementUnitsPreference =>\n\ttypeof value === 'object' && value !== null && !Array.isArray(value)\n\n/**\n * Reactive layer over payload-preferences for the per-bucket display units.\n * Payload's own usePreferences is ref-backed and never re-renders consumers,\n * so this provider holds the map in state: one toggle re-renders every\n * measurement field and cell on the page, then persists with merge mode.\n */\nexport const MeasurementUnitsProvider: React.FC<{\n\tchildren: React.ReactNode\n\t/** Kiosk/shared-account mode: skips both the preference fetch and writes, state stays session-only. Defaults to true. */\n\tpersist?: boolean\n}> = ({ children, persist = true }) => {\n\tconst { getPreference, setPreference } = usePreferences()\n\tconst [units, setUnits] = useState<MeasurementUnitsPreference>({})\n\t// No fetch to wait for when session-only, so first paint is already the final state.\n\tconst [ready, setReady] = useState(() => !persist)\n\n\tuseEffect(() => {\n\t\tif (!persist) return\n\t\tlet cancelled = false\n\t\tgetPreference<MeasurementUnitsPreference | null>(MEASUREMENT_PREFERENCE_KEY)\n\t\t\t.then((stored) => {\n\t\t\t\tif (cancelled) return\n\t\t\t\t// Toggles made before the fetch resolves win over the stored snapshot\n\t\t\t\tif (isPreference(stored)) setUnits((current) => ({ ...stored, ...current }))\n\t\t\t\tsetReady(true)\n\t\t\t})\n\t\t\t.catch(() => {\n\t\t\t\tif (!cancelled) setReady(true)\n\t\t\t})\n\t\treturn () => {\n\t\t\tcancelled = true\n\t\t}\n\t}, [getPreference, persist])\n\n\tconst setUnit = useCallback(\n\t\t(preferenceKey: string, unit: MeasurementUnitId) => {\n\t\t\tsetUnits((current) => ({ ...current, [preferenceKey]: unit }))\n\t\t\tif (persist) void setPreference(MEASUREMENT_PREFERENCE_KEY, { [preferenceKey]: unit }, true)\n\t\t},\n\t\t[persist, setPreference]\n\t)\n\n\tconst value = useMemo(() => ({ ready, setUnit, units }), [ready, setUnit, units])\n\treturn <Context.Provider value={value}>{children}</Context.Provider>\n}\n\nexport const useMeasurementUnits = (): MeasurementUnitsContextValue | null => useContext(Context)\n"],"mappings":";;;;;;AAaA,MAAM,UAAU,cAAmD,IAAI;AAEvE,MAAM,gBAAgB,UACrB,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAC,MAAM,QAAQ,KAAK;;;;;;;AAQpE,MAAa,4BAIP,EAAE,UAAU,UAAU,WAAW;CACtC,MAAM,EAAE,eAAe,kBAAkB,eAAe;CACxD,MAAM,CAAC,OAAO,YAAY,SAAqC,CAAC,CAAC;CAEjE,MAAM,CAAC,OAAO,YAAY,eAAe,CAAC,OAAO;CAEjD,gBAAgB;EACf,IAAI,CAAC,SAAS;EACd,IAAI,YAAY;EAChB,cAAiD,0BAA0B,EACzE,MAAM,WAAW;GACjB,IAAI,WAAW;GAEf,IAAI,aAAa,MAAM,GAAG,UAAU,aAAa;IAAE,GAAG;IAAQ,GAAG;GAAQ,EAAE;GAC3E,SAAS,IAAI;EACd,CAAC,EACA,YAAY;GACZ,IAAI,CAAC,WAAW,SAAS,IAAI;EAC9B,CAAC;EACF,aAAa;GACZ,YAAY;EACb;CACD,GAAG,CAAC,eAAe,OAAO,CAAC;CAE3B,MAAM,UAAU,aACd,eAAuB,SAA4B;EACnD,UAAU,aAAa;GAAE,GAAG;IAAU,gBAAgB;EAAK,EAAE;EAC7D,IAAI,SAAS,cAAmB,4BAA4B,GAAG,gBAAgB,KAAK,GAAG,IAAI;CAC5F,GACA,CAAC,SAAS,aAAa,CACxB;CAEA,MAAM,QAAQ,eAAe;EAAE;EAAO;EAAS;CAAM,IAAI;EAAC;EAAO;EAAS;CAAK,CAAC;CAChF,OAAO,oBAAC,QAAQ,UAAT;EAAyB;EAAQ;CAA2B,CAAA;AACpE;AAEA,MAAa,4BAAiE,WAAW,OAAO"}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { COMPOUNDS } from "../engine/units.js";
|
|
2
|
+
import "../engine/locale.js";
|
|
3
|
+
import { roundTo } from "../engine/convert.js";
|
|
4
|
+
//#region src/fields/measurement/client/editModel.ts
|
|
5
|
+
const numberToDraft = (value) => String(value);
|
|
6
|
+
const parseDraft = (raw) => {
|
|
7
|
+
if (raw.trim() === "") return null;
|
|
8
|
+
const parsed = Number(raw);
|
|
9
|
+
return Number.isFinite(parsed) ? parsed : null;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* The major part of a compound draft. An empty major beside a minor that holds a number
|
|
13
|
+
* reads as zero (`'' ft 5 in` is 5 in), so typing only the minor part commits; with both
|
|
14
|
+
* parts empty, or an unparseable major, there is nothing to commit.
|
|
15
|
+
*/
|
|
16
|
+
const parseMajorDraft = (drafts) => {
|
|
17
|
+
if (drafts.primary.trim() === "" && parseDraft(drafts.minor) !== null) return 0;
|
|
18
|
+
return parseDraft(drafts.primary);
|
|
19
|
+
};
|
|
20
|
+
const ALL_DIRTY = {
|
|
21
|
+
primary: true,
|
|
22
|
+
minor: true
|
|
23
|
+
};
|
|
24
|
+
const commitDrafts = (drafts, opts) => {
|
|
25
|
+
const { displayUnit, dirty = ALL_DIRTY, engine, entry = "free", paintedDrafts, precision, storageDigits = 6, storageUnit, storedValue = null } = opts;
|
|
26
|
+
if (engine.isCompoundUnit(displayUnit)) {
|
|
27
|
+
const compound = COMPOUNDS[displayUnit];
|
|
28
|
+
const hasStored = typeof storedValue === "number";
|
|
29
|
+
const dirtyCount = (dirty.primary ? 1 : 0) + (dirty.minor ? 1 : 0);
|
|
30
|
+
if (hasStored && dirtyCount === 1 && paintedDrafts) {
|
|
31
|
+
const exact = engine.decompose(storedValue, storageUnit, displayUnit, storageDigits);
|
|
32
|
+
const exactTotal = exact.major * compound.ratio + exact.minor;
|
|
33
|
+
if (dirty.primary) {
|
|
34
|
+
let typedMajor = parseMajorDraft(drafts);
|
|
35
|
+
if (typedMajor === null) return null;
|
|
36
|
+
if (entry === "quantize") typedMajor = roundTo(typedMajor, engine.precisionFor(compound.major, precision));
|
|
37
|
+
const paintedMajor = parseDraft(paintedDrafts.primary) ?? exact.major;
|
|
38
|
+
const newTotal = exactTotal + (typedMajor - paintedMajor) * compound.ratio;
|
|
39
|
+
return roundTo(engine.compose({
|
|
40
|
+
major: 0,
|
|
41
|
+
minor: newTotal
|
|
42
|
+
}, displayUnit, storageUnit), storageDigits);
|
|
43
|
+
}
|
|
44
|
+
const rawMinor = parseDraft(drafts.minor) ?? 0;
|
|
45
|
+
const newTotal = exactTotal + ((entry === "quantize" ? roundTo(rawMinor, engine.precisionFor(displayUnit, precision)) : rawMinor) - (parseDraft(paintedDrafts.minor) ?? 0));
|
|
46
|
+
return roundTo(engine.compose({
|
|
47
|
+
major: 0,
|
|
48
|
+
minor: newTotal
|
|
49
|
+
}, displayUnit, storageUnit), storageDigits);
|
|
50
|
+
}
|
|
51
|
+
const stored = hasStored ? engine.decompose(storedValue, storageUnit, displayUnit, storageDigits) : null;
|
|
52
|
+
let major;
|
|
53
|
+
if (dirty.primary) {
|
|
54
|
+
major = parseMajorDraft(drafts);
|
|
55
|
+
if (major !== null && entry === "quantize") major = roundTo(major, engine.precisionFor(compound.major, precision));
|
|
56
|
+
} else major = stored ? stored.major : parseMajorDraft(drafts);
|
|
57
|
+
if (major === null) return null;
|
|
58
|
+
let minor;
|
|
59
|
+
if (dirty.minor) {
|
|
60
|
+
const rawMinor = parseDraft(drafts.minor) ?? 0;
|
|
61
|
+
const maxMinor = compound.ratio - .001;
|
|
62
|
+
const clampedRaw = Math.sign(rawMinor) * Math.min(Math.abs(rawMinor), maxMinor);
|
|
63
|
+
minor = entry === "quantize" ? roundTo(clampedRaw, engine.precisionFor(displayUnit, precision)) : clampedRaw;
|
|
64
|
+
} else minor = stored ? stored.minor : parseDraft(paintedDrafts?.minor ?? "") ?? 0;
|
|
65
|
+
return roundTo(engine.compose({
|
|
66
|
+
major,
|
|
67
|
+
minor
|
|
68
|
+
}, displayUnit, storageUnit), storageDigits);
|
|
69
|
+
}
|
|
70
|
+
if (!dirty.primary) return storedValue;
|
|
71
|
+
const parsed = parseDraft(drafts.primary);
|
|
72
|
+
if (parsed === null) return null;
|
|
73
|
+
const value = entry === "quantize" ? roundTo(parsed, engine.precisionFor(displayUnit, precision)) : parsed;
|
|
74
|
+
return roundTo(engine.convert(value, displayUnit, storageUnit), storageDigits);
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* Drafts for repainting from a stored value. 'faithful' (default) is the shortest
|
|
78
|
+
* draft that commits back to the stored value: display precision first, then one
|
|
79
|
+
* more fraction digit at a time up to storage precision, since a draft that
|
|
80
|
+
* silently truncates the stored value would write the truncation back on the next
|
|
81
|
+
* save. 'display' skips escalation and renders straight at display digits
|
|
82
|
+
* (compound parts each at their own digits), trading faithfulness for a value that
|
|
83
|
+
* always matches what the unit picker promises.
|
|
84
|
+
*/
|
|
85
|
+
const draftsFor = (value, opts) => {
|
|
86
|
+
if (typeof value !== "number" || Number.isNaN(value)) return {
|
|
87
|
+
minor: "",
|
|
88
|
+
primary: ""
|
|
89
|
+
};
|
|
90
|
+
const { displayUnit, draft = "faithful", engine, precision, storageDigits = 6, storageUnit } = opts;
|
|
91
|
+
const draftAt = (digits) => {
|
|
92
|
+
if (engine.isCompoundUnit(displayUnit)) {
|
|
93
|
+
const parts = engine.decompose(value, storageUnit, displayUnit, digits);
|
|
94
|
+
return {
|
|
95
|
+
minor: numberToDraft(parts.minor),
|
|
96
|
+
primary: numberToDraft(parts.major)
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
minor: "",
|
|
101
|
+
primary: numberToDraft(roundTo(engine.convert(value, storageUnit, displayUnit), digits))
|
|
102
|
+
};
|
|
103
|
+
};
|
|
104
|
+
const displayDigits = engine.precisionFor(displayUnit, precision);
|
|
105
|
+
if (draft === "display") return draftAt(displayDigits);
|
|
106
|
+
const maxDigits = Math.max(displayDigits, storageDigits);
|
|
107
|
+
const target = roundTo(value, storageDigits);
|
|
108
|
+
const probeOpts = {
|
|
109
|
+
displayUnit,
|
|
110
|
+
engine,
|
|
111
|
+
precision,
|
|
112
|
+
storageDigits,
|
|
113
|
+
storageUnit
|
|
114
|
+
};
|
|
115
|
+
for (let digits = displayDigits; digits < maxDigits; digits++) {
|
|
116
|
+
const candidate = draftAt(digits);
|
|
117
|
+
if (commitDrafts(candidate, probeOpts) === target) return candidate;
|
|
118
|
+
}
|
|
119
|
+
return draftAt(maxDigits);
|
|
120
|
+
};
|
|
121
|
+
//#endregion
|
|
122
|
+
export { commitDrafts, draftsFor };
|
|
123
|
+
|
|
124
|
+
//# sourceMappingURL=editModel.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"editModel.js","names":[],"sources":["../../../../src/fields/measurement/client/editModel.ts"],"sourcesContent":["import { roundTo } from '../engine/convert'\nimport type { MeasurementEngine } from '../engine/registry'\nimport { COMPOUNDS, type MeasurementUnitId, STORAGE_FRACTION_DIGITS } from '../engine/units'\n\n/** Re-exported so MeasurementField/MeasurementCell keep one import site for the edit model. */\nexport { resolveDisplayUnit } from '../engine/locale'\n\nexport type MeasurementDrafts = { primary: string; minor: string }\n\n/** Which inputs the viewer has actually typed into since the last resync. */\nexport type DirtyDrafts = { primary: boolean; minor: boolean }\n\ntype UnitContext = {\n\t/** Bound over the field's custom units, so custom ids convert and format like built-ins. */\n\tengine: MeasurementEngine\n\tstorageUnit: MeasurementUnitId\n\tdisplayUnit: MeasurementUnitId\n\tprecision?: Partial<Record<MeasurementUnitId, number>>\n\t/**\n\t * Draft-repaint policy: 'faithful' (default) escalates fraction digits until the\n\t * draft round-trips to the stored value; 'display' renders straight at display\n\t * digits, so a stored value can print shorter than it is stored.\n\t */\n\tdraft?: 'display' | 'faithful'\n\t/** Fraction digits kept in the storage unit. Defaults to STORAGE_FRACTION_DIGITS. */\n\tstorageDigits?: number\n}\n\ntype CommitOptions = UnitContext & {\n\t/**\n\t * Entry-unit typing behavior: 'quantize' rounds a dirty part at its own display\n\t * digits, in the unit it was typed in, before converting; 'free' (default) keeps\n\t * the typed value exact.\n\t */\n\tentry?: 'quantize' | 'free'\n\t/** An untouched part never commits its displayed string; only a dirty one does. */\n\tdirty?: DirtyDrafts\n\t/** Current stored value, used to derive an untouched part's exact contribution. */\n\tstoredValue?: number | null\n\t/**\n\t * The compound drafts as painted on screen immediately before this edit (not the\n\t * post-keystroke `drafts`). A display-digit repaint can carry (182.5 cm storage\n\t * splits exactly to 5 ft 11.850394 in but paints as 6 ft 0 in), so when exactly\n\t * one part is dirty, the edited part's contribution is computed as a delta off\n\t * this baseline rather than a full replace, which would otherwise bake the\n\t * painted carry into storage.\n\t */\n\tpaintedDrafts?: MeasurementDrafts\n}\n\nconst numberToDraft = (value: number): string => String(value)\n\nconst parseDraft = (raw: string): number | null => {\n\tif (raw.trim() === '') return null\n\tconst parsed = Number(raw)\n\treturn Number.isFinite(parsed) ? parsed : null\n}\n\n/**\n * The major part of a compound draft. An empty major beside a minor that holds a number\n * reads as zero (`'' ft 5 in` is 5 in), so typing only the minor part commits; with both\n * parts empty, or an unparseable major, there is nothing to commit.\n */\nconst parseMajorDraft = (drafts: MeasurementDrafts): number | null => {\n\tif (drafts.primary.trim() === '' && parseDraft(drafts.minor) !== null) return 0\n\treturn parseDraft(drafts.primary)\n}\n\n// draftsFor/commitDrafts default to 'faithful'/'free'/ALL_DIRTY when a caller omits\n// them, i.e. the shape of 'exact' mode, not the engine's own 'readable' default: the\n// component always threads its resolved precision explicitly, so these bare defaults\n// exist only for a caller invoking editModel directly (tests, or an embedder outside\n// the component), and preserve the single behavior this module had before precision\n// modes existed.\nconst ALL_DIRTY: DirtyDrafts = { primary: true, minor: true }\n\nexport const commitDrafts = (drafts: MeasurementDrafts, opts: CommitOptions): number | null => {\n\tconst {\n\t\tdisplayUnit,\n\t\tdirty = ALL_DIRTY,\n\t\tengine,\n\t\tentry = 'free',\n\t\tpaintedDrafts,\n\t\tprecision,\n\t\tstorageDigits = STORAGE_FRACTION_DIGITS,\n\t\tstorageUnit,\n\t\tstoredValue = null,\n\t} = opts\n\n\tif (engine.isCompoundUnit(displayUnit)) {\n\t\tconst compound = COMPOUNDS[displayUnit]\n\t\tconst hasStored = typeof storedValue === 'number'\n\t\tconst dirtyCount = (dirty.primary ? 1 : 0) + (dirty.minor ? 1 : 0)\n\n\t\t// Delta path: exactly one part was touched and a painted baseline exists, so\n\t\t// the untouched part's contribution comes entirely from the exact stored\n\t\t// total (never its carried, display-precision draft), and the touched part\n\t\t// contributes only the change the viewer actually typed, in minor units.\n\t\tif (hasStored && dirtyCount === 1 && paintedDrafts) {\n\t\t\tconst exact = engine.decompose(storedValue as number, storageUnit, displayUnit, storageDigits)\n\t\t\tconst exactTotal = exact.major * compound.ratio + exact.minor\n\n\t\t\tif (dirty.primary) {\n\t\t\t\tlet typedMajor = parseMajorDraft(drafts)\n\t\t\t\tif (typedMajor === null) return null\n\t\t\t\tif (entry === 'quantize') {\n\t\t\t\t\ttypedMajor = roundTo(typedMajor, engine.precisionFor(compound.major, precision))\n\t\t\t\t}\n\t\t\t\tconst paintedMajor = parseDraft(paintedDrafts.primary) ?? exact.major\n\t\t\t\tconst newTotal = exactTotal + (typedMajor - paintedMajor) * compound.ratio\n\t\t\t\treturn roundTo(\n\t\t\t\t\tengine.compose({ major: 0, minor: newTotal }, displayUnit, storageUnit),\n\t\t\t\t\tstorageDigits\n\t\t\t\t)\n\t\t\t}\n\n\t\t\tconst rawMinor = parseDraft(drafts.minor) ?? 0\n\t\t\tconst typedMinor =\n\t\t\t\tentry === 'quantize'\n\t\t\t\t\t? roundTo(rawMinor, engine.precisionFor(displayUnit, precision))\n\t\t\t\t\t: rawMinor\n\t\t\tconst paintedMinor = parseDraft(paintedDrafts.minor) ?? 0\n\t\t\tconst newTotal = exactTotal + (typedMinor - paintedMinor)\n\t\t\treturn roundTo(\n\t\t\t\tengine.compose({ major: 0, minor: newTotal }, displayUnit, storageUnit),\n\t\t\t\tstorageDigits\n\t\t\t)\n\t\t}\n\n\t\t// Both parts dirty (an explicit full replace) or no baseline to diff\n\t\t// against: compose the two parts directly, as before.\n\t\tconst stored = hasStored\n\t\t\t? engine.decompose(storedValue as number, storageUnit, displayUnit, storageDigits)\n\t\t\t: null\n\n\t\tlet major: number | null\n\t\tif (dirty.primary) {\n\t\t\tmajor = parseMajorDraft(drafts)\n\t\t\tif (major !== null && entry === 'quantize') {\n\t\t\t\tmajor = roundTo(major, engine.precisionFor(compound.major, precision))\n\t\t\t}\n\t\t} else {\n\t\t\tmajor = stored ? stored.major : parseMajorDraft(drafts)\n\t\t}\n\t\tif (major === null) return null\n\n\t\tlet minor: number\n\t\tif (dirty.minor) {\n\t\t\tconst rawMinor = parseDraft(drafts.minor) ?? 0\n\t\t\tconst maxMinor = compound.ratio - 0.001\n\t\t\t// Clamp the raw typed magnitude (bounds a literal 24) before quantizing:\n\t\t\t// a quantized value landing exactly on the ratio (11.96 -> 12) must be\n\t\t\t// free to carry into the major via compose, matching decompose's own\n\t\t\t// carry contract, so nothing clamps it back down afterward.\n\t\t\tconst clampedRaw = Math.sign(rawMinor) * Math.min(Math.abs(rawMinor), maxMinor)\n\t\t\tminor =\n\t\t\t\tentry === 'quantize'\n\t\t\t\t\t? roundTo(clampedRaw, engine.precisionFor(displayUnit, precision))\n\t\t\t\t\t: clampedRaw\n\t\t} else {\n\t\t\t// No stored value to derive an exact minor from (a brand new, never-saved\n\t\t\t// field): fall back to the painted baseline, matching major's own fallback,\n\t\t\t// never a bare 0 (a cleared-then-retyped major would otherwise commit with\n\t\t\t// its minor silently zeroed).\n\t\t\tminor = stored ? stored.minor : (parseDraft(paintedDrafts?.minor ?? '') ?? 0)\n\t\t}\n\n\t\treturn roundTo(engine.compose({ major, minor }, displayUnit, storageUnit), storageDigits)\n\t}\n\n\tif (!dirty.primary) return storedValue\n\tconst parsed = parseDraft(drafts.primary)\n\tif (parsed === null) return null\n\tconst value =\n\t\tentry === 'quantize' ? roundTo(parsed, engine.precisionFor(displayUnit, precision)) : parsed\n\treturn roundTo(engine.convert(value, displayUnit, storageUnit), storageDigits)\n}\n\n/**\n * Drafts for repainting from a stored value. 'faithful' (default) is the shortest\n * draft that commits back to the stored value: display precision first, then one\n * more fraction digit at a time up to storage precision, since a draft that\n * silently truncates the stored value would write the truncation back on the next\n * save. 'display' skips escalation and renders straight at display digits\n * (compound parts each at their own digits), trading faithfulness for a value that\n * always matches what the unit picker promises.\n */\nexport const draftsFor = (value: number | null, opts: UnitContext): MeasurementDrafts => {\n\tif (typeof value !== 'number' || Number.isNaN(value)) return { minor: '', primary: '' }\n\tconst {\n\t\tdisplayUnit,\n\t\tdraft = 'faithful',\n\t\tengine,\n\t\tprecision,\n\t\tstorageDigits = STORAGE_FRACTION_DIGITS,\n\t\tstorageUnit,\n\t} = opts\n\tconst draftAt = (digits: number): MeasurementDrafts => {\n\t\tif (engine.isCompoundUnit(displayUnit)) {\n\t\t\tconst parts = engine.decompose(value, storageUnit, displayUnit, digits)\n\t\t\treturn { minor: numberToDraft(parts.minor), primary: numberToDraft(parts.major) }\n\t\t}\n\t\tconst converted = roundTo(engine.convert(value, storageUnit, displayUnit), digits)\n\t\treturn { minor: '', primary: numberToDraft(converted) }\n\t}\n\n\tconst displayDigits = engine.precisionFor(displayUnit, precision)\n\tif (draft === 'display') return draftAt(displayDigits)\n\n\t// Digits past storage precision never survive a save, so that is where escalation stops.\n\tconst maxDigits = Math.max(displayDigits, storageDigits)\n\tconst target = roundTo(value, storageDigits)\n\t// A bare probe: never forward `opts` itself, so an entry/dirty/storedValue field\n\t// a future CommitOptions caller happens to carry can never poison this check.\n\tconst probeOpts: UnitContext = { displayUnit, engine, precision, storageDigits, storageUnit }\n\tfor (let digits = displayDigits; digits < maxDigits; digits++) {\n\t\tconst candidate = draftAt(digits)\n\t\tif (commitDrafts(candidate, probeOpts) === target) return candidate\n\t}\n\treturn draftAt(maxDigits)\n}\n"],"mappings":";;;;AAkDA,MAAM,iBAAiB,UAA0B,OAAO,KAAK;AAE7D,MAAM,cAAc,QAA+B;CAClD,IAAI,IAAI,KAAK,MAAM,IAAI,OAAO;CAC9B,MAAM,SAAS,OAAO,GAAG;CACzB,OAAO,OAAO,SAAS,MAAM,IAAI,SAAS;AAC3C;;;;;;AAOA,MAAM,mBAAmB,WAA6C;CACrE,IAAI,OAAO,QAAQ,KAAK,MAAM,MAAM,WAAW,OAAO,KAAK,MAAM,MAAM,OAAO;CAC9E,OAAO,WAAW,OAAO,OAAO;AACjC;AAQA,MAAM,YAAyB;CAAE,SAAS;CAAM,OAAO;AAAK;AAE5D,MAAa,gBAAgB,QAA2B,S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