@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.
Files changed (200) hide show
  1. package/dist/Avatar.js +5 -5
  2. package/dist/Badge2.js +86 -11
  3. package/dist/Badge2.js.map +1 -1
  4. package/dist/ChartFrame.js +4 -1
  5. package/dist/ChartFrame.js.map +1 -1
  6. package/dist/Checkbox2.js +5 -5
  7. package/dist/CurrencyFieldFilter.js +60 -34
  8. package/dist/CurrencyFieldFilter.js.map +1 -1
  9. package/dist/Dialog.js +1 -1
  10. package/dist/DropdownMenu.js +6 -1
  11. package/dist/DropdownMenu.js.map +1 -1
  12. package/dist/IconSelect.js +2302 -2324
  13. package/dist/IconSelect.js.map +1 -1
  14. package/dist/Input.js +1 -1
  15. package/dist/Input.js.map +1 -1
  16. package/dist/Label.js +1 -1
  17. package/dist/OTPField.js +2 -2
  18. package/dist/OTPField.js.map +1 -1
  19. package/dist/PhoneField.js +2 -2
  20. package/dist/PhoneField.js.map +1 -1
  21. package/dist/Progress.js +2 -2
  22. package/dist/QuantityField.js +437 -105
  23. package/dist/QuantityField.js.map +1 -1
  24. package/dist/RadioGroup.js +7 -7
  25. package/dist/Select.js +92 -4
  26. package/dist/Select.js.map +1 -1
  27. package/dist/Sidebar.js +1 -1
  28. package/dist/SingleSelect.js +1 -1
  29. package/dist/SingleSelect.js.map +1 -1
  30. package/dist/Sonner.js +1 -1
  31. package/dist/Spinner.js +8 -2
  32. package/dist/Spinner.js.map +1 -1
  33. package/dist/Switch.js +5 -5
  34. package/dist/TabsSkeleton.js +2 -2
  35. package/dist/TextArea.js +1 -1
  36. package/dist/TextArea.js.map +1 -1
  37. package/dist/TextField.js +29 -27
  38. package/dist/TextField.js.map +1 -1
  39. package/dist/Toggle.js +1 -1
  40. package/dist/assets/quantum-ui.css +473 -171
  41. package/dist/bindFilters.js +14 -1
  42. package/dist/bindFilters.js.map +1 -1
  43. package/dist/components/ContextBar.js.map +1 -1
  44. package/dist/components/QuantityField.js +1 -1
  45. package/dist/components/RichTextEditor.js +1 -1
  46. package/dist/components/TreeView.js +4 -4
  47. package/dist/currencyCode.js +1520 -0
  48. package/dist/currencyCode.js.map +1 -0
  49. package/dist/dropdown-menu.js +9 -9
  50. package/dist/image-component.js +1 -1
  51. package/dist/index.js +2 -2
  52. package/dist/index10.js +37 -1499
  53. package/dist/index10.js.map +1 -1
  54. package/dist/index11.js +503 -28
  55. package/dist/index11.js.map +1 -1
  56. package/dist/index12.js +230 -454
  57. package/dist/index12.js.map +1 -1
  58. package/dist/index13.js +1073 -213
  59. package/dist/index13.js.map +1 -1
  60. package/dist/index14.js +20 -1139
  61. package/dist/index14.js.map +1 -1
  62. package/dist/index15.js +14 -20
  63. package/dist/index15.js.map +1 -1
  64. package/dist/index16.js +173 -19
  65. package/dist/index16.js.map +1 -1
  66. package/dist/index17.js +173 -168
  67. package/dist/index17.js.map +1 -1
  68. package/dist/index18.js +201 -171
  69. package/dist/index18.js.map +1 -1
  70. package/dist/index19.js +190 -186
  71. package/dist/index19.js.map +1 -1
  72. package/dist/index20.js +204 -202
  73. package/dist/index20.js.map +1 -1
  74. package/dist/index21.js +68 -209
  75. package/dist/index21.js.map +1 -1
  76. package/dist/index22.js +1128 -70
  77. package/dist/index22.js.map +1 -1
  78. package/dist/index23.js +4 -1133
  79. package/dist/index23.js.map +1 -1
  80. package/dist/index24.js +232 -4
  81. package/dist/index24.js.map +1 -1
  82. package/dist/index25.js +1100 -207
  83. package/dist/index25.js.map +1 -1
  84. package/dist/index26.js +2469 -1023
  85. package/dist/index26.js.map +1 -1
  86. package/dist/index27.js +35 -2569
  87. package/dist/index27.js.map +1 -1
  88. package/dist/index28.js +308 -36
  89. package/dist/index28.js.map +1 -1
  90. package/dist/index29.js +7 -307
  91. package/dist/index29.js.map +1 -1
  92. package/dist/index30.js +30 -8
  93. package/dist/index30.js.map +1 -1
  94. package/dist/index31.js +59 -29
  95. package/dist/index31.js.map +1 -1
  96. package/dist/index32.js +10 -59
  97. package/dist/index32.js.map +1 -1
  98. package/dist/index33.js +35347 -9
  99. package/dist/index33.js.map +1 -1
  100. package/dist/index34.js +72 -35342
  101. package/dist/index34.js.map +1 -1
  102. package/dist/index35.js +187 -72
  103. package/dist/index35.js.map +1 -1
  104. package/dist/index5.js +4 -4
  105. package/dist/index9.js +1 -1
  106. package/dist/lib/components/ContextBar/useContextState.d.ts +7 -1
  107. package/dist/lib/components/ContextBar/useContextState.d.ts.map +1 -1
  108. package/dist/lib/components/CurrencyField/CurrencyField.d.ts +8 -4
  109. package/dist/lib/components/CurrencyField/CurrencyField.d.ts.map +1 -1
  110. package/dist/lib/components/CurrencyField/CurrencyFieldFilter.d.ts +3 -2
  111. package/dist/lib/components/CurrencyField/CurrencyFieldFilter.d.ts.map +1 -1
  112. package/dist/lib/components/DataTable/DataTable.d.ts.map +1 -1
  113. package/dist/lib/components/DataTable/components/DataTablePagination.d.ts.map +1 -1
  114. package/dist/lib/components/DataTable/components/DataTableSelectionBar.d.ts +3 -2
  115. package/dist/lib/components/DataTable/components/DataTableSelectionBar.d.ts.map +1 -1
  116. package/dist/lib/components/DataTable/hooks/useDataTable.d.ts +4 -1
  117. package/dist/lib/components/DataTable/hooks/useDataTable.d.ts.map +1 -1
  118. package/dist/lib/components/DataTable/index.d.ts +1 -1
  119. package/dist/lib/components/DataTable/index.d.ts.map +1 -1
  120. package/dist/lib/components/DataTable/types.d.ts +13 -1
  121. package/dist/lib/components/DataTable/types.d.ts.map +1 -1
  122. package/dist/lib/components/DataTable/utils.d.ts +2 -2
  123. package/dist/lib/components/DataTable/utils.d.ts.map +1 -1
  124. package/dist/lib/components/DropdownMenu/DropdownMenu.d.ts.map +1 -1
  125. package/dist/lib/components/DropdownMenu/types.d.ts +2 -0
  126. package/dist/lib/components/DropdownMenu/types.d.ts.map +1 -1
  127. package/dist/lib/components/Field/numberConstraints.d.ts +19 -0
  128. package/dist/lib/components/Field/numberConstraints.d.ts.map +1 -0
  129. package/dist/lib/components/PhoneField/PhoneField.d.ts.map +1 -1
  130. package/dist/lib/components/QuantityField/QuantityField.d.ts +11 -1
  131. package/dist/lib/components/QuantityField/QuantityField.d.ts.map +1 -1
  132. package/dist/lib/components/QuantityField/index.d.ts +1 -1
  133. package/dist/lib/components/QuantityField/index.d.ts.map +1 -1
  134. package/dist/lib/components/QuantityField/quantity.d.ts +14 -0
  135. package/dist/lib/components/QuantityField/quantity.d.ts.map +1 -1
  136. package/dist/lib/components/Select/Select.d.ts +3 -0
  137. package/dist/lib/components/Select/Select.d.ts.map +1 -1
  138. package/dist/lib/components/Select/hooks/useSelect.d.ts +2 -0
  139. package/dist/lib/components/Select/hooks/useSelect.d.ts.map +1 -1
  140. package/dist/lib/components/TextField/TextField.d.ts.map +1 -1
  141. package/dist/lib/hooks/useWorkspaceCurrency.d.ts +2 -1
  142. package/dist/lib/hooks/useWorkspaceCurrency.d.ts.map +1 -1
  143. package/dist/lib/types/conditions.d.ts +2 -1
  144. package/dist/lib/types/conditions.d.ts.map +1 -1
  145. package/dist/lib/{components/DataTable/components → utils}/bindFilters.d.ts +2 -1
  146. package/dist/lib/utils/bindFilters.d.ts.map +1 -0
  147. package/dist/lib/utils/currency.d.ts +7 -5
  148. package/dist/lib/utils/currency.d.ts.map +1 -1
  149. package/dist/lib/utils/currencyCode.d.ts +14 -0
  150. package/dist/lib/utils/currencyCode.d.ts.map +1 -0
  151. package/dist/lib/utils/format.d.ts +7 -5
  152. package/dist/lib/utils/format.d.ts.map +1 -1
  153. package/dist/lib/utils/money.d.ts +5 -4
  154. package/dist/lib/utils/money.d.ts.map +1 -1
  155. package/dist/lib/utils/store.d.ts +3 -0
  156. package/dist/lib/utils/store.d.ts.map +1 -0
  157. package/dist/lib/utils/zod.d.ts +3 -1
  158. package/dist/lib/utils/zod.d.ts.map +1 -1
  159. package/dist/numberConstraints.js +68 -0
  160. package/dist/numberConstraints.js.map +1 -0
  161. package/dist/popover.js +5 -2
  162. package/dist/popover.js.map +1 -1
  163. package/dist/react.js +45 -0
  164. package/dist/react.js.map +1 -0
  165. package/dist/ruler.js +27 -0
  166. package/dist/ruler.js.map +1 -0
  167. package/dist/select2.js +2 -2
  168. package/dist/select2.js.map +1 -1
  169. package/dist/separator2.js +1 -1
  170. package/dist/shadcn/shadcnBadge/Badge.d.ts +5 -2
  171. package/dist/shadcn/shadcnBadge/Badge.d.ts.map +1 -1
  172. package/dist/shadcn/shadcnChart/Chart.d.ts.map +1 -1
  173. package/dist/shadcn/shadcnPopover/index.d.ts +1 -1
  174. package/dist/shadcn/shadcnPopover/index.d.ts.map +1 -1
  175. package/dist/shadcn/shadcnPopover/popover.d.ts +2 -1
  176. package/dist/shadcn/shadcnPopover/popover.d.ts.map +1 -1
  177. package/dist/tooltip2.js +1 -1
  178. package/dist/useContextState.js +39 -6
  179. package/dist/useContextState.js.map +1 -1
  180. package/dist/useDataTable.js +148 -64
  181. package/dist/useDataTable.js.map +1 -1
  182. package/dist/useWorkspaceCurrency.js.map +1 -1
  183. package/dist/utils/currency.js +3 -14
  184. package/dist/utils/currency.js.map +1 -1
  185. package/dist/utils/format.js +14 -6
  186. package/dist/utils/format.js.map +1 -1
  187. package/dist/utils/money.js +5 -3
  188. package/dist/utils/money.js.map +1 -1
  189. package/dist/utils/motion.js +2 -2
  190. package/dist/utils/store.js +67 -0
  191. package/dist/utils/store.js.map +1 -0
  192. package/dist/utils/zod.js +10 -7
  193. package/dist/utils/zod.js.map +1 -1
  194. package/dist/value.js +1 -1
  195. package/package.json +5 -1
  196. package/dist/Spinner2.js +0 -10
  197. package/dist/Spinner2.js.map +0 -1
  198. package/dist/index36.js +0 -199
  199. package/dist/index36.js.map +0 -1
  200. package/dist/lib/components/DataTable/components/bindFilters.d.ts.map +0 -1
@@ -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 { S as Spinner } from './Spinner2.js';
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 AMOUNT_PATTERN = /^-?\d*\.?\d*$/;
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 [text, setText] = React__default.useState("");
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 unit = units.find((option) => option.id === pickedUnitId) ?? units.find((option) => option.id === defaultUnitId) ?? units.find((option) => option.id === primaryUnitId);
67
- const primaryUnit = units.find((option) => option.id === primaryUnitId);
68
- const isDisabled = !!disabled || !!loading || !unit;
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
- () => units.map((option) => ({ value: option.id, label: option.symbol, description: option.name })),
71
- [units]
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 (!unit) return;
182
+ if (!primaryUnit || !anchor) return;
75
183
  const qty = value ?? void 0;
76
- if (shown.current && shown.current.qty === qty && shown.current.unitId === unit.id) return;
77
- shown.current = { qty, unitId: unit.id };
78
- setText(fromPrimaryQty(qty, unit));
79
- }, [value, unit]);
80
- const emit = (nextText, nextUnit) => {
81
- const qty = toPrimaryQty(nextText, nextUnit);
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, unitId: nextUnit.id };
192
+ shown.current = { qty };
84
193
  if (qty !== previous) onChange?.(qty === void 0 && nullable ? null : qty);
85
194
  };
86
- const handleTextChange = (event) => {
87
- if (!AMOUNT_PATTERN.test(event.target.value)) return;
88
- setText(event.target.value);
89
- if (unit) emit(event.target.value, unit);
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 handleUnitChange = (next) => {
92
- const nextUnit = units.find((option) => option.id === next);
93
- if (!nextUnit) return;
94
- setPickedUnitId(nextUnit.id);
95
- emit(text, nextUnit);
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 showHint = !!unit && !!primaryUnit && unit.id !== primaryUnit.id && value != null;
98
- const message = error && unit && primaryUnit ? resolveQuantityMessage(error, unit, primaryUnit, formatAmount) : error;
99
- const describedBy = [
100
- showHint && `${fieldId}-hint`,
101
- description && !message && `${fieldId}-description`,
102
- message && `${fieldId}-error`
103
- ].filter(Boolean).join(" ") || void 0;
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
- "div",
109
- {
110
- className: cn(
111
- "flex h-9 w-full items-stretch rounded-md border border-input bg-transparent shadow-xs transition-[color,box-shadow] dark:bg-input/30",
112
- "focus-within:border-ring focus-within:ring-1 focus-within:ring-ring/50",
113
- isDisabled && "cursor-not-allowed opacity-50",
114
- message && "border-destructive focus-within:ring-destructive/20 dark:focus-within:ring-destructive/40",
115
- className
116
- ),
117
- children: [
118
- /* @__PURE__ */ jsx(
119
- "input",
120
- {
121
- ref,
122
- id: fieldId,
123
- type: "text",
124
- inputMode: "decimal",
125
- autoComplete: "off",
126
- value: text,
127
- onChange: handleTextChange,
128
- onBlur,
129
- disabled: isDisabled,
130
- required,
131
- placeholder,
132
- "aria-invalid": !!message,
133
- "aria-busy": loading || void 0,
134
- "aria-describedby": describedBy,
135
- className: "min-w-0 flex-1 bg-transparent px-3 py-1 text-sm tabular-nums outline-none placeholder:text-muted-foreground disabled:cursor-not-allowed"
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
- /* @__PURE__ */ jsx("div", { className: "flex w-28 shrink-0 items-stretch border-l border-input", children: /* @__PURE__ */ jsx(
139
- Select,
140
- {
141
- options,
142
- value: unit?.id ?? null,
143
- onChange: handleUnitChange,
144
- disabled: isDisabled,
145
- clearable: false,
146
- label: "",
147
- contentClassName: "min-w-40",
148
- anchor: ({ selectedOption, open, disabled: anchorDisabled }) => /* @__PURE__ */ jsxs(
149
- "button",
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
- type: "button",
152
- disabled: anchorDisabled,
153
- "aria-label": "Unit",
154
- "aria-expanded": open,
155
- className: "flex h-full w-full items-center justify-between gap-1 rounded-r-md px-3 text-sm font-medium text-foreground outline-none hover:bg-accent/50 focus-visible:bg-accent/50 disabled:cursor-not-allowed",
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
- showHint && /* @__PURE__ */ jsxs("p", { id: `${fieldId}-hint`, "aria-live": "polite", className: "text-xs tabular-nums text-muted-foreground", children: [
176
- "= ",
177
- formatAmount(value),
178
- " ",
179
- primaryUnit.symbol
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, toPrimaryQty as t };
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