@vritti/quantum-ui 0.6.9 → 0.6.11
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/dist/Avatar.js +5 -5
- package/dist/Badge2.js +86 -11
- package/dist/Badge2.js.map +1 -1
- package/dist/ChartFrame.js +4 -1
- package/dist/ChartFrame.js.map +1 -1
- package/dist/Checkbox2.js +5 -5
- package/dist/CurrencyFieldFilter.js +60 -34
- package/dist/CurrencyFieldFilter.js.map +1 -1
- package/dist/Dialog.js +1 -1
- package/dist/DropdownMenu.js +6 -1
- package/dist/DropdownMenu.js.map +1 -1
- package/dist/IconSelect.js +2302 -2324
- package/dist/IconSelect.js.map +1 -1
- package/dist/Input.js +1 -1
- package/dist/Input.js.map +1 -1
- package/dist/Label.js +1 -1
- package/dist/OTPField.js +2 -2
- package/dist/OTPField.js.map +1 -1
- package/dist/PhoneField.js +2 -2
- package/dist/PhoneField.js.map +1 -1
- package/dist/Progress.js +2 -2
- package/dist/QuantityField.js +437 -105
- package/dist/QuantityField.js.map +1 -1
- package/dist/RadioGroup.js +7 -7
- package/dist/Select.js +92 -4
- package/dist/Select.js.map +1 -1
- package/dist/Sidebar.js +1 -1
- package/dist/SingleSelect.js +1 -1
- package/dist/SingleSelect.js.map +1 -1
- package/dist/Sonner.js +1 -1
- package/dist/Spinner.js +8 -2
- package/dist/Spinner.js.map +1 -1
- package/dist/Switch.js +5 -5
- package/dist/TabsSkeleton.js +2 -2
- package/dist/TextArea.js +1 -1
- package/dist/TextArea.js.map +1 -1
- package/dist/TextField.js +29 -27
- package/dist/TextField.js.map +1 -1
- package/dist/Toggle.js +1 -1
- package/dist/assets/quantum-ui.css +473 -171
- package/dist/bindFilters.js +14 -1
- package/dist/bindFilters.js.map +1 -1
- package/dist/components/ContextBar.js.map +1 -1
- package/dist/components/QuantityField.js +1 -1
- package/dist/components/RichTextEditor.js +1 -1
- package/dist/components/TreeView.js +4 -4
- package/dist/currencyCode.js +1520 -0
- package/dist/currencyCode.js.map +1 -0
- package/dist/dropdown-menu.js +9 -9
- package/dist/image-component.js +1 -1
- package/dist/index.js +2 -2
- package/dist/index10.js +37 -1499
- package/dist/index10.js.map +1 -1
- package/dist/index11.js +503 -28
- package/dist/index11.js.map +1 -1
- package/dist/index12.js +230 -454
- package/dist/index12.js.map +1 -1
- package/dist/index13.js +1073 -213
- package/dist/index13.js.map +1 -1
- package/dist/index14.js +20 -1139
- package/dist/index14.js.map +1 -1
- package/dist/index15.js +14 -20
- package/dist/index15.js.map +1 -1
- package/dist/index16.js +173 -19
- package/dist/index16.js.map +1 -1
- package/dist/index17.js +173 -168
- package/dist/index17.js.map +1 -1
- package/dist/index18.js +201 -171
- package/dist/index18.js.map +1 -1
- package/dist/index19.js +190 -186
- package/dist/index19.js.map +1 -1
- package/dist/index20.js +204 -202
- package/dist/index20.js.map +1 -1
- package/dist/index21.js +68 -209
- package/dist/index21.js.map +1 -1
- package/dist/index22.js +1128 -70
- package/dist/index22.js.map +1 -1
- package/dist/index23.js +4 -1133
- package/dist/index23.js.map +1 -1
- package/dist/index24.js +232 -4
- package/dist/index24.js.map +1 -1
- package/dist/index25.js +1100 -207
- package/dist/index25.js.map +1 -1
- package/dist/index26.js +2469 -1023
- package/dist/index26.js.map +1 -1
- package/dist/index27.js +35 -2569
- package/dist/index27.js.map +1 -1
- package/dist/index28.js +308 -36
- package/dist/index28.js.map +1 -1
- package/dist/index29.js +7 -307
- package/dist/index29.js.map +1 -1
- package/dist/index30.js +30 -8
- package/dist/index30.js.map +1 -1
- package/dist/index31.js +59 -29
- package/dist/index31.js.map +1 -1
- package/dist/index32.js +10 -59
- package/dist/index32.js.map +1 -1
- package/dist/index33.js +35347 -9
- package/dist/index33.js.map +1 -1
- package/dist/index34.js +72 -35342
- package/dist/index34.js.map +1 -1
- package/dist/index35.js +187 -72
- package/dist/index35.js.map +1 -1
- package/dist/index5.js +4 -4
- package/dist/index9.js +1 -1
- package/dist/lib/components/ContextBar/useContextState.d.ts +7 -1
- package/dist/lib/components/ContextBar/useContextState.d.ts.map +1 -1
- package/dist/lib/components/CurrencyField/CurrencyField.d.ts +8 -4
- package/dist/lib/components/CurrencyField/CurrencyField.d.ts.map +1 -1
- package/dist/lib/components/CurrencyField/CurrencyFieldFilter.d.ts +3 -2
- package/dist/lib/components/CurrencyField/CurrencyFieldFilter.d.ts.map +1 -1
- package/dist/lib/components/DataTable/DataTable.d.ts.map +1 -1
- package/dist/lib/components/DataTable/components/DataTablePagination.d.ts.map +1 -1
- package/dist/lib/components/DataTable/components/DataTableSelectionBar.d.ts +3 -2
- package/dist/lib/components/DataTable/components/DataTableSelectionBar.d.ts.map +1 -1
- package/dist/lib/components/DataTable/hooks/useDataTable.d.ts +4 -1
- package/dist/lib/components/DataTable/hooks/useDataTable.d.ts.map +1 -1
- package/dist/lib/components/DataTable/index.d.ts +1 -1
- package/dist/lib/components/DataTable/index.d.ts.map +1 -1
- package/dist/lib/components/DataTable/types.d.ts +13 -1
- package/dist/lib/components/DataTable/types.d.ts.map +1 -1
- package/dist/lib/components/DataTable/utils.d.ts +2 -2
- package/dist/lib/components/DataTable/utils.d.ts.map +1 -1
- package/dist/lib/components/DropdownMenu/DropdownMenu.d.ts.map +1 -1
- package/dist/lib/components/DropdownMenu/types.d.ts +2 -0
- package/dist/lib/components/DropdownMenu/types.d.ts.map +1 -1
- package/dist/lib/components/Field/numberConstraints.d.ts +19 -0
- package/dist/lib/components/Field/numberConstraints.d.ts.map +1 -0
- package/dist/lib/components/PhoneField/PhoneField.d.ts.map +1 -1
- package/dist/lib/components/QuantityField/QuantityField.d.ts +11 -1
- package/dist/lib/components/QuantityField/QuantityField.d.ts.map +1 -1
- package/dist/lib/components/QuantityField/index.d.ts +1 -1
- package/dist/lib/components/QuantityField/index.d.ts.map +1 -1
- package/dist/lib/components/QuantityField/quantity.d.ts +14 -0
- package/dist/lib/components/QuantityField/quantity.d.ts.map +1 -1
- package/dist/lib/components/Select/Select.d.ts +3 -0
- package/dist/lib/components/Select/Select.d.ts.map +1 -1
- package/dist/lib/components/Select/hooks/useSelect.d.ts +2 -0
- package/dist/lib/components/Select/hooks/useSelect.d.ts.map +1 -1
- package/dist/lib/components/TextField/TextField.d.ts.map +1 -1
- package/dist/lib/hooks/useWorkspaceCurrency.d.ts +2 -1
- package/dist/lib/hooks/useWorkspaceCurrency.d.ts.map +1 -1
- package/dist/lib/types/conditions.d.ts +2 -1
- package/dist/lib/types/conditions.d.ts.map +1 -1
- package/dist/lib/{components/DataTable/components → utils}/bindFilters.d.ts +2 -1
- package/dist/lib/utils/bindFilters.d.ts.map +1 -0
- package/dist/lib/utils/currency.d.ts +7 -5
- package/dist/lib/utils/currency.d.ts.map +1 -1
- package/dist/lib/utils/currencyCode.d.ts +14 -0
- package/dist/lib/utils/currencyCode.d.ts.map +1 -0
- package/dist/lib/utils/format.d.ts +7 -5
- package/dist/lib/utils/format.d.ts.map +1 -1
- package/dist/lib/utils/money.d.ts +5 -4
- package/dist/lib/utils/money.d.ts.map +1 -1
- package/dist/lib/utils/store.d.ts +3 -0
- package/dist/lib/utils/store.d.ts.map +1 -0
- package/dist/lib/utils/zod.d.ts +3 -1
- package/dist/lib/utils/zod.d.ts.map +1 -1
- package/dist/numberConstraints.js +68 -0
- package/dist/numberConstraints.js.map +1 -0
- package/dist/popover.js +5 -2
- package/dist/popover.js.map +1 -1
- package/dist/react.js +45 -0
- package/dist/react.js.map +1 -0
- package/dist/ruler.js +27 -0
- package/dist/ruler.js.map +1 -0
- package/dist/select2.js +2 -2
- package/dist/select2.js.map +1 -1
- package/dist/separator2.js +1 -1
- package/dist/shadcn/shadcnBadge/Badge.d.ts +5 -2
- package/dist/shadcn/shadcnBadge/Badge.d.ts.map +1 -1
- package/dist/shadcn/shadcnChart/Chart.d.ts.map +1 -1
- package/dist/shadcn/shadcnPopover/index.d.ts +1 -1
- package/dist/shadcn/shadcnPopover/index.d.ts.map +1 -1
- package/dist/shadcn/shadcnPopover/popover.d.ts +2 -1
- package/dist/shadcn/shadcnPopover/popover.d.ts.map +1 -1
- package/dist/tooltip2.js +1 -1
- package/dist/useContextState.js +39 -6
- package/dist/useContextState.js.map +1 -1
- package/dist/useDataTable.js +148 -64
- package/dist/useDataTable.js.map +1 -1
- package/dist/useWorkspaceCurrency.js.map +1 -1
- package/dist/utils/currency.js +3 -14
- package/dist/utils/currency.js.map +1 -1
- package/dist/utils/format.js +14 -6
- package/dist/utils/format.js.map +1 -1
- package/dist/utils/money.js +5 -3
- package/dist/utils/money.js.map +1 -1
- package/dist/utils/motion.js +2 -2
- package/dist/utils/store.js +67 -0
- package/dist/utils/store.js.map +1 -0
- package/dist/utils/zod.js +10 -7
- package/dist/utils/zod.js.map +1 -1
- package/dist/value.js +1 -1
- package/package.json +5 -1
- package/dist/Spinner2.js +0 -10
- package/dist/Spinner2.js.map +0 -1
- package/dist/index36.js +0 -199
- package/dist/index36.js.map +0 -1
- package/dist/lib/components/DataTable/components/bindFilters.d.ts.map +0 -1
package/dist/QuantityField.js
CHANGED
|
@@ -4,9 +4,13 @@ import { F as Field, e as FieldLabel, a as FieldContent, b as FieldDescription,
|
|
|
4
4
|
import { c as cn } from './utils.js';
|
|
5
5
|
import { formatNumber } from './utils/format.js';
|
|
6
6
|
import { getLocale } from './utils/locale.js';
|
|
7
|
+
import { B as Button } from './Button.js';
|
|
8
|
+
import { b as boundWords, N as NumberFieldLabelRow, B as BoundAfterValue, i as isOutOfBounds } from './numberConstraints.js';
|
|
7
9
|
import { S as Select } from './Select.js';
|
|
8
10
|
import Decimal from './utils/decimal.js';
|
|
9
|
-
import {
|
|
11
|
+
import { P as Popover, c as PopoverAnchor, a as PopoverTrigger, b as PopoverContent } from './popover.js';
|
|
12
|
+
import { R as Ruler } from './ruler.js';
|
|
13
|
+
import { a as Spinner } from './Spinner.js';
|
|
10
14
|
import { C as ChevronDown } from './chevron-down.js';
|
|
11
15
|
|
|
12
16
|
const PRIMARY_DECIMALS = 3;
|
|
@@ -27,6 +31,86 @@ function fromPrimaryQty(primaryQty, unit) {
|
|
|
27
31
|
if (primaryQty == null || !Number.isFinite(primaryQty)) return "";
|
|
28
32
|
return new Decimal(primaryQty).times(unit.uomQty).div(unit.primaryUomQty).toDecimalPlaces(UNIT_DECIMALS).toFixed();
|
|
29
33
|
}
|
|
34
|
+
function unitSize(unit) {
|
|
35
|
+
return new Decimal(unit.primaryUomQty).div(unit.uomQty);
|
|
36
|
+
}
|
|
37
|
+
function isPackUnit(unit, primaryUnitId) {
|
|
38
|
+
return unit.id !== primaryUnitId && unit.isPack !== false;
|
|
39
|
+
}
|
|
40
|
+
function packLadder(anchor, units, primaryUnitId, cascade) {
|
|
41
|
+
const ladder = [anchor];
|
|
42
|
+
if (!cascade) return ladder;
|
|
43
|
+
const smaller = units.filter((unit) => isPackUnit(unit, primaryUnitId) && unit.id !== anchor.id).filter((unit) => unitSize(unit).lt(unitSize(anchor)) && unitSize(unit).gt(1)).sort((a, b) => unitSize(b).cmp(unitSize(a)));
|
|
44
|
+
for (const unit of smaller) {
|
|
45
|
+
if (unitSize(ladder[ladder.length - 1]).mod(unitSize(unit)).isZero())
|
|
46
|
+
ladder.push(unit);
|
|
47
|
+
}
|
|
48
|
+
return ladder;
|
|
49
|
+
}
|
|
50
|
+
function breakDownQty(primaryQty, units, anchorUnitId, primaryUnitId, cascade = false) {
|
|
51
|
+
const primary = units.find((unit) => unit.id === primaryUnitId);
|
|
52
|
+
if (!primary || !Number.isFinite(primaryQty)) return [];
|
|
53
|
+
const anchor = units.find((unit) => unit.id === anchorUnitId) ?? primary;
|
|
54
|
+
const total = new Decimal(primaryQty);
|
|
55
|
+
if (anchor.id !== primary.id && !isPackUnit(anchor, primaryUnitId)) {
|
|
56
|
+
return [{ unit: anchor, amount: total.div(unitSize(anchor)).toDecimalPlaces(PRIMARY_DECIMALS) }];
|
|
57
|
+
}
|
|
58
|
+
const pieces = [];
|
|
59
|
+
let left = total;
|
|
60
|
+
if (anchor.id !== primary.id) {
|
|
61
|
+
for (const unit of packLadder(anchor, units, primaryUnitId, cascade)) {
|
|
62
|
+
const amount = left.div(unitSize(unit)).floor();
|
|
63
|
+
pieces.push({ unit, amount });
|
|
64
|
+
left = left.minus(amount.times(unitSize(unit)));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
pieces.push({ unit: primary, amount: left.toDecimalPlaces(PRIMARY_DECIMALS) });
|
|
68
|
+
return pieces;
|
|
69
|
+
}
|
|
70
|
+
function entryUnits(units, primaryUnitId, anchorUnitId) {
|
|
71
|
+
return units.filter((unit) => unit.id === primaryUnitId || unit.id === anchorUnitId || isPackUnit(unit, primaryUnitId)).sort((a, b) => unitSize(b).cmp(unitSize(a)));
|
|
72
|
+
}
|
|
73
|
+
function takesDecimals(unit, primaryUnitId, allowDecimal) {
|
|
74
|
+
return allowDecimal && !isPackUnit(unit, primaryUnitId);
|
|
75
|
+
}
|
|
76
|
+
function toDisplayQuantity(primaryQty, units, primaryUnitId, displayUnitId, cascade = false) {
|
|
77
|
+
const primary = units.find((unit) => unit.id === primaryUnitId);
|
|
78
|
+
if (!primary || !Number.isFinite(primaryQty)) return null;
|
|
79
|
+
const display = units.find((unit) => unit.id === displayUnitId) ?? primary;
|
|
80
|
+
const size = unitSize(display);
|
|
81
|
+
const total = new Decimal(primaryQty);
|
|
82
|
+
const whole = total.div(size).trunc();
|
|
83
|
+
const isPack = isPackUnit(display, primaryUnitId);
|
|
84
|
+
const breakdown = isPack && primaryQty >= 0 ? breakDownQty(primaryQty, units, display.id, primaryUnitId, cascade).filter((piece) => !piece.amount.isZero()).map((piece) => ({ qty: piece.amount.toNumber(), symbol: piece.unit.symbol })) : [];
|
|
85
|
+
return {
|
|
86
|
+
primaryUomQty: primaryQty,
|
|
87
|
+
displayUomQty: total.div(size).toDecimalPlaces(PRIMARY_DECIMALS).toNumber(),
|
|
88
|
+
wholeDisplayUomQty: whole.toNumber(),
|
|
89
|
+
remainderPrimaryUomQty: total.minus(whole.times(size)).toDecimalPlaces(PRIMARY_DECIMALS).toNumber(),
|
|
90
|
+
displayUomSymbol: display.symbol,
|
|
91
|
+
primaryUomSymbol: primary.symbol,
|
|
92
|
+
isPack,
|
|
93
|
+
breakdown
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
function sumParts(parts, units) {
|
|
97
|
+
let total = null;
|
|
98
|
+
for (const unit of units) {
|
|
99
|
+
const amount = parseAmount(parts[unit.id] ?? "");
|
|
100
|
+
if (amount) total = (total ?? new Decimal(0)).plus(amount.times(unitSize(unit)));
|
|
101
|
+
}
|
|
102
|
+
return total?.toDecimalPlaces(PRIMARY_DECIMALS).toNumber();
|
|
103
|
+
}
|
|
104
|
+
function partsOfQty(primaryQty, units, anchorUnitId, primaryUnitId, cascade = false) {
|
|
105
|
+
if (primaryQty == null) return {};
|
|
106
|
+
const anchor = units.find((unit) => unit.id === anchorUnitId);
|
|
107
|
+
const enteredIn = anchor && isPackUnit(anchor, primaryUnitId) ? anchorUnitId : primaryUnitId;
|
|
108
|
+
const parts = breakDownQty(primaryQty, units, enteredIn, primaryUnitId, cascade).filter(
|
|
109
|
+
(piece) => !piece.amount.isZero()
|
|
110
|
+
);
|
|
111
|
+
if (parts.length === 0) return { [enteredIn]: "0" };
|
|
112
|
+
return Object.fromEntries(parts.map((piece) => [piece.unit.id, piece.amount.toFixed()]));
|
|
113
|
+
}
|
|
30
114
|
function quantityPlaceholder(primaryQty) {
|
|
31
115
|
return `{{qty:${primaryQty}}}`;
|
|
32
116
|
}
|
|
@@ -37,7 +121,9 @@ function resolveQuantityMessage(message, unit, primaryUnit, formatNumber) {
|
|
|
37
121
|
).split(PRIMARY_UNIT_PLACEHOLDER).join(primaryUnit.symbol);
|
|
38
122
|
}
|
|
39
123
|
|
|
40
|
-
const
|
|
124
|
+
const WHOLE_PATTERN = /^\d*$/;
|
|
125
|
+
const DECIMAL_PATTERN = /^\d*\.?\d{0,3}$/;
|
|
126
|
+
const DIGIT_KEY = /^\d$/;
|
|
41
127
|
function formatAmount(value) {
|
|
42
128
|
return String(formatNumber(value, { localeTag: getLocale() ?? "en-US" }).primary);
|
|
43
129
|
}
|
|
@@ -46,137 +132,383 @@ const QuantityField = React__default.forwardRef(
|
|
|
46
132
|
units,
|
|
47
133
|
primaryUnitId,
|
|
48
134
|
defaultUnitId,
|
|
135
|
+
anchorUnitId,
|
|
136
|
+
onAnchorUnitChange,
|
|
137
|
+
anchorUnitIds,
|
|
138
|
+
allowDecimal = false,
|
|
139
|
+
cascade = false,
|
|
140
|
+
compact,
|
|
49
141
|
value,
|
|
50
142
|
onChange,
|
|
51
143
|
onBlur,
|
|
52
144
|
nullable,
|
|
53
145
|
label,
|
|
54
146
|
description,
|
|
55
|
-
placeholder,
|
|
147
|
+
placeholder = "Qty",
|
|
56
148
|
error,
|
|
57
149
|
disabled,
|
|
58
150
|
required,
|
|
59
151
|
loading,
|
|
60
|
-
className
|
|
152
|
+
className,
|
|
153
|
+
min,
|
|
154
|
+
max,
|
|
155
|
+
positive,
|
|
156
|
+
nonZero
|
|
61
157
|
}, ref) => {
|
|
62
158
|
const fieldId = React__default.useId();
|
|
63
159
|
const [pickedUnitId, setPickedUnitId] = React__default.useState();
|
|
64
|
-
const [
|
|
160
|
+
const [parts, setParts] = React__default.useState({});
|
|
161
|
+
const [open, setOpen] = React__default.useState(false);
|
|
65
162
|
const shown = React__default.useRef(null);
|
|
66
|
-
const
|
|
67
|
-
const
|
|
68
|
-
const
|
|
163
|
+
const focusTarget = React__default.useRef(null);
|
|
164
|
+
const fieldBox = React__default.useRef(null);
|
|
165
|
+
const panel = React__default.useRef(null);
|
|
166
|
+
const rowInputs = React__default.useRef(/* @__PURE__ */ new Map());
|
|
167
|
+
React__default.useImperativeHandle(ref, () => focusTarget.current);
|
|
168
|
+
const primaryUnit = units.find((unit) => unit.id === primaryUnitId);
|
|
169
|
+
const anchorUnits = React__default.useMemo(
|
|
170
|
+
() => anchorUnitIds ? units.filter((unit) => anchorUnitIds.includes(unit.id)) : units,
|
|
171
|
+
[units, anchorUnitIds]
|
|
172
|
+
);
|
|
173
|
+
const anchor = anchorUnits.find((unit) => unit.id === (anchorUnitId ?? pickedUnitId)) ?? anchorUnits.find((unit) => unit.id === defaultUnitId) ?? anchorUnits.find((unit) => unit.id === primaryUnitId) ?? anchorUnits[0];
|
|
174
|
+
const singleUnit = units.length === 1 && !!primaryUnit;
|
|
175
|
+
const isDisabled = !!disabled || !!loading || !anchor;
|
|
69
176
|
const options = React__default.useMemo(
|
|
70
|
-
() =>
|
|
71
|
-
[
|
|
177
|
+
() => [...anchorUnits].sort((a, b) => unitSize(b).cmp(unitSize(a))).map((unit) => ({ value: unit.id, label: unit.symbol, description: unit.name })),
|
|
178
|
+
[anchorUnits]
|
|
72
179
|
);
|
|
180
|
+
const ladder = React__default.useMemo(() => entryUnits(units, primaryUnitId, anchor?.id), [units, primaryUnitId, anchor]);
|
|
73
181
|
React__default.useEffect(() => {
|
|
74
|
-
if (!
|
|
182
|
+
if (!primaryUnit || !anchor) return;
|
|
75
183
|
const qty = value ?? void 0;
|
|
76
|
-
if (shown.current && shown.current.qty === qty
|
|
77
|
-
shown.current = { qty
|
|
78
|
-
|
|
79
|
-
}, [value,
|
|
80
|
-
const
|
|
81
|
-
|
|
184
|
+
if (shown.current && shown.current.qty === qty) return;
|
|
185
|
+
shown.current = { qty };
|
|
186
|
+
setParts(partsOfQty(qty, units, anchor.id, primaryUnit.id, cascade));
|
|
187
|
+
}, [value, units, anchor, primaryUnit, cascade]);
|
|
188
|
+
const commit = (next) => {
|
|
189
|
+
setParts(next);
|
|
190
|
+
const qty = sumParts(next, units);
|
|
82
191
|
const previous = shown.current?.qty;
|
|
83
|
-
shown.current = { qty
|
|
192
|
+
shown.current = { qty };
|
|
84
193
|
if (qty !== previous) onChange?.(qty === void 0 && nullable ? null : qty);
|
|
85
194
|
};
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
195
|
+
const enter = (unit, text) => {
|
|
196
|
+
const pattern = takesDecimals(unit, primaryUnitId, allowDecimal) ? DECIMAL_PATTERN : WHOLE_PATTERN;
|
|
197
|
+
if (!pattern.test(text)) return;
|
|
198
|
+
commit({ ...parts, [unit.id]: text });
|
|
199
|
+
};
|
|
200
|
+
const handleAnchorChange = (next) => {
|
|
201
|
+
const unit = units.find((option) => option.id === next);
|
|
202
|
+
if (!unit) return;
|
|
203
|
+
setPickedUnitId(unit.id);
|
|
204
|
+
onAnchorUnitChange?.(unit.id);
|
|
205
|
+
};
|
|
206
|
+
const focusRow = (unitId) => {
|
|
207
|
+
const input = unitId ? rowInputs.current.get(unitId) : void 0;
|
|
208
|
+
if (!input) return;
|
|
209
|
+
input.focus();
|
|
210
|
+
input.setSelectionRange(input.value.length, input.value.length);
|
|
211
|
+
};
|
|
212
|
+
const handleInputBlur = (event) => {
|
|
213
|
+
const next = event.relatedTarget;
|
|
214
|
+
if (next && (fieldBox.current?.contains(next) || panel.current?.contains(next))) return;
|
|
215
|
+
onBlur?.();
|
|
216
|
+
};
|
|
217
|
+
const handleOpenChange = (next) => {
|
|
218
|
+
setOpen(next);
|
|
219
|
+
if (!next) onBlur?.();
|
|
90
220
|
};
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
if (
|
|
94
|
-
|
|
95
|
-
|
|
221
|
+
const handleReadingKeyDown = (event) => {
|
|
222
|
+
if (!anchor) return;
|
|
223
|
+
if (DIGIT_KEY.test(event.key)) {
|
|
224
|
+
event.preventDefault();
|
|
225
|
+
const typed = Object.values(parts).filter((amount) => amount !== "" && Number(amount) !== 0).length;
|
|
226
|
+
if (typed <= 1) commit({ [anchor.id]: event.key });
|
|
227
|
+
setOpen(true);
|
|
228
|
+
} else if (event.key === "ArrowDown") {
|
|
229
|
+
event.preventDefault();
|
|
230
|
+
setOpen(true);
|
|
231
|
+
}
|
|
96
232
|
};
|
|
97
|
-
const
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
233
|
+
const handleRowKeyDown = (event, index) => {
|
|
234
|
+
if (event.key !== "Enter" && event.key !== "ArrowDown" && event.key !== "ArrowUp") return;
|
|
235
|
+
event.preventDefault();
|
|
236
|
+
const next = ladder[index + (event.key === "ArrowUp" ? -1 : 1)];
|
|
237
|
+
if (next) focusRow(next.id);
|
|
238
|
+
else if (event.key === "Enter") handleOpenChange(false);
|
|
239
|
+
};
|
|
240
|
+
const total = sumParts(parts, units);
|
|
241
|
+
const pieces = total === void 0 || !anchor || !primaryUnit ? [] : breakDownQty(total, units, anchor.id, primaryUnit.id, cascade);
|
|
242
|
+
const shownPieces = pieces.filter((piece) => !piece.amount.isZero());
|
|
243
|
+
const reading = total === void 0 || !anchor ? null : shownPieces.length === 0 ? `0 ${anchor.symbol}` : shownPieces.map((piece) => `${formatAmount(piece.amount.toNumber())} ${piece.unit.symbol}`).join(compact ? " " : " + ");
|
|
244
|
+
const inAnchorAlone = !!anchor && (shownPieces.length === 0 || shownPieces.length === 1 && shownPieces[0].unit.id === anchor.id);
|
|
245
|
+
const typedOnly = !!anchor && Object.keys(parts).every((unitId) => unitId === anchor.id || !parts[unitId]);
|
|
246
|
+
const anchorText = !anchor ? "" : typedOnly ? parts[anchor.id] ?? "" : fromPrimaryQty(total, anchor);
|
|
247
|
+
const enterInAnchor = (text) => {
|
|
248
|
+
if (!anchor) return;
|
|
249
|
+
const pattern = takesDecimals(anchor, primaryUnitId, allowDecimal) ? DECIMAL_PATTERN : WHOLE_PATTERN;
|
|
250
|
+
if (!pattern.test(text)) return;
|
|
251
|
+
commit({ [anchor.id]: text });
|
|
252
|
+
};
|
|
253
|
+
const handleInputKeyDown = (event) => {
|
|
254
|
+
if (event.key !== "ArrowDown") return;
|
|
255
|
+
event.preventDefault();
|
|
256
|
+
setOpen(true);
|
|
257
|
+
};
|
|
258
|
+
const boundReading = (qty) => {
|
|
259
|
+
if (!anchor || !primaryUnit) return formatAmount(qty);
|
|
260
|
+
const boundPieces = breakDownQty(qty, units, anchor.id, primaryUnit.id, cascade).filter(
|
|
261
|
+
(piece) => !piece.amount.isZero()
|
|
262
|
+
);
|
|
263
|
+
if (boundPieces.length === 0) return formatAmount(0);
|
|
264
|
+
if (boundPieces.length === 1 && boundPieces[0].unit.id === anchor.id) {
|
|
265
|
+
return formatAmount(boundPieces[0].amount.toNumber());
|
|
266
|
+
}
|
|
267
|
+
return boundPieces.map((piece) => `${formatAmount(piece.amount.toNumber())} ${piece.unit.symbol}`).join(" + ");
|
|
268
|
+
};
|
|
269
|
+
const bound = boundWords(
|
|
270
|
+
min === void 0 ? void 0 : boundReading(min),
|
|
271
|
+
max === void 0 ? void 0 : boundReading(max)
|
|
272
|
+
);
|
|
273
|
+
const outOfBounds = isOutOfBounds(total, min, max);
|
|
274
|
+
const wholeOnly = anchor ? !takesDecimals(anchor, primaryUnitId, allowDecimal) : !allowDecimal;
|
|
275
|
+
const inputBound = (text) => bound ? /* @__PURE__ */ jsx(
|
|
276
|
+
BoundAfterValue,
|
|
277
|
+
{
|
|
278
|
+
text: text || placeholder,
|
|
279
|
+
bound,
|
|
280
|
+
outOfBounds,
|
|
281
|
+
className: cn("text-sm tabular-nums", compact ? "pl-2.5" : "pl-3")
|
|
282
|
+
}
|
|
283
|
+
) : null;
|
|
284
|
+
const message = error && anchor && primaryUnit ? resolveQuantityMessage(error, anchor, primaryUnit, formatAmount) : error;
|
|
285
|
+
const describedBy = [description && !message && `${fieldId}-description`, message && `${fieldId}-error`].filter(Boolean).join(" ") || void 0;
|
|
286
|
+
const boxClass = cn(
|
|
287
|
+
"relative flex h-9 w-full items-stretch rounded-md border border-input shadow-xs transition-[color,box-shadow] bg-input/30",
|
|
288
|
+
"focus-within:border-ring focus-within:ring-1 focus-within:ring-ring/50",
|
|
289
|
+
open && "border-ring ring-1 ring-ring/50",
|
|
290
|
+
isDisabled && "cursor-not-allowed opacity-50",
|
|
291
|
+
message && "border-destructive ring-destructive/20 dark:ring-destructive/40 focus-within:border-destructive focus-within:ring-destructive/20 dark:focus-within:ring-destructive/40",
|
|
292
|
+
className
|
|
293
|
+
);
|
|
104
294
|
return /* @__PURE__ */ jsxs(Field, { "data-disabled": isDisabled, children: [
|
|
105
|
-
label && /* @__PURE__ */ jsx(FieldLabel, { htmlFor: fieldId, children: label }),
|
|
295
|
+
label && /* @__PURE__ */ jsx(NumberFieldLabelRow, { integer: wholeOnly, positive, nonZero, children: /* @__PURE__ */ jsx(FieldLabel, { htmlFor: fieldId, children: label }) }),
|
|
106
296
|
/* @__PURE__ */ jsxs(FieldContent, { children: [
|
|
107
|
-
/* @__PURE__ */ jsxs(
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
297
|
+
singleUnit ? /* @__PURE__ */ jsxs("div", { className: boxClass, children: [
|
|
298
|
+
/* @__PURE__ */ jsx(
|
|
299
|
+
"input",
|
|
300
|
+
{
|
|
301
|
+
ref: (node) => {
|
|
302
|
+
focusTarget.current = node;
|
|
303
|
+
},
|
|
304
|
+
id: fieldId,
|
|
305
|
+
type: "text",
|
|
306
|
+
inputMode: allowDecimal ? "decimal" : "numeric",
|
|
307
|
+
autoComplete: "off",
|
|
308
|
+
value: parts[primaryUnit.id] ?? "",
|
|
309
|
+
onChange: (event) => enter(primaryUnit, event.target.value),
|
|
310
|
+
onBlur,
|
|
311
|
+
disabled: isDisabled,
|
|
312
|
+
required,
|
|
313
|
+
placeholder,
|
|
314
|
+
"aria-invalid": !!message,
|
|
315
|
+
"aria-describedby": describedBy,
|
|
316
|
+
className: cn(
|
|
317
|
+
"min-w-0 flex-1 bg-transparent py-1 text-sm tabular-nums outline-none placeholder:text-muted-foreground disabled:cursor-not-allowed",
|
|
318
|
+
compact ? "px-2.5" : "px-3"
|
|
319
|
+
)
|
|
320
|
+
}
|
|
321
|
+
),
|
|
322
|
+
inputBound(parts[primaryUnit.id] ?? ""),
|
|
323
|
+
/* @__PURE__ */ jsx(
|
|
324
|
+
"span",
|
|
325
|
+
{
|
|
326
|
+
className: cn(
|
|
327
|
+
"flex shrink-0 items-center rounded-r-md border-l border-input bg-muted/60 text-sm font-medium text-muted-foreground",
|
|
328
|
+
compact ? "px-2" : "px-3"
|
|
137
329
|
),
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
330
|
+
children: primaryUnit.symbol
|
|
331
|
+
}
|
|
332
|
+
)
|
|
333
|
+
] }) : /* @__PURE__ */ jsxs(Popover, { open, onOpenChange: handleOpenChange, children: [
|
|
334
|
+
/* @__PURE__ */ jsx(PopoverAnchor, { asChild: true, children: /* @__PURE__ */ jsxs("div", { ref: fieldBox, className: boxClass, children: [
|
|
335
|
+
inAnchorAlone && anchor ? /* @__PURE__ */ jsx(
|
|
336
|
+
"input",
|
|
337
|
+
{
|
|
338
|
+
ref: (node) => {
|
|
339
|
+
focusTarget.current = node;
|
|
340
|
+
},
|
|
341
|
+
id: fieldId,
|
|
342
|
+
type: "text",
|
|
343
|
+
inputMode: takesDecimals(anchor, primaryUnitId, allowDecimal) ? "decimal" : "numeric",
|
|
344
|
+
autoComplete: "off",
|
|
345
|
+
value: anchorText,
|
|
346
|
+
onChange: (event) => enterInAnchor(event.target.value),
|
|
347
|
+
onKeyDown: handleInputKeyDown,
|
|
348
|
+
onBlur: handleInputBlur,
|
|
349
|
+
disabled: isDisabled,
|
|
350
|
+
required,
|
|
351
|
+
placeholder,
|
|
352
|
+
"aria-invalid": !!message,
|
|
353
|
+
"aria-busy": loading || void 0,
|
|
354
|
+
"aria-describedby": describedBy,
|
|
355
|
+
className: cn(
|
|
356
|
+
"min-w-0 flex-1 rounded-l-md bg-transparent py-1 text-sm tabular-nums outline-none placeholder:text-muted-foreground disabled:cursor-not-allowed",
|
|
357
|
+
compact ? "px-2.5" : "px-3"
|
|
358
|
+
)
|
|
359
|
+
}
|
|
360
|
+
) : null,
|
|
361
|
+
inAnchorAlone && anchor ? inputBound(anchorText) : null,
|
|
362
|
+
inAnchorAlone && anchor ? /* @__PURE__ */ jsx(PopoverTrigger, { asChild: true, children: /* @__PURE__ */ jsx(
|
|
363
|
+
"button",
|
|
364
|
+
{
|
|
365
|
+
type: "button",
|
|
366
|
+
disabled: isDisabled,
|
|
367
|
+
"aria-label": "Enter the quantity by unit",
|
|
368
|
+
title: "Enter by unit (or arrow down)",
|
|
369
|
+
className: cn(
|
|
370
|
+
"flex shrink-0 items-center text-muted-foreground outline-none hover:text-foreground focus-visible:text-foreground disabled:cursor-not-allowed",
|
|
371
|
+
compact ? "px-1.5" : "px-2",
|
|
372
|
+
open && "text-foreground"
|
|
373
|
+
),
|
|
374
|
+
children: /* @__PURE__ */ jsx(Ruler, { className: "size-3.5" })
|
|
375
|
+
}
|
|
376
|
+
) }) : /* @__PURE__ */ jsx(PopoverTrigger, { asChild: true, children: /* @__PURE__ */ jsxs(
|
|
377
|
+
"button",
|
|
378
|
+
{
|
|
379
|
+
ref: (node) => {
|
|
380
|
+
focusTarget.current = node;
|
|
381
|
+
},
|
|
382
|
+
id: fieldId,
|
|
383
|
+
type: "button",
|
|
384
|
+
disabled: isDisabled,
|
|
385
|
+
title: reading ?? void 0,
|
|
386
|
+
onKeyDown: handleReadingKeyDown,
|
|
387
|
+
"aria-invalid": !!message,
|
|
388
|
+
"aria-busy": loading || void 0,
|
|
389
|
+
"aria-describedby": describedBy,
|
|
390
|
+
className: cn(
|
|
391
|
+
"flex min-w-0 flex-1 items-center rounded-l-md text-left text-sm tabular-nums outline-none disabled:cursor-not-allowed",
|
|
392
|
+
compact ? "px-2.5" : "px-3"
|
|
393
|
+
),
|
|
394
|
+
children: [
|
|
395
|
+
/* @__PURE__ */ jsx("span", { className: cn("min-w-0 truncate", !reading && "text-muted-foreground"), children: reading ?? placeholder }),
|
|
396
|
+
bound && /* @__PURE__ */ jsx(
|
|
397
|
+
"span",
|
|
150
398
|
{
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
children:
|
|
157
|
-
loading ? /* @__PURE__ */ jsx(Spinner, { className: "size-3.5 text-muted-foreground" }) : /* @__PURE__ */ jsx("span", { className: "truncate", children: selectedOption?.label ?? unit?.symbol ?? "—" }),
|
|
158
|
-
/* @__PURE__ */ jsx(
|
|
159
|
-
ChevronDown,
|
|
160
|
-
{
|
|
161
|
-
className: cn(
|
|
162
|
-
"size-3.5 shrink-0 text-muted-foreground transition-transform",
|
|
163
|
-
open && "rotate-180"
|
|
164
|
-
)
|
|
165
|
-
}
|
|
166
|
-
)
|
|
167
|
-
]
|
|
399
|
+
"aria-hidden": true,
|
|
400
|
+
className: cn(
|
|
401
|
+
"ml-1.5 shrink-0 text-xs text-muted-foreground",
|
|
402
|
+
outOfBounds && "text-destructive"
|
|
403
|
+
),
|
|
404
|
+
children: bound
|
|
168
405
|
}
|
|
169
406
|
)
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
407
|
+
]
|
|
408
|
+
}
|
|
409
|
+
) }),
|
|
410
|
+
/* @__PURE__ */ jsx("div", { className: "flex shrink-0 items-stretch border-l border-input", children: /* @__PURE__ */ jsx(
|
|
411
|
+
Select,
|
|
412
|
+
{
|
|
413
|
+
options,
|
|
414
|
+
value: anchor?.id ?? null,
|
|
415
|
+
onChange: handleAnchorChange,
|
|
416
|
+
disabled: isDisabled,
|
|
417
|
+
clearable: false,
|
|
418
|
+
label: "",
|
|
419
|
+
contentClassName: "min-w-40",
|
|
420
|
+
anchor: ({ selectedOption, open: unitsOpen, disabled: anchorDisabled }) => /* @__PURE__ */ jsxs(
|
|
421
|
+
"button",
|
|
422
|
+
{
|
|
423
|
+
type: "button",
|
|
424
|
+
disabled: anchorDisabled,
|
|
425
|
+
"aria-label": "Read the quantity in",
|
|
426
|
+
"aria-expanded": unitsOpen,
|
|
427
|
+
className: cn(
|
|
428
|
+
"flex h-full items-center gap-1 rounded-r-md text-sm font-medium text-foreground outline-none hover:bg-accent/50 focus-visible:bg-accent/50 disabled:cursor-not-allowed",
|
|
429
|
+
compact ? "px-2" : "px-3"
|
|
430
|
+
),
|
|
431
|
+
children: [
|
|
432
|
+
loading ? /* @__PURE__ */ jsx(Spinner, { className: "size-3.5 text-muted-foreground" }) : /* @__PURE__ */ jsx("span", { className: "truncate", children: selectedOption?.label ?? anchor?.symbol ?? "—" }),
|
|
433
|
+
/* @__PURE__ */ jsx(
|
|
434
|
+
ChevronDown,
|
|
435
|
+
{
|
|
436
|
+
className: cn(
|
|
437
|
+
"size-3.5 shrink-0 text-muted-foreground transition-transform",
|
|
438
|
+
unitsOpen && "rotate-180"
|
|
439
|
+
)
|
|
440
|
+
}
|
|
441
|
+
)
|
|
442
|
+
]
|
|
443
|
+
}
|
|
444
|
+
)
|
|
445
|
+
}
|
|
446
|
+
) })
|
|
447
|
+
] }) }),
|
|
448
|
+
/* @__PURE__ */ jsxs(
|
|
449
|
+
PopoverContent,
|
|
450
|
+
{
|
|
451
|
+
ref: panel,
|
|
452
|
+
align: "start",
|
|
453
|
+
className: "w-72 p-3",
|
|
454
|
+
"aria-label": "Quantity by unit",
|
|
455
|
+
onOpenAutoFocus: (event) => {
|
|
456
|
+
event.preventDefault();
|
|
457
|
+
focusRow(anchor?.id);
|
|
458
|
+
},
|
|
459
|
+
children: [
|
|
460
|
+
/* @__PURE__ */ jsx("p", { className: "mb-2 text-xs font-medium tracking-wider text-muted-foreground uppercase", children: "Quantity by unit" }),
|
|
461
|
+
/* @__PURE__ */ jsx("div", { className: "flex flex-col gap-1.5", children: ladder.map((unit, index) => {
|
|
462
|
+
const decimal = takesDecimals(unit, primaryUnitId, allowDecimal);
|
|
463
|
+
const size = unitSize(unit);
|
|
464
|
+
return /* @__PURE__ */ jsxs("label", { className: "flex items-center gap-2", children: [
|
|
465
|
+
/* @__PURE__ */ jsx("span", { className: "w-14 truncate text-sm font-medium", children: unit.symbol }),
|
|
466
|
+
/* @__PURE__ */ jsx(
|
|
467
|
+
"input",
|
|
468
|
+
{
|
|
469
|
+
ref: (node) => {
|
|
470
|
+
if (node) rowInputs.current.set(unit.id, node);
|
|
471
|
+
else rowInputs.current.delete(unit.id);
|
|
472
|
+
},
|
|
473
|
+
type: "text",
|
|
474
|
+
inputMode: decimal ? "decimal" : "numeric",
|
|
475
|
+
autoComplete: "off",
|
|
476
|
+
value: parts[unit.id] ?? "",
|
|
477
|
+
placeholder: "0",
|
|
478
|
+
onChange: (event) => enter(unit, event.target.value),
|
|
479
|
+
onKeyDown: (event) => handleRowKeyDown(event, index),
|
|
480
|
+
className: "h-8 min-w-0 flex-1 rounded-md border border-input px-2 text-sm tabular-nums shadow-xs outline-none focus-visible:border-ring focus-visible:ring-1 focus-visible:ring-ring/50 bg-input/30"
|
|
481
|
+
}
|
|
482
|
+
),
|
|
483
|
+
/* @__PURE__ */ jsx("span", { className: "w-20 shrink-0 truncate text-right text-xs text-muted-foreground tabular-nums", children: unit.id === primaryUnitId || !primaryUnit ? "" : `×${formatAmount(size.toNumber())} ${primaryUnit.symbol}` })
|
|
484
|
+
] }, unit.id);
|
|
485
|
+
}) }),
|
|
486
|
+
/* @__PURE__ */ jsxs("div", { className: "mt-3 flex items-center justify-between border-t pt-2.5", children: [
|
|
487
|
+
/* @__PURE__ */ jsxs("span", { className: "text-sm tabular-nums", children: [
|
|
488
|
+
/* @__PURE__ */ jsx("span", { className: "text-muted-foreground", children: "=" }),
|
|
489
|
+
" ",
|
|
490
|
+
/* @__PURE__ */ jsx("span", { className: "font-medium", children: total === void 0 || !primaryUnit ? "—" : `${formatAmount(total)} ${primaryUnit.symbol}` })
|
|
491
|
+
] }),
|
|
492
|
+
/* @__PURE__ */ jsxs("div", { className: "flex gap-1.5", children: [
|
|
493
|
+
/* @__PURE__ */ jsx(
|
|
494
|
+
Button,
|
|
495
|
+
{
|
|
496
|
+
type: "button",
|
|
497
|
+
variant: "ghost",
|
|
498
|
+
size: "xs",
|
|
499
|
+
onClick: () => {
|
|
500
|
+
commit({});
|
|
501
|
+
focusRow(anchor?.id);
|
|
502
|
+
},
|
|
503
|
+
children: "Clear"
|
|
504
|
+
}
|
|
505
|
+
),
|
|
506
|
+
/* @__PURE__ */ jsx(Button, { type: "button", size: "xs", onClick: () => handleOpenChange(false), children: "Done" })
|
|
507
|
+
] })
|
|
508
|
+
] })
|
|
509
|
+
]
|
|
510
|
+
}
|
|
511
|
+
)
|
|
180
512
|
] }),
|
|
181
513
|
description && !message && /* @__PURE__ */ jsx(FieldDescription, { id: `${fieldId}-description`, children: description }),
|
|
182
514
|
message && /* @__PURE__ */ jsx(FieldError, { id: `${fieldId}-error`, children: message })
|
|
@@ -186,5 +518,5 @@ const QuantityField = React__default.forwardRef(
|
|
|
186
518
|
);
|
|
187
519
|
QuantityField.displayName = "QuantityField";
|
|
188
520
|
|
|
189
|
-
export { PRIMARY_UNIT_PLACEHOLDER as P, QuantityField as Q, fromPrimaryQty as f, quantityPlaceholder as q, resolveQuantityMessage as r,
|
|
521
|
+
export { PRIMARY_UNIT_PLACEHOLDER as P, QuantityField as Q, toDisplayQuantity as a, breakDownQty as b, toPrimaryQty as c, entryUnits as e, fromPrimaryQty as f, partsOfQty as p, quantityPlaceholder as q, resolveQuantityMessage as r, sumParts as s, takesDecimals as t, unitSize as u };
|
|
190
522
|
//# sourceMappingURL=QuantityField.js.map
|