@eifi1/ui-kit 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +219 -0
- package/dist/components/account-settings.d.ts +71 -0
- package/dist/components/account-settings.js +159 -0
- package/dist/components/account-settings.js.map +1 -0
- package/dist/components/alert-banner.d.ts +20 -0
- package/dist/components/alert-banner.js +39 -0
- package/dist/components/alert-banner.js.map +1 -0
- package/dist/components/amount-input.d.ts +117 -0
- package/dist/components/amount-input.js +243 -0
- package/dist/components/amount-input.js.map +1 -0
- package/dist/components/calculator.d.ts +42 -0
- package/dist/components/calculator.js +161 -0
- package/dist/components/calculator.js.map +1 -0
- package/dist/components/chart.d.ts +58 -0
- package/dist/components/chart.js +158 -0
- package/dist/components/chart.js.map +1 -0
- package/dist/components/combobox-core.d.ts +77 -0
- package/dist/components/combobox-core.js +263 -0
- package/dist/components/combobox-core.js.map +1 -0
- package/dist/components/combobox.d.ts +141 -0
- package/dist/components/combobox.js +550 -0
- package/dist/components/combobox.js.map +1 -0
- package/dist/components/currency-select.d.ts +37 -0
- package/dist/components/currency-select.js +131 -0
- package/dist/components/currency-select.js.map +1 -0
- package/dist/components/data-table-filter-popover.d.ts +28 -0
- package/dist/components/data-table-filter-popover.js +201 -0
- package/dist/components/data-table-filter-popover.js.map +1 -0
- package/dist/components/data-table-filters.d.ts +5 -0
- package/dist/components/data-table-filters.js +118 -0
- package/dist/components/data-table-filters.js.map +1 -0
- package/dist/components/data-table-labels.d.ts +33 -0
- package/dist/components/data-table-labels.js +53 -0
- package/dist/components/data-table-labels.js.map +1 -0
- package/dist/components/data-table-pagination.d.ts +18 -0
- package/dist/components/data-table-pagination.js +92 -0
- package/dist/components/data-table-pagination.js.map +1 -0
- package/dist/components/data-table-sort.d.ts +23 -0
- package/dist/components/data-table-sort.js +55 -0
- package/dist/components/data-table-sort.js.map +1 -0
- package/dist/components/data-table.d.ts +5 -0
- package/dist/components/data-table.js +926 -0
- package/dist/components/data-table.js.map +1 -0
- package/dist/components/date-picker.d.ts +85 -0
- package/dist/components/date-picker.js +253 -0
- package/dist/components/date-picker.js.map +1 -0
- package/dist/components/dropdown.d.ts +98 -0
- package/dist/components/dropdown.js +122 -0
- package/dist/components/dropdown.js.map +1 -0
- package/dist/components/entity-combobox.d.ts +49 -0
- package/dist/components/entity-combobox.js +115 -0
- package/dist/components/entity-combobox.js.map +1 -0
- package/dist/components/file-dropzone.d.ts +18 -0
- package/dist/components/file-dropzone.js +101 -0
- package/dist/components/file-dropzone.js.map +1 -0
- package/dist/components/full-bleed-dialog.d.ts +57 -0
- package/dist/components/full-bleed-dialog.js +68 -0
- package/dist/components/full-bleed-dialog.js.map +1 -0
- package/dist/components/grouped-picker.d.ts +37 -0
- package/dist/components/grouped-picker.js +87 -0
- package/dist/components/grouped-picker.js.map +1 -0
- package/dist/components/hover-menu.d.ts +16 -0
- package/dist/components/hover-menu.js +113 -0
- package/dist/components/hover-menu.js.map +1 -0
- package/dist/components/mini-calendar.d.ts +33 -0
- package/dist/components/mini-calendar.js +133 -0
- package/dist/components/mini-calendar.js.map +1 -0
- package/dist/components/modal.d.ts +76 -0
- package/dist/components/modal.js +156 -0
- package/dist/components/modal.js.map +1 -0
- package/dist/components/multi-entity-combobox.d.ts +47 -0
- package/dist/components/multi-entity-combobox.js +116 -0
- package/dist/components/multi-entity-combobox.js.map +1 -0
- package/dist/components/multi-select.d.ts +39 -0
- package/dist/components/multi-select.js +132 -0
- package/dist/components/multi-select.js.map +1 -0
- package/dist/components/number-input.d.ts +74 -0
- package/dist/components/number-input.js +114 -0
- package/dist/components/number-input.js.map +1 -0
- package/dist/components/numpad-sheet.d.ts +45 -0
- package/dist/components/numpad-sheet.js +102 -0
- package/dist/components/numpad-sheet.js.map +1 -0
- package/dist/components/picker-sheet.d.ts +44 -0
- package/dist/components/picker-sheet.js +78 -0
- package/dist/components/picker-sheet.js.map +1 -0
- package/dist/components/popover.d.ts +20 -0
- package/dist/components/popover.js +46 -0
- package/dist/components/popover.js.map +1 -0
- package/dist/components/search-field.d.ts +52 -0
- package/dist/components/search-field.js +64 -0
- package/dist/components/search-field.js.map +1 -0
- package/dist/components/settings-fields.d.ts +39 -0
- package/dist/components/settings-fields.js +17 -0
- package/dist/components/settings-fields.js.map +1 -0
- package/dist/components/swipeable-row.d.ts +60 -0
- package/dist/components/swipeable-row.js +115 -0
- package/dist/components/swipeable-row.js.map +1 -0
- package/dist/components/toggle-group.d.ts +34 -0
- package/dist/components/toggle-group.js +90 -0
- package/dist/components/toggle-group.js.map +1 -0
- package/dist/components/tooltip.d.ts +86 -0
- package/dist/components/tooltip.js +171 -0
- package/dist/components/tooltip.js.map +1 -0
- package/dist/components/ui.d.ts +239 -0
- package/dist/components/ui.js +512 -0
- package/dist/components/ui.js.map +1 -0
- package/dist/components/use-mobile-reveal.d.ts +31 -0
- package/dist/components/use-mobile-reveal.js +33 -0
- package/dist/components/use-mobile-reveal.js.map +1 -0
- package/dist/components/use-table-state.d.ts +54 -0
- package/dist/components/use-table-state.js +153 -0
- package/dist/components/use-table-state.js.map +1 -0
- package/dist/components/user-avatar.d.ts +23 -0
- package/dist/components/user-avatar.js +33 -0
- package/dist/components/user-avatar.js.map +1 -0
- package/dist/components/wizard-stepper.d.ts +14 -0
- package/dist/components/wizard-stepper.js +27 -0
- package/dist/components/wizard-stepper.js.map +1 -0
- package/dist/data-table-filters-aXyf0Xub.d.ts +261 -0
- package/dist/feedback/feedback-attachment.d.ts +63 -0
- package/dist/feedback/feedback-attachment.js +160 -0
- package/dist/feedback/feedback-attachment.js.map +1 -0
- package/dist/feedback/feedback-dialog.d.ts +72 -0
- package/dist/feedback/feedback-dialog.js +109 -0
- package/dist/feedback/feedback-dialog.js.map +1 -0
- package/dist/feedback/feedback-inbox.d.ts +226 -0
- package/dist/feedback/feedback-inbox.js +315 -0
- package/dist/feedback/feedback-inbox.js.map +1 -0
- package/dist/hooks/use-anchored-panel.d.ts +80 -0
- package/dist/hooks/use-anchored-panel.js +71 -0
- package/dist/hooks/use-anchored-panel.js.map +1 -0
- package/dist/hooks/use-anchored-rect.d.ts +24 -0
- package/dist/hooks/use-anchored-rect.js +40 -0
- package/dist/hooks/use-anchored-rect.js.map +1 -0
- package/dist/hooks/use-body-scroll-lock.d.ts +9 -0
- package/dist/hooks/use-body-scroll-lock.js +41 -0
- package/dist/hooks/use-body-scroll-lock.js.map +1 -0
- package/dist/hooks/use-close-transition.d.ts +41 -0
- package/dist/hooks/use-close-transition.js +36 -0
- package/dist/hooks/use-close-transition.js.map +1 -0
- package/dist/hooks/use-dismiss.d.ts +34 -0
- package/dist/hooks/use-dismiss.js +41 -0
- package/dist/hooks/use-dismiss.js.map +1 -0
- package/dist/hooks/use-media-query.d.ts +8 -0
- package/dist/hooks/use-media-query.js +18 -0
- package/dist/hooks/use-media-query.js.map +1 -0
- package/dist/hooks/use-overlay-history.d.ts +10 -0
- package/dist/hooks/use-overlay-history.js +99 -0
- package/dist/hooks/use-overlay-history.js.map +1 -0
- package/dist/hooks/use-row-swipe.d.ts +30 -0
- package/dist/hooks/use-row-swipe.js +128 -0
- package/dist/hooks/use-row-swipe.js.map +1 -0
- package/dist/index.d.ts +75 -0
- package/dist/index.js +90 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/calc.d.ts +89 -0
- package/dist/lib/calc.js +180 -0
- package/dist/lib/calc.js.map +1 -0
- package/dist/lib/cn.d.ts +6 -0
- package/dist/lib/cn.js +7 -0
- package/dist/lib/cn.js.map +1 -0
- package/dist/lib/dates.d.ts +84 -0
- package/dist/lib/dates.js +108 -0
- package/dist/lib/dates.js.map +1 -0
- package/dist/lib/logger.d.ts +17 -0
- package/dist/lib/logger.js +30 -0
- package/dist/lib/logger.js.map +1 -0
- package/dist/lib/safe-storage.d.ts +23 -0
- package/dist/lib/safe-storage.js +20 -0
- package/dist/lib/safe-storage.js.map +1 -0
- package/dist/search/command-palette.d.ts +71 -0
- package/dist/search/command-palette.js +198 -0
- package/dist/search/command-palette.js.map +1 -0
- package/dist/shell/app-shell.d.ts +64 -0
- package/dist/shell/app-shell.js +237 -0
- package/dist/shell/app-shell.js.map +1 -0
- package/dist/shell/option-switcher-menu.d.ts +24 -0
- package/dist/shell/option-switcher-menu.js +44 -0
- package/dist/shell/option-switcher-menu.js.map +1 -0
- package/dist/shell/role-switcher.d.ts +19 -0
- package/dist/shell/role-switcher.js +28 -0
- package/dist/shell/role-switcher.js.map +1 -0
- package/dist/shell/top-bar.d.ts +15 -0
- package/dist/shell/top-bar.js +29 -0
- package/dist/shell/top-bar.js.map +1 -0
- package/dist/shell/topbar-action-menu.d.ts +44 -0
- package/dist/shell/topbar-action-menu.js +49 -0
- package/dist/shell/topbar-action-menu.js.map +1 -0
- package/dist/shell/topbar-controls.d.ts +54 -0
- package/dist/shell/topbar-controls.js +152 -0
- package/dist/shell/topbar-controls.js.map +1 -0
- package/dist/theme/chart-palette.d.ts +60 -0
- package/dist/theme/chart-palette.js +79 -0
- package/dist/theme/chart-palette.js.map +1 -0
- package/dist/theme/palette-presets.d.ts +39 -0
- package/dist/theme/palette-presets.js +440 -0
- package/dist/theme/palette-presets.js.map +1 -0
- package/dist/theme/palette-store.d.ts +45 -0
- package/dist/theme/palette-store.js +52 -0
- package/dist/theme/palette-store.js.map +1 -0
- package/dist/theme/theme-store.d.ts +57 -0
- package/dist/theme/theme-store.js +96 -0
- package/dist/theme/theme-store.js.map +1 -0
- package/dist/tour/tour.d.ts +104 -0
- package/dist/tour/tour.js +351 -0
- package/dist/tour/tour.js.map +1 -0
- package/dist/wizard/stepper-nav.d.ts +30 -0
- package/dist/wizard/stepper-nav.js +146 -0
- package/dist/wizard/stepper-nav.js.map +1 -0
- package/dist/wizard/types.d.ts +124 -0
- package/dist/wizard/types.js +24 -0
- package/dist/wizard/types.js.map +1 -0
- package/dist/wizard/use-wizard.d.ts +6 -0
- package/dist/wizard/use-wizard.js +222 -0
- package/dist/wizard/use-wizard.js.map +1 -0
- package/dist/wizard/validation.d.ts +23 -0
- package/dist/wizard/validation.js +13 -0
- package/dist/wizard/validation.js.map +1 -0
- package/dist/wizard/wizard-context.d.ts +19 -0
- package/dist/wizard/wizard-context.js +15 -0
- package/dist/wizard/wizard-context.js.map +1 -0
- package/dist/wizard/wizard-step.d.ts +11 -0
- package/dist/wizard/wizard-step.js +12 -0
- package/dist/wizard/wizard-step.js.map +1 -0
- package/dist/wizard/wizard-summary.d.ts +18 -0
- package/dist/wizard/wizard-summary.js +41 -0
- package/dist/wizard/wizard-summary.js.map +1 -0
- package/package.json +87 -0
- package/tokens.css +254 -0
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { forwardRef, useCallback, useId, useMemo, useRef, useState } from "react";
|
|
3
|
+
import { ChevronDown } from "lucide-react";
|
|
4
|
+
import { CURRENCIES, CurrencyFlag, getCurrency } from "./currency-select.js";
|
|
5
|
+
import { FIELD_BASE, FIELD_DISPLAY, FIELD_INVALID, FLOATING_INPUT_CLASS, FLOATING_LABEL_CLASS, PHONE_QUERY } from "./ui.js";
|
|
6
|
+
import { cn } from "../lib/cn.js";
|
|
7
|
+
import { useMediaQuery } from "../hooks/use-media-query.js";
|
|
8
|
+
import { CalculatorButton } from "./calculator.js";
|
|
9
|
+
import { NumberPadSheet } from "./numpad-sheet.js";
|
|
10
|
+
import { DropdownPanel, DropdownSearchHeader, useDropdownSearch } from "./dropdown.js";
|
|
11
|
+
import {
|
|
12
|
+
commitExpression,
|
|
13
|
+
evaluateExpression,
|
|
14
|
+
formatResult,
|
|
15
|
+
isBareAmount,
|
|
16
|
+
looksLikeExpression,
|
|
17
|
+
sanitizeLive,
|
|
18
|
+
splitLeadingSign
|
|
19
|
+
} from "../lib/calc.js";
|
|
20
|
+
const TONE_CLASS = {
|
|
21
|
+
neutral: "",
|
|
22
|
+
outflow: "text-money-neg",
|
|
23
|
+
inflow: "text-money-pos"
|
|
24
|
+
};
|
|
25
|
+
const DISPLAY_INPUT_CLASS = cn(
|
|
26
|
+
FIELD_DISPLAY,
|
|
27
|
+
"text-4xl font-semibold leading-tight tracking-tight tabular-nums"
|
|
28
|
+
);
|
|
29
|
+
function isResultOf(previous, text) {
|
|
30
|
+
const n = evaluateExpression(previous);
|
|
31
|
+
return n !== null && formatResult(n) === text.trim();
|
|
32
|
+
}
|
|
33
|
+
const AmountInput = forwardRef(
|
|
34
|
+
({ value, onChange, currency, onCurrencyChange, placeholder, label, disabled, invalid, className, id, ariaLabel, autoFocus, tone = "neutral", negative = false, onNegativeChange, variant = "field", align = "start", labels }, ref) => {
|
|
35
|
+
const generatedId = useId();
|
|
36
|
+
const fieldId = id ?? generatedId;
|
|
37
|
+
const editable = !!onCurrencyChange;
|
|
38
|
+
const isMobile = useMediaQuery(PHONE_QUERY, false);
|
|
39
|
+
const showCalc = !disabled && !isMobile;
|
|
40
|
+
const [focused, setFocused] = useState(false);
|
|
41
|
+
const showNumpad = isMobile && !disabled && focused;
|
|
42
|
+
const asDisplay = variant === "display" && isMobile;
|
|
43
|
+
const innerRef = useRef(null);
|
|
44
|
+
const setRefs = useCallback(
|
|
45
|
+
(el) => {
|
|
46
|
+
innerRef.current = el;
|
|
47
|
+
if (typeof ref === "function") ref(el);
|
|
48
|
+
else if (ref) ref.current = el;
|
|
49
|
+
},
|
|
50
|
+
[ref]
|
|
51
|
+
);
|
|
52
|
+
const signOwned = onNegativeChange !== void 0;
|
|
53
|
+
const shown = signOwned && negative && isBareAmount(value) ? `-${value}` : value;
|
|
54
|
+
const handleText = (raw, previous = shown) => {
|
|
55
|
+
const text = sanitizeLive(raw);
|
|
56
|
+
if (!signOwned) {
|
|
57
|
+
onChange(text);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const { sign, rest } = splitLeadingSign(text);
|
|
61
|
+
if (sign) {
|
|
62
|
+
onNegativeChange(sign === "-");
|
|
63
|
+
} else if (looksLikeExpression(previous) && isBareAmount(text) && isResultOf(previous, text)) {
|
|
64
|
+
onNegativeChange(false);
|
|
65
|
+
}
|
|
66
|
+
onChange(sign ? rest : text);
|
|
67
|
+
};
|
|
68
|
+
const commit = () => handleText(commitExpression(shown));
|
|
69
|
+
const { open, setOpen, wrapperRef, panelRef, query, setQuery, inputRef } = useDropdownSearch();
|
|
70
|
+
const chipRef = useRef(null);
|
|
71
|
+
const selected = getCurrency(currency);
|
|
72
|
+
const filtered = useMemo(() => {
|
|
73
|
+
const q = query.trim().toLowerCase();
|
|
74
|
+
if (!q) return CURRENCIES;
|
|
75
|
+
return CURRENCIES.filter(
|
|
76
|
+
(c) => c.code.toLowerCase().includes(q) || c.name.toLowerCase().includes(q) || c.symbol.toLowerCase().includes(q)
|
|
77
|
+
);
|
|
78
|
+
}, [query]);
|
|
79
|
+
return /* @__PURE__ */ jsxs("div", { ref: wrapperRef, className: cn("w-full", className), children: [
|
|
80
|
+
/* @__PURE__ */ jsxs("div", { className: "relative w-full", children: [
|
|
81
|
+
/* @__PURE__ */ jsx(
|
|
82
|
+
"input",
|
|
83
|
+
{
|
|
84
|
+
ref: setRefs,
|
|
85
|
+
id: fieldId,
|
|
86
|
+
"aria-label": ariaLabel,
|
|
87
|
+
type: "text",
|
|
88
|
+
autoFocus,
|
|
89
|
+
inputMode: isMobile ? "none" : "decimal",
|
|
90
|
+
autoComplete: "off",
|
|
91
|
+
disabled,
|
|
92
|
+
placeholder: asDisplay ? placeholder ?? "0" : label !== void 0 ? " " : placeholder,
|
|
93
|
+
value: shown,
|
|
94
|
+
onChange: (e) => handleText(e.target.value),
|
|
95
|
+
onFocus: () => setFocused(true),
|
|
96
|
+
onBlur: () => {
|
|
97
|
+
commit();
|
|
98
|
+
setFocused(false);
|
|
99
|
+
},
|
|
100
|
+
onKeyDown: (e) => {
|
|
101
|
+
if (e.key === "Enter") commit();
|
|
102
|
+
},
|
|
103
|
+
className: cn(
|
|
104
|
+
// With a label, use the floating-input padding (pt-4 pb-1) like every
|
|
105
|
+
// other labelled field so the value sits BELOW the floated label rather
|
|
106
|
+
// than vertically centred where it overlaps the resting label
|
|
107
|
+
// (feedback #314). FLOATING_INPUT_CLASS already bundles the peer +
|
|
108
|
+
// transparent-placeholder bits.
|
|
109
|
+
asDisplay ? DISPLAY_INPUT_CLASS : label !== void 0 ? FLOATING_INPUT_CLASS : FIELD_BASE,
|
|
110
|
+
// Room for the trailing controls. showCalc is always false on a phone,
|
|
111
|
+
// so the display shape only ever has to clear the currency chip — and
|
|
112
|
+
// it clears it by the chip's actual width (a text-sm code plus a
|
|
113
|
+
// chevron, ~58px) rather than the 5rem the boxed shape reserves. On a
|
|
114
|
+
// 375px screen those 16px are the difference between a readable figure
|
|
115
|
+
// and a clipped one (feedback #430 rework).
|
|
116
|
+
asDisplay ? currency ? editable ? "pr-16" : "pr-12" : "pr-0" : showCalc ? currency ? editable ? "pr-24" : "pr-16" : "pr-10" : currency ? editable ? "pr-20" : "pr-14" : "pr-3",
|
|
117
|
+
// Centred display shape: centre in what is LEFT of the currency chip.
|
|
118
|
+
// `text-center` alone would centre the figure in the whole box, i.e.
|
|
119
|
+
// partly underneath the chip; the padding above is what takes the chip
|
|
120
|
+
// out of the centring, and nothing is added on the left because a
|
|
121
|
+
// mirrored reservation would spend the width twice (see `align`).
|
|
122
|
+
asDisplay && align === "center" && "text-center",
|
|
123
|
+
// Last, so it wins over the base class's own text colour.
|
|
124
|
+
TONE_CLASS[tone],
|
|
125
|
+
invalid && FIELD_INVALID
|
|
126
|
+
),
|
|
127
|
+
"aria-invalid": invalid || void 0
|
|
128
|
+
}
|
|
129
|
+
),
|
|
130
|
+
label !== void 0 && // The display shape drops the label VISUALLY, not from the accessibility
|
|
131
|
+
// tree: a 36px tinted figure at the top of a transaction form is legibly
|
|
132
|
+
// the amount, but a screen reader still needs the name, and callers that
|
|
133
|
+
// pass `label` without `ariaLabel` would otherwise be left with none.
|
|
134
|
+
/* @__PURE__ */ jsx("label", { htmlFor: fieldId, className: asDisplay ? "sr-only" : FLOATING_LABEL_CLASS, children: label }),
|
|
135
|
+
/* @__PURE__ */ jsxs(
|
|
136
|
+
"div",
|
|
137
|
+
{
|
|
138
|
+
className: cn(
|
|
139
|
+
"absolute flex items-center gap-0.5",
|
|
140
|
+
asDisplay ? "bottom-1 right-0" : "inset-y-1 right-1"
|
|
141
|
+
),
|
|
142
|
+
children: [
|
|
143
|
+
showCalc && /* @__PURE__ */ jsx(
|
|
144
|
+
CalculatorButton,
|
|
145
|
+
{
|
|
146
|
+
value: shown,
|
|
147
|
+
onChange: handleText,
|
|
148
|
+
className: "px-1.5",
|
|
149
|
+
ariaLabel: labels?.calculatorTrigger,
|
|
150
|
+
labels: labels?.calculator
|
|
151
|
+
}
|
|
152
|
+
),
|
|
153
|
+
currency && !editable && /* @__PURE__ */ jsx(
|
|
154
|
+
"span",
|
|
155
|
+
{
|
|
156
|
+
"aria-hidden": true,
|
|
157
|
+
className: cn(
|
|
158
|
+
"pointer-events-none px-1 font-medium text-slate-500 dark:text-slate-400",
|
|
159
|
+
// A text-xs chip next to a 36px figure reads as a stray footnote.
|
|
160
|
+
asDisplay ? "text-sm" : "text-xs"
|
|
161
|
+
),
|
|
162
|
+
children: currency
|
|
163
|
+
}
|
|
164
|
+
),
|
|
165
|
+
currency && editable && /* @__PURE__ */ jsxs(
|
|
166
|
+
"button",
|
|
167
|
+
{
|
|
168
|
+
ref: chipRef,
|
|
169
|
+
type: "button",
|
|
170
|
+
onClick: () => setOpen((v) => !v),
|
|
171
|
+
"aria-label": selected ? `${labels?.currency ?? "Currency"}: ${selected.code}` : labels?.currency ?? "Currency",
|
|
172
|
+
className: cn(
|
|
173
|
+
"flex items-center gap-1 rounded px-2 py-1 font-medium text-slate-600 hover:bg-slate-100 dark:text-slate-300 dark:hover:bg-slate-800",
|
|
174
|
+
asDisplay ? "text-sm" : "text-xs"
|
|
175
|
+
),
|
|
176
|
+
children: [
|
|
177
|
+
currency,
|
|
178
|
+
/* @__PURE__ */ jsx(ChevronDown, { className: "size-3" })
|
|
179
|
+
]
|
|
180
|
+
}
|
|
181
|
+
)
|
|
182
|
+
]
|
|
183
|
+
}
|
|
184
|
+
),
|
|
185
|
+
editable && open && /* @__PURE__ */ jsx(
|
|
186
|
+
DropdownPanel,
|
|
187
|
+
{
|
|
188
|
+
anchorRef: chipRef,
|
|
189
|
+
panelRef,
|
|
190
|
+
empty: filtered.length === 0,
|
|
191
|
+
header: /* @__PURE__ */ jsx(
|
|
192
|
+
DropdownSearchHeader,
|
|
193
|
+
{
|
|
194
|
+
query,
|
|
195
|
+
onQueryChange: setQuery,
|
|
196
|
+
inputRef,
|
|
197
|
+
placeholder: labels?.currencySearch ?? "Search currency"
|
|
198
|
+
}
|
|
199
|
+
),
|
|
200
|
+
children: filtered.map((c) => {
|
|
201
|
+
const active = c.code === selected?.code;
|
|
202
|
+
return /* @__PURE__ */ jsx("li", { children: /* @__PURE__ */ jsxs(
|
|
203
|
+
"button",
|
|
204
|
+
{
|
|
205
|
+
type: "button",
|
|
206
|
+
onClick: () => {
|
|
207
|
+
onCurrencyChange?.(c.code);
|
|
208
|
+
setOpen(false);
|
|
209
|
+
},
|
|
210
|
+
className: cn(
|
|
211
|
+
"flex w-full items-center gap-2 px-3 py-1.5 text-sm text-left hover:bg-slate-50 dark:hover:bg-slate-800",
|
|
212
|
+
active && "bg-slate-100 dark:bg-slate-800"
|
|
213
|
+
),
|
|
214
|
+
children: [
|
|
215
|
+
/* @__PURE__ */ jsx(CurrencyFlag, { country: c.country }),
|
|
216
|
+
/* @__PURE__ */ jsx("span", { className: "font-mono text-slate-500 dark:text-slate-400 w-8 shrink-0 text-xs", children: c.symbol }),
|
|
217
|
+
/* @__PURE__ */ jsx("span", { className: "font-medium text-slate-900 dark:text-slate-100", children: c.code }),
|
|
218
|
+
/* @__PURE__ */ jsx("span", { className: "text-slate-500 dark:text-slate-400 truncate text-xs", children: c.name })
|
|
219
|
+
]
|
|
220
|
+
}
|
|
221
|
+
) }, c.code);
|
|
222
|
+
})
|
|
223
|
+
}
|
|
224
|
+
)
|
|
225
|
+
] }),
|
|
226
|
+
showNumpad && /* @__PURE__ */ jsx(
|
|
227
|
+
NumberPadSheet,
|
|
228
|
+
{
|
|
229
|
+
value: shown,
|
|
230
|
+
onChange: handleText,
|
|
231
|
+
onDone: () => innerRef.current?.blur(),
|
|
232
|
+
label,
|
|
233
|
+
labels: labels?.pad
|
|
234
|
+
}
|
|
235
|
+
)
|
|
236
|
+
] });
|
|
237
|
+
}
|
|
238
|
+
);
|
|
239
|
+
AmountInput.displayName = "AmountInput";
|
|
240
|
+
export {
|
|
241
|
+
AmountInput
|
|
242
|
+
};
|
|
243
|
+
//# sourceMappingURL=amount-input.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/components/amount-input.tsx"],"sourcesContent":["import { forwardRef, useCallback, useId, useMemo, useRef, useState } from \"react\";\nimport { ChevronDown } from \"lucide-react\";\nimport type { ReactNode } from \"react\";\nimport { CURRENCIES, CurrencyFlag, getCurrency } from \"./currency-select\";\nimport { FIELD_BASE, FIELD_DISPLAY, FIELD_INVALID, FLOATING_INPUT_CLASS, FLOATING_LABEL_CLASS, PHONE_QUERY } from \"./ui\";\nimport { cn } from \"../lib/cn\";\nimport { useMediaQuery } from \"../hooks/use-media-query\";\nimport { CalculatorButton, type CalculatorButtonLabels } from \"./calculator\";\nimport { NumberPadSheet, type NumberPadSheetLabels } from \"./numpad-sheet\";\nimport { DropdownPanel, DropdownSearchHeader, useDropdownSearch } from \"./dropdown\";\nimport {\n commitExpression,\n evaluateExpression,\n formatResult,\n isBareAmount,\n looksLikeExpression,\n sanitizeLive,\n splitLeadingSign,\n} from \"../lib/calc\";\n\ninterface AmountInputProps {\n value: string;\n onChange: (value: string) => void;\n currency?: string;\n onCurrencyChange?: (code: string) => void;\n placeholder?: string;\n label?: ReactNode;\n disabled?: boolean;\n /** Required and unanswered — {@link FIELD_INVALID}. See {@link Input}'s `invalid`. */\n invalid?: boolean;\n className?: string;\n id?: string;\n ariaLabel?: string;\n /** Every user-facing string this component and the two it composes own, so a\n * translating host can supply its own. The package carries no translation\n * catalog (see the README) — English defaults, each key optional, so the other\n * consumer apps are unaffected. `currency` names the picker chip and is\n * suffixed with the selected code; the rest are pass-through. */\n labels?: {\n currency?: string;\n currencySearch?: string;\n /** Names the calculator TRIGGER this component renders. `calculator` below\n * names the controls inside the popover it opens. */\n calculatorTrigger?: string;\n pad?: NumberPadSheetLabels;\n calculator?: CalculatorButtonLabels;\n };\n /** Focus the field on mount — on mobile this also opens the numpad sheet, so a\n * new-transaction form can jump straight to amount entry (feedback #70). */\n autoFocus?: boolean;\n /**\n * Tint the typed figure by what it will DO (Keksdose feedback #167).\n *\n * The direction is chosen with a toggle beside the field rather than typed, so\n * the digits alone say nothing about which way the money goes — on the one screen\n * where getting that backwards is the expensive mistake. Colour says it\n * continuously, and it costs no layout. It stays the MAIN indication (live #201\n * rework: *\"The main indication shall still be the color\"*); {@link negative}\n * renders the sign as a second, quieter signal in front of the figure.\n *\n * Never the ONLY signal: the toggle it mirrors is right there, labelled, so this\n * is reinforcement rather than the sole carrier of the meaning.\n */\n tone?: \"neutral\" | \"outflow\" | \"inflow\";\n /**\n * The SIGN of the figure, owned by the caller's direction control (Keksdose live\n * #201 rework: *\"Outflow should be negative and inflow positive … the sign shall\n * be displayed but not necessarily be typed in\"*).\n *\n * `value` stays the MAGNITUDE. The minus is glued on at render time and stripped\n * off again before anything is reported back, so the caller never sees its own\n * prefix echoed and no code path can end up applying it twice. That is the whole\n * cure: there is exactly ONE place the sign lives, and it is not the text.\n *\n * Because it is rendered rather than stored it cannot be deleted (the next render\n * puts it back), a select-all-and-retype keeps the direction, and the caret needs\n * no minding.\n */\n negative?: boolean;\n /**\n * A sign the user TYPED, or a calculation that resolved to a signed number.\n *\n * SET, never flip. A \"-\" means *\"this is an outflow\"*, so typing it twice says the\n * same thing twice — which is exactly what the rework asks for (*\"Subtracting\n * values twice resulting in inflow -> outflow and back to inflow seems not\n * reasonable\"*). \"+\" says the other direction.\n *\n * Its presence is what ARMS the whole sign behaviour. A field with no direction\n * control — an account's starting balance, a loan payment, a holding's cost —\n * passes neither prop and keeps a text that is the whole truth, minus included.\n */\n onNegativeChange?: (negative: boolean) => void;\n /**\n * How the field presents itself ON A PHONE (Keksdose feedback #176).\n *\n * `\"field\"` (default) is the labelled, bordered input used everywhere else.\n *\n * `\"display\"` keeps every mechanic — the same `<input>`, the same numpad sheet,\n * the same currency picker, the same commit/blur contract — and only drops the\n * chrome: no border, no fill, no floating label, the figure set at display size.\n * On a form whose whole point is one number, a bordered `text-sm` box draws the\n * amount at the same weight as the memo beside it; this makes it the headline\n * instead. It stays fully editable — the underline and the caret are what say so.\n *\n * Phone-only by construction: it keys off the same breakpoint as the numpad, so\n * from `md` up the field is byte-for-byte the normal one and a desktop grid keeps\n * its column rhythm.\n */\n variant?: \"field\" | \"display\";\n /**\n * Where the figure sits in its row, for `variant=\"display\"` only (Keksdose\n * feedback #176 rework).\n *\n * `\"start\"` (default) keeps it flush left, in the reading column the fields\n * below it share — and it is what the entry forms use, because a figure that\n * starts at a known edge is legible at any width: when the value outgrows the\n * field, the digits that scroll out of sight are the trailing ones, not the\n * magnitude (Keksdose feedback #430 rework).\n *\n * `\"center\"` makes it a centred headline. It centres in the width that REMAINS\n * after the trailing currency control, not in the whole box: the control is\n * pinned to the right edge, so a plain `text-center` would centre the figure\n * under it and the last digits would sit behind the chip. This used to mirror\n * the control's reservation onto the left instead, which centred correctly but\n * spent twice the control's width on padding — fatal on a 375px screen, where\n * it left a 36px figure 16px to live in.\n */\n align?: \"start\" | \"center\";\n}\n\n// The consuming app's ONE money palette (`--money-expense` / `--money-income`),\n// not a bespoke rose/emerald pairing: a figure being typed has to wear the same\n// colour the same figure will wear once it is a row in the table behind the form\n// (Keksdose dev#434). The utilities are unlayered and theme-aware, so no `dark:`\n// variant is needed and they win over the field's own text colour regardless of\n// where `cn` puts them.\nconst TONE_CLASS: Record<\"neutral\" | \"outflow\" | \"inflow\", string> = {\n neutral: \"\",\n outflow: \"text-money-neg\",\n inflow: \"text-money-pos\",\n};\n\n// The shared chrome-less treatment at money size. `tabular-nums` so digits don't\n// reflow the figure as they are typed; FIELD_DISPLAY's own muted placeholder\n// colour is what keeps an EMPTY form from painting its zero in the direction tint\n// (`tone` is computed from 0, which reads as an inflow).\nconst DISPLAY_INPUT_CLASS = cn(\n FIELD_DISPLAY,\n \"text-4xl font-semibold leading-tight tracking-tight tabular-nums\",\n);\n\n/**\n * Is `text` the ANSWER to `previous`, or something typed OVER it?\n *\n * \"The old text was a sum and the new one is bare\" cannot tell those apart, and both\n * gestures are ordinary: select-all-and-retype after a typo, or backspacing \"-20+\"\n * back down to a fresh figure. Read as an answer, they move the direction chip — so\n * an outflow of 7 quietly booked +7.00 (live #201 rework).\n *\n * The question that does separate them is arithmetic, not shape: evaluate what the\n * text replaced and see whether this IS that number. \"-12+30\" → \"18\" still is, so a\n * sum that comes out positive still takes the chip with it; \"-20+50\" → \"7\" is not,\n * and \"-20+\" evaluates to nothing at all.\n */\nfunction isResultOf(previous: string, text: string): boolean {\n const n = evaluateExpression(previous);\n return n !== null && formatResult(n) === text.trim();\n}\n\nexport const AmountInput = forwardRef<HTMLInputElement, AmountInputProps>(\n ({ value, onChange, currency, onCurrencyChange, placeholder, label, disabled, invalid, className, id, ariaLabel, autoFocus, tone = \"neutral\", negative = false, onNegativeChange, variant = \"field\", align = \"start\", labels }, ref) => {\n const generatedId = useId();\n const fieldId = id ?? generatedId;\n const editable = !!onCurrencyChange;\n // On phones we suppress the OS keyboard (inputMode=\"none\" below) and show our\n // own calculator numpad, so the desktop popover trigger is hidden. The\n // right-padding keys off showCalc, so it tightens up automatically.\n const isMobile = useMediaQuery(PHONE_QUERY, false);\n const showCalc = !disabled && !isMobile;\n const [focused, setFocused] = useState(false);\n // On mobile, focusing the field opens the numpad bottom sheet in place of the\n // native keyboard (feedback #334) — a full calculator keypad, not just the old\n // operator bar. An internal ref lets the sheet's \"Done\" blur the input, which\n // commits + closes via the existing onBlur handler.\n const showNumpad = isMobile && !disabled && focused;\n // Gate the display treatment on the SAME `isMobile` the numpad uses rather than\n // on `max-md:` classes, so the two can never drift apart on the breakpoint —\n // and so the two shapes are separate class strings instead of one string\n // fighting itself through responsive overrides. `useMediaQuery` reads\n // synchronously on mount, so there is no field→figure flash.\n const asDisplay = variant === \"display\" && isMobile;\n const innerRef = useRef<HTMLInputElement>(null);\n const setRefs = useCallback(\n (el: HTMLInputElement | null) => {\n innerRef.current = el;\n if (typeof ref === \"function\") ref(el);\n else if (ref) ref.current = el;\n },\n [ref],\n );\n // The sign is DISPLAYED, not stored (live #201 rework). `value` is the\n // magnitude; the minus is glued on here and taken off again in handleText, so\n // the two can never disagree and the caller can never double-apply it. Only in\n // front of a bare amount: an expression being typed (\"12-30\") owns its own\n // minus and has to survive keystroke-for-keystroke.\n const signOwned = onNegativeChange !== undefined;\n const shown = signOwned && negative && isBareAmount(value) ? `-${value}` : value;\n // Every route into the field — typing, the numpad sheet, the desktop\n // calculator, the blur/Enter commit — funnels through here, so the split is\n // written once and the four entry paths cannot drift.\n //\n // `previous` is the text this one REPLACES, and it is what says whether a bare\n // figure arriving here is an answer or a magnitude. For the three paths that\n // edit the field's own text that is simply what the field was showing; the\n // desktop calculator keeps its expression in a popover the field never sees,\n // so it passes it in (see `CalculatorButton`'s `onChange`).\n const handleText = (raw: string, previous: string = shown) => {\n const text = sanitizeLive(raw);\n if (!signOwned) {\n onChange(text);\n return;\n }\n const { sign, rest } = splitLeadingSign(text);\n if (sign) {\n // SET, not flip: re-reporting the same sign on every keystroke is a no-op.\n onNegativeChange(sign === \"-\");\n } else if (looksLikeExpression(previous) && isBareAmount(text) && isResultOf(previous, text)) {\n // A calculation that just RESOLVED states its sign in both directions. The\n // figure it ran on is the SIGNED one the user could see — \"-12+30\" is +18 —\n // so a positive result has to move the chip too, or the field would answer\n // that sum with \"-18\" and contradict the arithmetic it just showed.\n //\n // Only when it really resolved, though: see {@link isResultOf}. A bare figure\n // arriving on top of an expression is far more often a replacement than an\n // answer, and reading it as an answer flips the direction of money.\n onNegativeChange(false);\n }\n onChange(sign ? rest : text);\n };\n const commit = () => handleText(commitExpression(shown));\n const { open, setOpen, wrapperRef, panelRef, query, setQuery, inputRef } = useDropdownSearch();\n // The chip the currency list hangs off (Keksdose dev#548). It is portalled now, so\n // the panel needs a real trigger rect rather than a relative parent — see\n // `DropdownPanel`'s note for what an `overflow` ancestor did to it before.\n const chipRef = useRef<HTMLButtonElement>(null);\n\n const selected = getCurrency(currency);\n const filtered = useMemo(() => {\n const q = query.trim().toLowerCase();\n if (!q) return CURRENCIES;\n return CURRENCIES.filter(\n (c) =>\n c.code.toLowerCase().includes(q) ||\n c.name.toLowerCase().includes(q) ||\n c.symbol.toLowerCase().includes(q),\n );\n }, [query]);\n\n return (\n <div ref={wrapperRef} className={cn(\"w-full\", className)}>\n {/* Inner relative box holds the input + its absolutely-positioned\n trailing controls (calculator / currency), so those stay anchored to\n the INPUT even when the mobile math bar is rendered below (#334). */}\n <div className=\"relative w-full\">\n <input\n ref={setRefs}\n id={fieldId}\n aria-label={ariaLabel}\n type=\"text\"\n // eslint-disable-next-line jsx-a11y/no-autofocus -- opt-in per call site (feedback #70)\n autoFocus={autoFocus}\n // On mobile suppress the OS keyboard so our numpad sheet owns entry; the\n // field keeps focus/caret. Desktop keeps the native decimal keypad.\n inputMode={isMobile ? \"none\" : \"decimal\"}\n autoComplete=\"off\"\n disabled={disabled}\n // The floating-label pattern needs a blank placeholder for its\n // peer-placeholder-shown trick. The display shape has no floating label,\n // so it spends the placeholder on what an empty borderless form actually\n // needs: a zero to aim at, at full size. The caller supplies it because\n // only the app knows the locale's decimal separator.\n placeholder={asDisplay ? (placeholder ?? \"0\") : label !== undefined ? \" \" : placeholder}\n value={shown}\n onChange={(e) => handleText(e.target.value)}\n onFocus={() => setFocused(true)}\n onBlur={() => {\n commit();\n setFocused(false);\n }}\n onKeyDown={(e) => {\n if (e.key === \"Enter\") commit();\n }}\n className={cn(\n // With a label, use the floating-input padding (pt-4 pb-1) like every\n // other labelled field so the value sits BELOW the floated label rather\n // than vertically centred where it overlaps the resting label\n // (feedback #314). FLOATING_INPUT_CLASS already bundles the peer +\n // transparent-placeholder bits.\n asDisplay ? DISPLAY_INPUT_CLASS : label !== undefined ? FLOATING_INPUT_CLASS : FIELD_BASE,\n // Room for the trailing controls. showCalc is always false on a phone,\n // so the display shape only ever has to clear the currency chip — and\n // it clears it by the chip's actual width (a text-sm code plus a\n // chevron, ~58px) rather than the 5rem the boxed shape reserves. On a\n // 375px screen those 16px are the difference between a readable figure\n // and a clipped one (feedback #430 rework).\n asDisplay\n ? currency ? (editable ? \"pr-16\" : \"pr-12\") : \"pr-0\"\n : showCalc\n ? currency ? (editable ? \"pr-24\" : \"pr-16\") : \"pr-10\"\n : currency ? (editable ? \"pr-20\" : \"pr-14\") : \"pr-3\",\n // Centred display shape: centre in what is LEFT of the currency chip.\n // `text-center` alone would centre the figure in the whole box, i.e.\n // partly underneath the chip; the padding above is what takes the chip\n // out of the centring, and nothing is added on the left because a\n // mirrored reservation would spend the width twice (see `align`).\n asDisplay && align === \"center\" && \"text-center\",\n // Last, so it wins over the base class's own text colour.\n TONE_CLASS[tone],\n invalid && FIELD_INVALID,\n )}\n aria-invalid={invalid || undefined}\n />\n {label !== undefined && (\n // The display shape drops the label VISUALLY, not from the accessibility\n // tree: a 36px tinted figure at the top of a transaction form is legibly\n // the amount, but a screen reader still needs the name, and callers that\n // pass `label` without `ariaLabel` would otherwise be left with none.\n <label htmlFor={fieldId} className={asDisplay ? \"sr-only\" : FLOATING_LABEL_CLASS}>\n {label}\n </label>\n )}\n {/* Trailing controls share one flex track so the calculator icon and the\n currency suffix/picker sit side by side without overlapping. */}\n {/* The display shape has no box to inset from, so the controls align to the\n figure's own right edge and its baseline strip (`bottom-1`, matching the\n input's pb-1) instead of floating inside a field. */}\n <div\n className={cn(\n \"absolute flex items-center gap-0.5\",\n asDisplay ? \"bottom-1 right-0\" : \"inset-y-1 right-1\",\n )}\n >\n {showCalc && (\n <CalculatorButton\n value={shown}\n onChange={handleText}\n className=\"px-1.5\"\n ariaLabel={labels?.calculatorTrigger}\n labels={labels?.calculator}\n />\n )}\n {currency && !editable && (\n <span\n aria-hidden\n className={cn(\n \"pointer-events-none px-1 font-medium text-slate-500 dark:text-slate-400\",\n // A text-xs chip next to a 36px figure reads as a stray footnote.\n asDisplay ? \"text-sm\" : \"text-xs\",\n )}\n >\n {currency}\n </span>\n )}\n {currency && editable && (\n <button\n ref={chipRef}\n type=\"button\"\n onClick={() => setOpen((v) => !v)}\n aria-label={\n selected ? `${labels?.currency ?? \"Currency\"}: ${selected.code}` : (labels?.currency ?? \"Currency\")\n }\n className={cn(\n \"flex items-center gap-1 rounded px-2 py-1 font-medium text-slate-600 hover:bg-slate-100 dark:text-slate-300 dark:hover:bg-slate-800\",\n asDisplay ? \"text-sm\" : \"text-xs\",\n )}\n >\n {currency}\n <ChevronDown className=\"size-3\" />\n </button>\n )}\n </div>\n {editable && open && (\n <DropdownPanel\n anchorRef={chipRef}\n panelRef={panelRef}\n empty={filtered.length === 0}\n header={\n <DropdownSearchHeader\n query={query}\n onQueryChange={setQuery}\n inputRef={inputRef}\n placeholder={labels?.currencySearch ?? \"Search currency\"}\n />\n }\n >\n {filtered.map((c) => {\n const active = c.code === selected?.code;\n return (\n <li key={c.code}>\n <button\n type=\"button\"\n onClick={() => {\n onCurrencyChange?.(c.code);\n setOpen(false);\n }}\n className={cn(\n \"flex w-full items-center gap-2 px-3 py-1.5 text-sm text-left hover:bg-slate-50 dark:hover:bg-slate-800\",\n active && \"bg-slate-100 dark:bg-slate-800\",\n )}\n >\n <CurrencyFlag country={c.country} />\n <span className=\"font-mono text-slate-500 dark:text-slate-400 w-8 shrink-0 text-xs\">\n {c.symbol}\n </span>\n <span className=\"font-medium text-slate-900 dark:text-slate-100\">{c.code}</span>\n <span className=\"text-slate-500 dark:text-slate-400 truncate text-xs\">{c.name}</span>\n </button>\n </li>\n );\n })}\n </DropdownPanel>\n )}\n </div>\n {showNumpad && (\n <NumberPadSheet\n value={shown}\n onChange={handleText}\n onDone={() => innerRef.current?.blur()}\n label={label}\n labels={labels?.pad}\n />\n )}\n </div>\n );\n },\n);\n\nAmountInput.displayName = \"AmountInput\";\n"],"mappings":"AAwQQ,cAoGI,YApGJ;AAxQR,SAAS,YAAY,aAAa,OAAO,SAAS,QAAQ,gBAAgB;AAC1E,SAAS,mBAAmB;AAE5B,SAAS,YAAY,cAAc,mBAAmB;AACtD,SAAS,YAAY,eAAe,eAAe,sBAAsB,sBAAsB,mBAAmB;AAClH,SAAS,UAAU;AACnB,SAAS,qBAAqB;AAC9B,SAAS,wBAAqD;AAC9D,SAAS,sBAAiD;AAC1D,SAAS,eAAe,sBAAsB,yBAAyB;AACvE;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAsHP,MAAM,aAA+D;AAAA,EACnE,SAAS;AAAA,EACT,SAAS;AAAA,EACT,QAAQ;AACV;AAMA,MAAM,sBAAsB;AAAA,EAC1B;AAAA,EACA;AACF;AAeA,SAAS,WAAW,UAAkB,MAAuB;AAC3D,QAAM,IAAI,mBAAmB,QAAQ;AACrC,SAAO,MAAM,QAAQ,aAAa,CAAC,MAAM,KAAK,KAAK;AACrD;AAEO,MAAM,cAAc;AAAA,EACzB,CAAC,EAAE,OAAO,UAAU,UAAU,kBAAkB,aAAa,OAAO,UAAU,SAAS,WAAW,IAAI,WAAW,WAAW,OAAO,WAAW,WAAW,OAAO,kBAAkB,UAAU,SAAS,QAAQ,SAAS,OAAO,GAAG,QAAQ;AACtO,UAAM,cAAc,MAAM;AAC1B,UAAM,UAAU,MAAM;AACtB,UAAM,WAAW,CAAC,CAAC;AAInB,UAAM,WAAW,cAAc,aAAa,KAAK;AACjD,UAAM,WAAW,CAAC,YAAY,CAAC;AAC/B,UAAM,CAAC,SAAS,UAAU,IAAI,SAAS,KAAK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AA,kBAEA;AAAA,wCAAC,gBAAa,SAAS,EAAE,SAAS;AAAA,oBAClC,oBAAC,UAAK,WAAU,qEACb,YAAE,QACL;AAAA,oBACA,oBAAC,UAAK,WAAU,kDAAkD,YAAE,MAAK;AAAA,oBACzE,oBAAC,UAAK,WAAU,uDAAuD,YAAE,MAAK;AAAA;AAAA;AAAA,cAChF,KAlBO,EAAE,IAmBX;AAAA,YAEJ,CAAC;AAAA;AAAA,QACL;AAAA,SAEF;AAAA,MACC,cACC;AAAA,QAAC;AAAA;AAAA,UACC,OAAO;AAAA,UACP,UAAU;AAAA,UACV,QAAQ,MAAM,SAAS,SAAS,KAAK;AAAA,UACrC;AAAA,UACA,QAAQ,QAAQ;AAAA;AAAA,MAClB;AAAA,OAEJ;AAAA,EAEJ;AACF;AAEA,YAAY,cAAc;","names":[]}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
|
|
3
|
+
/** Screen-reader names for the calculator panel's own controls. */
|
|
4
|
+
interface CalculatorButtonLabels {
|
|
5
|
+
calculation?: string;
|
|
6
|
+
backspace?: string;
|
|
7
|
+
equals?: string;
|
|
8
|
+
}
|
|
9
|
+
interface CalculatorButtonProps {
|
|
10
|
+
/** Current field text — seeds the keypad so the user can keep calculating
|
|
11
|
+
* from what's already entered. */
|
|
12
|
+
value: string;
|
|
13
|
+
/**
|
|
14
|
+
* Called with the evaluated result whenever the running expression resolves
|
|
15
|
+
* to a number, so the host field tracks the keypad live.
|
|
16
|
+
*
|
|
17
|
+
* `expression` is the TEXT that result came out of — `"-42+50"` for an 8 — and
|
|
18
|
+
* is omitted when there was no calculation (a cleared pad). A host that keeps
|
|
19
|
+
* the sign of its figure OUTSIDE the text (see `AmountInput`'s `negative` /
|
|
20
|
+
* `onNegativeChange`, where a direction toggle owns it) cannot otherwise tell
|
|
21
|
+
* that 8 from a freshly typed one: the first is a sum that came out positive
|
|
22
|
+
* and has to take the direction with it, the second is a magnitude that must
|
|
23
|
+
* leave the direction alone. Hosts whose text carries its own sign can take
|
|
24
|
+
* the one argument and ignore this.
|
|
25
|
+
*/
|
|
26
|
+
onChange: (value: string, expression?: string) => void;
|
|
27
|
+
className?: string;
|
|
28
|
+
ariaLabel?: string;
|
|
29
|
+
/** Screen-reader names for the panel's own controls. `ariaLabel` above already
|
|
30
|
+
* covers the trigger; these three were hardcoded English inside the popover,
|
|
31
|
+
* which a translating host had no way to reach. */
|
|
32
|
+
labels?: CalculatorButtonLabels;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* A calculator icon that opens a small keypad popover anchored to its host
|
|
36
|
+
* field. Designed as a trailing-edge adornment: render it inside the field's
|
|
37
|
+
* `relative` wrapper, positioned absolutely. The keypad seeds from the current
|
|
38
|
+
* field value and writes evaluated results straight back through `onChange`.
|
|
39
|
+
*/
|
|
40
|
+
declare function CalculatorButton({ value, onChange, className, ariaLabel, labels, }: CalculatorButtonProps): react.JSX.Element;
|
|
41
|
+
|
|
42
|
+
export { CalculatorButton, type CalculatorButtonLabels };
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useRef, useState } from "react";
|
|
3
|
+
import { Calculator as CalculatorIcon, Delete } from "lucide-react";
|
|
4
|
+
import { Popover } from "./popover.js";
|
|
5
|
+
import { cn } from "../lib/cn.js";
|
|
6
|
+
import { evaluateExpression, formatResult, isBareAmount, splitLeadingSign } from "../lib/calc.js";
|
|
7
|
+
const KEYS = [
|
|
8
|
+
{ kind: "ins", label: "7", ins: "7" },
|
|
9
|
+
{ kind: "ins", label: "8", ins: "8" },
|
|
10
|
+
{ kind: "ins", label: "9", ins: "9" },
|
|
11
|
+
{ kind: "ins", label: "\xF7", ins: "\xF7", accent: true },
|
|
12
|
+
{ kind: "ins", label: "4", ins: "4" },
|
|
13
|
+
{ kind: "ins", label: "5", ins: "5" },
|
|
14
|
+
{ kind: "ins", label: "6", ins: "6" },
|
|
15
|
+
{ kind: "ins", label: "\xD7", ins: "\xD7", accent: true },
|
|
16
|
+
{ kind: "ins", label: "1", ins: "1" },
|
|
17
|
+
{ kind: "ins", label: "2", ins: "2" },
|
|
18
|
+
{ kind: "ins", label: "3", ins: "3" },
|
|
19
|
+
{ kind: "ins", label: "\u2212", ins: "\u2212", accent: true },
|
|
20
|
+
{ kind: "ins", label: "0", ins: "0" },
|
|
21
|
+
{ kind: "ins", label: ".", ins: "." },
|
|
22
|
+
{ kind: "back" },
|
|
23
|
+
{ kind: "ins", label: "+", ins: "+", accent: true }
|
|
24
|
+
];
|
|
25
|
+
const KEY_BASE = "flex h-9 items-center justify-center rounded-md text-sm font-medium transition-colors focus:outline-none focus:ring-2 focus:ring-slate-400";
|
|
26
|
+
const KEY_DIGIT = "bg-slate-100 text-slate-900 hover:bg-slate-200 dark:bg-slate-800 dark:text-slate-100 dark:hover:bg-slate-700";
|
|
27
|
+
const KEY_ACCENT = "bg-slate-200 text-slate-700 hover:bg-slate-300 dark:bg-slate-700 dark:text-slate-200 dark:hover:bg-slate-600";
|
|
28
|
+
function seed(initial) {
|
|
29
|
+
const t = initial.trim();
|
|
30
|
+
return t === "-" ? "" : t;
|
|
31
|
+
}
|
|
32
|
+
function CalculatorPanel({
|
|
33
|
+
initial,
|
|
34
|
+
onChange,
|
|
35
|
+
labels
|
|
36
|
+
}) {
|
|
37
|
+
const [expr, setExpr] = useState(() => seed(initial));
|
|
38
|
+
const inputRef = useRef(null);
|
|
39
|
+
const host = splitLeadingSign(initial.trim());
|
|
40
|
+
const text = host.sign === "-" && isBareAmount(expr) && host.rest === expr.trim() ? initial.trim() : expr;
|
|
41
|
+
const result = evaluateExpression(text);
|
|
42
|
+
useEffect(() => {
|
|
43
|
+
inputRef.current?.focus();
|
|
44
|
+
}, []);
|
|
45
|
+
const apply = (next) => {
|
|
46
|
+
setExpr(next);
|
|
47
|
+
const n = evaluateExpression(next);
|
|
48
|
+
if (n !== null) onChange(formatResult(n), next);
|
|
49
|
+
};
|
|
50
|
+
const clearAll = () => {
|
|
51
|
+
setExpr("");
|
|
52
|
+
onChange("");
|
|
53
|
+
};
|
|
54
|
+
const equals = () => {
|
|
55
|
+
if (result === null) return;
|
|
56
|
+
const out = formatResult(result);
|
|
57
|
+
setExpr(out);
|
|
58
|
+
onChange(out, text);
|
|
59
|
+
};
|
|
60
|
+
return /* @__PURE__ */ jsxs("div", { className: "space-y-2", children: [
|
|
61
|
+
/* @__PURE__ */ jsx(
|
|
62
|
+
"input",
|
|
63
|
+
{
|
|
64
|
+
ref: inputRef,
|
|
65
|
+
"aria-label": labels?.calculation ?? "Calculation",
|
|
66
|
+
value: text,
|
|
67
|
+
inputMode: "decimal",
|
|
68
|
+
onChange: (e) => apply(e.target.value),
|
|
69
|
+
onKeyDown: (e) => {
|
|
70
|
+
if (e.key === "Enter") {
|
|
71
|
+
e.preventDefault();
|
|
72
|
+
equals();
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
className: "block w-full rounded-md border border-slate-300 bg-white px-2 py-1.5 text-right font-mono text-sm text-slate-900 focus:border-slate-500 focus:ring-slate-500 dark:border-slate-700 dark:bg-slate-950 dark:text-slate-100 dark:focus:border-slate-400"
|
|
76
|
+
}
|
|
77
|
+
),
|
|
78
|
+
/* @__PURE__ */ jsx("div", { className: "h-4 pr-1 text-right font-mono text-xs text-slate-400 dark:text-slate-500", children: result !== null && formatResult(result) !== text.trim() ? `= ${formatResult(result)}` : "" }),
|
|
79
|
+
/* @__PURE__ */ jsxs("div", { className: "grid grid-cols-4 gap-1.5", children: [
|
|
80
|
+
KEYS.map(
|
|
81
|
+
(key, i) => key.kind === "back" ? /* @__PURE__ */ jsx(
|
|
82
|
+
"button",
|
|
83
|
+
{
|
|
84
|
+
type: "button",
|
|
85
|
+
"aria-label": labels?.backspace ?? "Backspace",
|
|
86
|
+
onClick: () => apply(text.slice(0, -1)),
|
|
87
|
+
className: cn(KEY_BASE, KEY_ACCENT),
|
|
88
|
+
children: /* @__PURE__ */ jsx(Delete, { className: "size-4" })
|
|
89
|
+
},
|
|
90
|
+
"back"
|
|
91
|
+
) : /* @__PURE__ */ jsx(
|
|
92
|
+
"button",
|
|
93
|
+
{
|
|
94
|
+
type: "button",
|
|
95
|
+
onClick: () => apply(text + key.ins),
|
|
96
|
+
className: cn(KEY_BASE, key.accent ? KEY_ACCENT : KEY_DIGIT),
|
|
97
|
+
children: key.label
|
|
98
|
+
},
|
|
99
|
+
`${key.ins}-${i}`
|
|
100
|
+
)
|
|
101
|
+
),
|
|
102
|
+
/* @__PURE__ */ jsx(
|
|
103
|
+
"button",
|
|
104
|
+
{
|
|
105
|
+
type: "button",
|
|
106
|
+
onClick: clearAll,
|
|
107
|
+
className: cn(KEY_BASE, "col-span-2 bg-slate-200 text-slate-700 hover:bg-slate-300 dark:bg-slate-700 dark:text-slate-200 dark:hover:bg-slate-600"),
|
|
108
|
+
children: "C"
|
|
109
|
+
}
|
|
110
|
+
),
|
|
111
|
+
/* @__PURE__ */ jsx(
|
|
112
|
+
"button",
|
|
113
|
+
{
|
|
114
|
+
type: "button",
|
|
115
|
+
"aria-label": labels?.equals ?? "Equals",
|
|
116
|
+
onClick: equals,
|
|
117
|
+
className: cn(
|
|
118
|
+
KEY_BASE,
|
|
119
|
+
"col-span-2 bg-slate-900 text-white hover:bg-slate-800 dark:bg-slate-100 dark:text-slate-900 dark:hover:bg-white"
|
|
120
|
+
),
|
|
121
|
+
children: "="
|
|
122
|
+
}
|
|
123
|
+
)
|
|
124
|
+
] })
|
|
125
|
+
] });
|
|
126
|
+
}
|
|
127
|
+
function CalculatorButton({
|
|
128
|
+
value,
|
|
129
|
+
onChange,
|
|
130
|
+
className,
|
|
131
|
+
ariaLabel = "Open calculator",
|
|
132
|
+
labels
|
|
133
|
+
}) {
|
|
134
|
+
return /* @__PURE__ */ jsx(
|
|
135
|
+
Popover,
|
|
136
|
+
{
|
|
137
|
+
width: 224,
|
|
138
|
+
trigger: ({ open, toggle, ref }) => /* @__PURE__ */ jsx(
|
|
139
|
+
"button",
|
|
140
|
+
{
|
|
141
|
+
ref,
|
|
142
|
+
type: "button",
|
|
143
|
+
tabIndex: -1,
|
|
144
|
+
onClick: toggle,
|
|
145
|
+
"aria-label": ariaLabel,
|
|
146
|
+
"aria-expanded": open,
|
|
147
|
+
className: cn(
|
|
148
|
+
"flex items-center justify-center text-slate-400 transition-colors hover:text-slate-600 dark:text-slate-500 dark:hover:text-slate-300",
|
|
149
|
+
className
|
|
150
|
+
),
|
|
151
|
+
children: /* @__PURE__ */ jsx(CalculatorIcon, { className: "size-4" })
|
|
152
|
+
}
|
|
153
|
+
),
|
|
154
|
+
children: () => /* @__PURE__ */ jsx(CalculatorPanel, { initial: value, onChange, labels })
|
|
155
|
+
}
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
export {
|
|
159
|
+
CalculatorButton
|
|
160
|
+
};
|
|
161
|
+
//# sourceMappingURL=calculator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/components/calculator.tsx"],"sourcesContent":["import { useEffect, useRef, useState } from \"react\";\nimport { Calculator as CalculatorIcon, Delete } from \"lucide-react\";\nimport { Popover } from \"./popover\";\nimport { cn } from \"../lib/cn\";\nimport { evaluateExpression, formatResult, isBareAmount, splitLeadingSign } from \"../lib/calc\";\n\n/** Screen-reader names for the calculator panel's own controls. */\nexport interface CalculatorButtonLabels {\n calculation?: string;\n backspace?: string;\n equals?: string;\n}\n\ninterface CalculatorButtonProps {\n /** Current field text — seeds the keypad so the user can keep calculating\n * from what's already entered. */\n value: string;\n /**\n * Called with the evaluated result whenever the running expression resolves\n * to a number, so the host field tracks the keypad live.\n *\n * `expression` is the TEXT that result came out of — `\"-42+50\"` for an 8 — and\n * is omitted when there was no calculation (a cleared pad). A host that keeps\n * the sign of its figure OUTSIDE the text (see `AmountInput`'s `negative` /\n * `onNegativeChange`, where a direction toggle owns it) cannot otherwise tell\n * that 8 from a freshly typed one: the first is a sum that came out positive\n * and has to take the direction with it, the second is a magnitude that must\n * leave the direction alone. Hosts whose text carries its own sign can take\n * the one argument and ignore this.\n */\n onChange: (value: string, expression?: string) => void;\n className?: string;\n ariaLabel?: string;\n /** Screen-reader names for the panel's own controls. `ariaLabel` above already\n * covers the trigger; these three were hardcoded English inside the popover,\n * which a translating host had no way to reach. */\n labels?: CalculatorButtonLabels;\n}\n\n/** Keypad layout (4 columns). The final C/= row is rendered separately. */\ntype Key =\n | { kind: \"ins\"; label: string; ins: string; accent?: boolean }\n | { kind: \"back\" };\n\nconst KEYS: Key[] = [\n { kind: \"ins\", label: \"7\", ins: \"7\" },\n { kind: \"ins\", label: \"8\", ins: \"8\" },\n { kind: \"ins\", label: \"9\", ins: \"9\" },\n { kind: \"ins\", label: \"÷\", ins: \"÷\", accent: true },\n { kind: \"ins\", label: \"4\", ins: \"4\" },\n { kind: \"ins\", label: \"5\", ins: \"5\" },\n { kind: \"ins\", label: \"6\", ins: \"6\" },\n { kind: \"ins\", label: \"×\", ins: \"×\", accent: true },\n { kind: \"ins\", label: \"1\", ins: \"1\" },\n { kind: \"ins\", label: \"2\", ins: \"2\" },\n { kind: \"ins\", label: \"3\", ins: \"3\" },\n { kind: \"ins\", label: \"−\", ins: \"−\", accent: true },\n { kind: \"ins\", label: \"0\", ins: \"0\" },\n { kind: \"ins\", label: \".\", ins: \".\" },\n { kind: \"back\" },\n { kind: \"ins\", label: \"+\", ins: \"+\", accent: true },\n];\n\nconst KEY_BASE =\n \"flex h-9 items-center justify-center rounded-md text-sm font-medium transition-colors focus:outline-none focus:ring-2 focus:ring-slate-400\";\nconst KEY_DIGIT =\n \"bg-slate-100 text-slate-900 hover:bg-slate-200 dark:bg-slate-800 dark:text-slate-100 dark:hover:bg-slate-700\";\nconst KEY_ACCENT =\n \"bg-slate-200 text-slate-700 hover:bg-slate-300 dark:bg-slate-700 dark:text-slate-200 dark:hover:bg-slate-600\";\n\nfunction seed(initial: string): string {\n const t = initial.trim();\n return t === \"-\" ? \"\" : t;\n}\n\nfunction CalculatorPanel({\n initial,\n onChange,\n labels,\n}: {\n initial: string;\n onChange: (value: string, expression?: string) => void;\n labels?: CalculatorButtonLabels;\n}) {\n const [expr, setExpr] = useState(() => seed(initial));\n const inputRef = useRef<HTMLInputElement>(null);\n // What the pad SHOWS. Normally the expression it holds — but when the host owns\n // the sign of the figure, the magnitude it hands back is not the whole number:\n // type 50 into a pad opened on an outflow and the host answers \"-50\". Derived\n // rather than stored, so there is no echo to guard against and a host that\n // ignores `onChange` entirely (a static `value`) still sees its own keystrokes:\n // the pad only defers when the host's text is character-for-character its own\n // last number with a minus in front.\n const host = splitLeadingSign(initial.trim());\n const text = host.sign === \"-\" && isBareAmount(expr) && host.rest === expr.trim() ? initial.trim() : expr;\n const result = evaluateExpression(text);\n\n // Focus the display on open so the user can type or hit Enter immediately.\n useEffect(() => {\n inputRef.current?.focus();\n }, []);\n\n // Update the host field whenever the expression resolves to a number; an\n // incomplete expression (e.g. \"12+\") simply leaves the field untouched. The\n // expression rides along with the result — see `onChange` above.\n const apply = (next: string) => {\n setExpr(next);\n const n = evaluateExpression(next);\n if (n !== null) onChange(formatResult(n), next);\n };\n\n const clearAll = () => {\n setExpr(\"\");\n onChange(\"\");\n };\n\n const equals = () => {\n if (result === null) return;\n const out = formatResult(result);\n setExpr(out);\n onChange(out, text);\n };\n\n return (\n <div className=\"space-y-2\">\n <input\n ref={inputRef}\n aria-label={labels?.calculation ?? \"Calculation\"}\n value={text}\n inputMode=\"decimal\"\n onChange={(e) => apply(e.target.value)}\n onKeyDown={(e) => {\n if (e.key === \"Enter\") {\n e.preventDefault();\n equals();\n }\n }}\n className=\"block w-full rounded-md border border-slate-300 bg-white px-2 py-1.5 text-right font-mono text-sm text-slate-900 focus:border-slate-500 focus:ring-slate-500 dark:border-slate-700 dark:bg-slate-950 dark:text-slate-100 dark:focus:border-slate-400\"\n />\n <div className=\"h-4 pr-1 text-right font-mono text-xs text-slate-400 dark:text-slate-500\">\n {result !== null && formatResult(result) !== text.trim() ? `= ${formatResult(result)}` : \"\"}\n </div>\n <div className=\"grid grid-cols-4 gap-1.5\">\n {KEYS.map((key, i) =>\n key.kind === \"back\" ? (\n <button\n key=\"back\"\n type=\"button\"\n aria-label={labels?.backspace ?? \"Backspace\"}\n onClick={() => apply(text.slice(0, -1))}\n className={cn(KEY_BASE, KEY_ACCENT)}\n >\n <Delete className=\"size-4\" />\n </button>\n ) : (\n <button\n key={`${key.ins}-${i}`}\n type=\"button\"\n onClick={() => apply(text + key.ins)}\n className={cn(KEY_BASE, key.accent ? KEY_ACCENT : KEY_DIGIT)}\n >\n {key.label}\n </button>\n ),\n )}\n <button\n type=\"button\"\n onClick={clearAll}\n className={cn(KEY_BASE, \"col-span-2 bg-slate-200 text-slate-700 hover:bg-slate-300 dark:bg-slate-700 dark:text-slate-200 dark:hover:bg-slate-600\")}\n >\n C\n </button>\n <button\n type=\"button\"\n aria-label={labels?.equals ?? \"Equals\"}\n onClick={equals}\n className={cn(\n KEY_BASE,\n \"col-span-2 bg-slate-900 text-white hover:bg-slate-800 dark:bg-slate-100 dark:text-slate-900 dark:hover:bg-white\",\n )}\n >\n =\n </button>\n </div>\n </div>\n );\n}\n\n/**\n * A calculator icon that opens a small keypad popover anchored to its host\n * field. Designed as a trailing-edge adornment: render it inside the field's\n * `relative` wrapper, positioned absolutely. The keypad seeds from the current\n * field value and writes evaluated results straight back through `onChange`.\n */\nexport function CalculatorButton({\n value,\n onChange,\n className,\n ariaLabel = \"Open calculator\",\n labels,\n}: CalculatorButtonProps) {\n return (\n <Popover\n width={224}\n trigger={({ open, toggle, ref }) => (\n <button\n ref={ref}\n type=\"button\"\n tabIndex={-1}\n onClick={toggle}\n aria-label={ariaLabel}\n aria-expanded={open}\n className={cn(\n \"flex items-center justify-center text-slate-400 transition-colors hover:text-slate-600 dark:text-slate-500 dark:hover:text-slate-300\",\n className,\n )}\n >\n <CalculatorIcon className=\"size-4\" />\n </button>\n )}\n >\n {() => <Calculat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SAAS;AAAA,UACT,cAAY;AAAA,UACZ,iBAAe;AAAA,UACf,WAAW;AAAA,YACT;AAAA,YACA;AAAA,UACF;AAAA,UAEA,8BAAC,kBAAe,WAAU,UAAS;AAAA;AAAA,MACrC;AAAA,MAGD,gBAAM,oBAAC,mBAAgB,SAAS,OAAO,UAAoB,QAAgB;AAAA;AAAA,EAC9E;AAEJ;","names":[]}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import * as recharts from 'recharts';
|
|
2
|
+
import { ResponsiveContainer, Tooltip } from 'recharts';
|
|
3
|
+
import * as react from 'react';
|
|
4
|
+
import { ReactNode, ComponentProps, RefObject } from 'react';
|
|
5
|
+
|
|
6
|
+
interface ChartSeriesConfig {
|
|
7
|
+
label: ReactNode;
|
|
8
|
+
color: string;
|
|
9
|
+
}
|
|
10
|
+
type ChartConfig = Record<string, ChartSeriesConfig>;
|
|
11
|
+
declare function useChart(): ChartConfig;
|
|
12
|
+
interface ChartContainerProps extends ComponentProps<"div"> {
|
|
13
|
+
config: ChartConfig;
|
|
14
|
+
children: ComponentProps<typeof ResponsiveContainer>["children"];
|
|
15
|
+
}
|
|
16
|
+
declare function ChartContainer({ id, className, children, config, ...props }: ChartContainerProps): react.JSX.Element;
|
|
17
|
+
declare const ChartTooltip: typeof Tooltip;
|
|
18
|
+
declare const ChartLegend: react.MemoExoticComponent<(outsideProps: recharts.LegendProps) => React.ReactPortal | null>;
|
|
19
|
+
interface TooltipPayloadItem {
|
|
20
|
+
dataKey?: string | number;
|
|
21
|
+
name?: string | number;
|
|
22
|
+
value?: number | string;
|
|
23
|
+
color?: string;
|
|
24
|
+
payload?: Record<string, unknown>;
|
|
25
|
+
}
|
|
26
|
+
interface ChartTooltipContentProps {
|
|
27
|
+
active?: boolean;
|
|
28
|
+
payload?: TooltipPayloadItem[];
|
|
29
|
+
label?: string | number;
|
|
30
|
+
indicator?: "dot" | "line";
|
|
31
|
+
hideLabel?: boolean;
|
|
32
|
+
labelFormatter?: (label: string | number) => ReactNode;
|
|
33
|
+
valueFormatter?: (value: number) => string;
|
|
34
|
+
/** Cursor position within the chart, injected by recharts. Used with
|
|
35
|
+
* {@link boundaryRef} to decide whether to flip the tooltip. */
|
|
36
|
+
coordinate?: {
|
|
37
|
+
x?: number;
|
|
38
|
+
y?: number;
|
|
39
|
+
};
|
|
40
|
+
/** When set, the tooltip flips to the LEFT of the cursor if it would otherwise
|
|
41
|
+
* spill past the right edge of this (usually horizontally-scrolling) container
|
|
42
|
+
* — e.g. a wide chart whose rightmost bars sat off-screen (feedback #77). */
|
|
43
|
+
boundaryRef?: RefObject<HTMLElement | null>;
|
|
44
|
+
}
|
|
45
|
+
declare function ChartTooltipContent({ active, payload, label, indicator, hideLabel, labelFormatter, valueFormatter, coordinate, boundaryRef, }: ChartTooltipContentProps): react.JSX.Element | null;
|
|
46
|
+
interface LegendPayloadItem {
|
|
47
|
+
value?: string;
|
|
48
|
+
dataKey?: string | number;
|
|
49
|
+
color?: string;
|
|
50
|
+
}
|
|
51
|
+
interface ChartLegendContentProps {
|
|
52
|
+
payload?: LegendPayloadItem[];
|
|
53
|
+
onItemClick?: (key: string) => void;
|
|
54
|
+
activeKey?: string | null;
|
|
55
|
+
}
|
|
56
|
+
declare function ChartLegendContent({ payload, onItemClick, activeKey }: ChartLegendContentProps): react.JSX.Element | null;
|
|
57
|
+
|
|
58
|
+
export { type ChartConfig, ChartContainer, ChartLegend, ChartLegendContent, type ChartSeriesConfig, ChartTooltip, ChartTooltipContent, useChart };
|