@milktop/react-ui 0.3.14 → 0.3.16

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.
@@ -18,5 +18,5 @@ export function ChartFrame({ rows, x, list, fmt, height, label, hover, onHover,
18
18
  observer.observe(el);
19
19
  return () => observer.disconnect();
20
20
  }, []);
21
- return (_jsxs("div", { ...rest, ...part('root'), className: cx(className, classes?.root), children: [_jsxs("div", { ...part('frame'), ref: frame, style: { height }, onPointerMove: onMove, onPointerLeave: () => onHover(null), role: "presentation", children: [width > 0 && children, hover != null && rows[hover] && (_jsxs("div", { ...part('tooltip'), "data-flip": flip || undefined, style: { left: tipLeft }, children: [_jsx("div", { ...part('tooltip-label'), children: String(rows[hover][x] ?? '') }), list.map((s) => (_jsxs("div", { ...part('tooltip-row'), children: [_jsx("span", { ...part('swatch'), style: { background: s.color } }), _jsx("span", { ...part('tooltip-name'), children: s.label }), _jsx("span", { ...part('tooltip-value'), children: fmt(rows[hover][s.key]) })] }, s.key)))] }))] }), list.length > 1 && (_jsx("div", { ...part('legend'), children: list.map((s) => _jsxs("span", { ...part('legend-item'), children: [_jsx("span", { ...part('swatch'), style: { background: s.color } }), s.label] }, s.key)) })), _jsxs("table", { ...part('sr-only'), children: [_jsx("caption", { children: label }), _jsxs("tbody", { children: [_jsxs("tr", { children: [_jsx("th", { children: x }), list.map((s) => _jsx("th", { children: s.label }, s.key))] }), rows.map((row, i) => _jsxs("tr", { children: [_jsx("td", { children: String(row[x] ?? '') }), list.map((s) => _jsx("td", { children: fmt(row[s.key]) }, s.key))] }, i))] })] })] }));
21
+ return (_jsxs("div", { ...rest, ...part('root'), className: cx(className, classes?.root), children: [_jsxs("div", { ...part('frame'), ref: frame, style: { height }, onPointerMove: onMove, onPointerLeave: () => onHover(null), role: "presentation", children: [width > 0 && children, hover != null && rows[hover] && (_jsxs("div", { ...part('tooltip'), "data-flip": flip || undefined, style: { left: tipLeft }, children: [_jsx("div", { ...part('tooltip-label'), children: String(rows[hover][x] ?? '') }), list.map((s) => (_jsxs("div", { ...part('tooltip-row'), children: [_jsx("span", { ...part('swatch'), style: { background: s.color } }), _jsx("span", { ...part('tooltip-name'), children: s.label }), _jsx("span", { ...part('tooltip-value'), children: fmt(rows[hover][s.key]) })] }, s.key)))] }))] }), list.length > 1 && (_jsx("div", { ...part('legend'), children: list.map((s) => _jsxs("span", { ...part('legend-item'), children: [_jsx("span", { ...part('swatch'), style: { background: s.color } }), s.label] }, s.key)) })), _jsx("div", { ...part('sr-only'), children: _jsxs("table", { children: [_jsx("caption", { children: label }), _jsxs("tbody", { children: [_jsxs("tr", { children: [_jsx("th", { children: x }), list.map((s) => _jsx("th", { children: s.label }, s.key))] }), rows.map((row, i) => _jsxs("tr", { children: [_jsx("td", { children: String(row[x] ?? '') }), list.map((s) => _jsx("td", { children: fmt(row[s.key]) }, s.key))] }, i))] })] }) })] }));
22
22
  }
@@ -37,5 +37,5 @@ export function DonutChart({ data = [], size = 160, thickness = 0.32, center, le
37
37
  const shown = hover != null ? data[hover] : null;
38
38
  return (_jsxs("div", { ...rest, ...part('root'), className: cx(className, classes?.root), "data-legend": legend || undefined, children: [_jsxs("div", { ...part('frame'), style: { width: size, height: size }, children: [_jsx("svg", { ...part('svg'), width: size, height: size, viewBox: `${-radius - 4} ${-radius - 4} ${size + 8} ${size + 8}`, "aria-hidden": "true", children: slices.map((s, i) => (_jsx("path", { ...part('slice'), d: (hover === i ? grown : shape)(s) ?? '', fill: colorOf(data[i], i), "data-dim": (hover != null && hover !== i) || undefined, onPointerEnter: () => setHover(i), onPointerLeave: () => setHover(null) }, data[i].label))) }), inner > 0 && center !== null && (_jsx("div", { ...part('center'), children: shown
39
39
  ? _jsxs(_Fragment, { children: [_jsx("strong", { children: fmt(shown.value) }), _jsx("span", { children: shown.label })] })
40
- : center ?? _jsxs(_Fragment, { children: [_jsx("strong", { children: fmt(total) }), _jsx("span", { children: t.chart.total })] }) }))] }), legend && (_jsx("ul", { ...part('legend'), children: data.map((slice, i) => (_jsxs("li", { ...part('legend-item'), "data-dim": (hover != null && hover !== i) || undefined, onPointerEnter: () => setHover(i), onPointerLeave: () => setHover(null), children: [_jsx("span", { ...part('swatch'), style: { background: colorOf(slice, i) } }), _jsx("span", { ...part('legend-label'), children: slice.label }), _jsxs("span", { ...part('legend-value'), children: [fmt(slice.value), total > 0 && _jsxs("small", { children: [" ", Math.round((slice.value / total) * 100), "%"] })] })] }, slice.label))) })), _jsxs("table", { ...part('sr-only'), children: [_jsx("caption", { children: label }), _jsx("tbody", { children: data.map((slice) => _jsxs("tr", { children: [_jsx("td", { children: slice.label }), _jsx("td", { children: fmt(slice.value) })] }, slice.label)) })] })] }));
40
+ : center ?? _jsxs(_Fragment, { children: [_jsx("strong", { children: fmt(total) }), _jsx("span", { children: t.chart.total })] }) }))] }), legend && (_jsx("ul", { ...part('legend'), children: data.map((slice, i) => (_jsxs("li", { ...part('legend-item'), "data-dim": (hover != null && hover !== i) || undefined, onPointerEnter: () => setHover(i), onPointerLeave: () => setHover(null), children: [_jsx("span", { ...part('swatch'), style: { background: colorOf(slice, i) } }), _jsx("span", { ...part('legend-label'), children: slice.label }), _jsxs("span", { ...part('legend-value'), children: [fmt(slice.value), total > 0 && _jsxs("small", { children: [" ", Math.round((slice.value / total) * 100), "%"] })] })] }, slice.label))) })), _jsx("div", { ...part('sr-only'), children: _jsxs("table", { children: [_jsx("caption", { children: label }), _jsx("tbody", { children: data.map((slice) => _jsxs("tr", { children: [_jsx("td", { children: slice.label }), _jsx("td", { children: fmt(slice.value) })] }, slice.label)) })] }) })] }));
41
41
  }
@@ -7,7 +7,9 @@
7
7
  * - `onValueChange`: called with the new number (or null)
8
8
  * - `min`, `max`, `step`
9
9
  * - `prefix`, `suffix`: text before or after it ('£', 'kg'); `icon`: an Iconify name before it
10
- * - `formatOptions`: Intl.NumberFormat options, e.g. { style: 'currency', currency: 'GBP' }, with `locale`
10
+ * - `formatOptions`: Intl.NumberFormat options, e.g. { style: 'currency', currency: 'GBP' }
11
+ * - `locale`: the number's language ('fr-FR' writes 4,25), else the LocaleProvider's; where the
12
+ * decimal is a comma and a point isn't a thousands separator (French), a typed point is a comma
11
13
  * - `allowMouseWheel`: scroll over the focused input to step
12
14
  * - `steppers`: false to hide the buttons
13
15
  * - `name`: for plain form posts
@@ -41,7 +43,7 @@ export type NumberInputProps = Omit<Ark.RootProps, 'value' | 'defaultValue' | 'o
41
43
  placeholder?: string;
42
44
  classes?: Classes<'root' | 'label' | 'control' | 'icon' | 'prefix' | 'input' | 'suffix' | 'decrementTrigger' | 'incrementTrigger'>;
43
45
  };
44
- declare function NumberInputShortcut({ label, value, defaultValue, onValueChange, prefix, suffix, icon, steppers, placeholder, className, classes, ...rest }: NumberInputProps): import("react").JSX.Element;
46
+ declare function NumberInputShortcut({ label, value, defaultValue, onValueChange, prefix, suffix, icon, steppers, placeholder, locale: localeProp, formatOptions: formatProp, className, classes, ...rest }: NumberInputProps): import("react").JSX.Element;
45
47
  export declare const NumberInput: typeof NumberInputShortcut & {
46
48
  Root: import("react").ForwardRefExoticComponent<Ark.RootProps & import("react").RefAttributes<HTMLDivElement>>;
47
49
  Label: import("react").ForwardRefExoticComponent<Ark.LabelProps & import("react").RefAttributes<HTMLLabelElement>>;
@@ -8,7 +8,9 @@ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-run
8
8
  * - `onValueChange`: called with the new number (or null)
9
9
  * - `min`, `max`, `step`
10
10
  * - `prefix`, `suffix`: text before or after it ('£', 'kg'); `icon`: an Iconify name before it
11
- * - `formatOptions`: Intl.NumberFormat options, e.g. { style: 'currency', currency: 'GBP' }, with `locale`
11
+ * - `formatOptions`: Intl.NumberFormat options, e.g. { style: 'currency', currency: 'GBP' }
12
+ * - `locale`: the number's language ('fr-FR' writes 4,25), else the LocaleProvider's; where the
13
+ * decimal is a comma and a point isn't a thousands separator (French), a typed point is a comma
12
14
  * - `allowMouseWheel`: scroll over the focused input to step
13
15
  * - `steppers`: false to hide the buttons
14
16
  * - `name`: for plain form posts
@@ -24,35 +26,91 @@ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-run
24
26
  * </NumberInput.Control>
25
27
  * </NumberInput.Root>
26
28
  */
27
- import { useState } from 'react';
29
+ import { useMemo, useState } from 'react';
28
30
  import { NumberInput as Ark } from '@ark-ui/react/number-input';
29
31
  import { useFieldContext } from '@ark-ui/react/field';
32
+ import { useLocaleContext } from '@ark-ui/react/locale';
33
+ import { NumberParser } from '@internationalized/number';
30
34
  import '../icons.js';
31
35
  import { cx, partsOf, withParts } from '../utils.js';
32
36
  import { useMessages } from '../format/LocaleProvider.js';
33
37
  import '../theme.css';
34
38
  import './number-input.css';
35
- const toText = (value) => (value == null ? '' : String(value));
36
- function NumberInputShortcut({ label = null, value, defaultValue, onValueChange, prefix = null, suffix = null, icon = null, steppers = true, placeholder = '', className, classes, ...rest }) {
39
+ // Without formatOptions zag reads the text with parseFloat, which stops at a decimal comma.
40
+ const plain = { maximumFractionDigits: 15, useGrouping: false };
41
+ // A French keypad's decimal key types a point. Caught natively, before zag's
42
+ // onBeforeInput refuses it: on keydown, and on beforeinput for text typed
43
+ // without one (Chrome fires beforeinput before the textInput React reads).
44
+ const pointAsComma = (parser) => (input) => {
45
+ if (!input)
46
+ return;
47
+ const insert = (event, typed) => {
48
+ event.preventDefault();
49
+ const text = typed.replaceAll('.', ',');
50
+ const start = input.selectionStart ?? input.value.length, end = input.selectionEnd ?? start;
51
+ if (!parser.isValidPartialNumber(input.value.slice(0, start) + text + input.value.slice(end)))
52
+ return;
53
+ input.setRangeText(text, start, end, 'end');
54
+ input.dispatchEvent(new Event('input', { bubbles: true }));
55
+ };
56
+ const key = (event) => {
57
+ if (event.key === '.' && !event.ctrlKey && !event.metaKey && !event.altKey && !event.isComposing)
58
+ insert(event, '.');
59
+ };
60
+ const beforeInput = (event) => {
61
+ if (event.inputType === 'insertText' && event.data?.includes('.'))
62
+ insert(event, event.data);
63
+ };
64
+ input.addEventListener('keydown', key);
65
+ input.addEventListener('beforeinput', beforeInput);
66
+ return () => {
67
+ input.removeEventListener('keydown', key);
68
+ input.removeEventListener('beforeinput', beforeInput);
69
+ };
70
+ };
71
+ function numbers(locale, given) {
72
+ const parts = new Intl.NumberFormat(locale).formatToParts(1234567.5);
73
+ const decimal = parts.find((p) => p.type === 'decimal')?.value;
74
+ const group = parts.find((p) => p.type === 'group')?.value;
75
+ const formatOptions = given ?? (decimal === '.' ? undefined : plain);
76
+ const format = new Intl.NumberFormat(locale, formatOptions);
77
+ const parser = new NumberParser(locale, formatOptions);
78
+ return {
79
+ formatOptions,
80
+ // Not where a point groups thousands (de-DE's 1.234).
81
+ inputRef: decimal === ',' && group !== '.' ? pointAsComma(parser) : undefined,
82
+ // As zag writes its value, so it reads it back.
83
+ toText: (value) => (value == null ? '' : formatOptions ? format.format(value) : String(value)),
84
+ };
85
+ }
86
+ function NumberInputShortcut({ label = null, value, defaultValue, onValueChange, prefix = null, suffix = null, icon = null, steppers = true, placeholder = '', locale: localeProp, formatOptions: formatProp, className, classes, ...rest }) {
37
87
  const t = useMessages();
38
88
  const field = useFieldContext();
39
89
  const part = partsOf('number-input', classes);
40
- // Zag works on the text ('1.', '' while typing); `value` is its number.
41
- // The text follows a controlled value that changes from outside.
90
+ const appLocale = useLocaleContext().locale;
91
+ const locale = localeProp ?? appLocale;
92
+ const key = JSON.stringify(formatProp ?? null);
93
+ const { formatOptions, inputRef, toText } = useMemo(() => numbers(locale, formatProp), [locale, key]); // eslint-disable-line react-hooks/exhaustive-deps
94
+ // Zag works on the text ('1,', '' while typing); `value` is its number.
95
+ // The text follows a controlled value that changes from outside, not the
96
+ // number it just reported.
42
97
  const [text, setText] = useState(() => toText(value !== undefined ? value : defaultValue));
43
98
  const [seen, setSeen] = useState(value);
99
+ const [sent, setSent] = useState(value);
44
100
  if (value !== seen) {
45
101
  setSeen(value);
46
- const number = text === '' ? null : Number(text);
47
- if (value !== number)
102
+ setSent(value);
103
+ if (value !== sent)
48
104
  setText(toText(value));
49
105
  }
50
106
  function change(details) {
107
+ const number = Number.isNaN(details.valueAsNumber) ? null : details.valueAsNumber;
51
108
  setText(details.value);
52
- onValueChange?.(Number.isNaN(details.valueAsNumber) ? null : details.valueAsNumber, details);
109
+ setSent(number);
110
+ onValueChange?.(number, details);
53
111
  }
54
- return (_jsxs(Ark.Root, { ...(t.zag.numberInput && { translations: t.zag.numberInput }), ...rest, ...part('root'), className: cx(className, classes?.root), value: text, onValueChange: change, children: [label && !field && _jsx(Ark.Label, { ...part('label'), children: label }), _jsxs(Ark.Control, { ...part('control'), children: [icon ? _jsx("iconify-icon", { ...part('icon'), icon: icon, "aria-hidden": "true" })
55
- : prefix != null && _jsx("span", { ...part('prefix'), children: prefix }), _jsx(Ark.Input, { ...part('input'), placeholder: placeholder }), suffix != null && _jsx("span", { ...part('suffix'), children: suffix }), steppers && _jsxs(_Fragment, { children: [_jsx(Ark.DecrementTrigger, { ...part('decrement-trigger'), children: _jsx("iconify-icon", { icon: "lucide:minus" }) }), _jsx(Ark.IncrementTrigger, { ...part('increment-trigger'), children: _jsx("iconify-icon", { icon: "lucide:plus" }) })] })] })] }));
112
+ return (_jsxs(Ark.Root, { ...(t.zag.numberInput && { translations: t.zag.numberInput }), ...rest, ...part('root'), className: cx(className, classes?.root), locale: locale, ...(formatOptions && { formatOptions }), value: text, onValueChange: change, children: [label && !field && _jsx(Ark.Label, { ...part('label'), children: label }), _jsxs(Ark.Control, { ...part('control'), children: [icon ? _jsx("iconify-icon", { ...part('icon'), icon: icon, "aria-hidden": "true" })
113
+ : prefix != null && _jsx("span", { ...part('prefix'), children: prefix }), _jsx(Ark.Input, { ...part('input'), placeholder: placeholder, ref: inputRef }), suffix != null && _jsx("span", { ...part('suffix'), children: suffix }), steppers && _jsxs(_Fragment, { children: [_jsx(Ark.DecrementTrigger, { ...part('decrement-trigger'), children: _jsx("iconify-icon", { icon: "lucide:minus" }) }), _jsx(Ark.IncrementTrigger, { ...part('increment-trigger'), children: _jsx("iconify-icon", { icon: "lucide:plus" }) })] })] })] }));
56
114
  }
57
115
  export const NumberInput = withParts(NumberInputShortcut, {
58
116
  Root: Ark.Root,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@milktop/react-ui",
3
- "version": "0.3.14",
3
+ "version": "0.3.16",
4
4
  "description": "React components on Ark UI (Zag state machines), with imba-ui's API and design",
5
5
  "license": "MIT",
6
6
  "author": "André Goldstein",
@@ -49,9 +49,10 @@
49
49
  "dev": "vite playground --port 5194",
50
50
  "build": "vite build playground",
51
51
  "build:lib": "rm -rf dist && tsc -p tsconfig.build.json && node scripts/after-tsc.js",
52
- "check": "tsc && eslint src playground",
52
+ "check": "tsc && eslint src playground test",
53
+ "test": "vitest run",
53
54
  "prepublishOnly": "npm run build:lib",
54
- "preversion": "npm run check && npm run build:lib",
55
+ "preversion": "npm run check && npm test && npm run build:lib",
55
56
  "postversion": "git push --follow-tags",
56
57
  "release:patch": "npm version patch",
57
58
  "release:minor": "npm version minor",
@@ -68,6 +69,7 @@
68
69
  "@dnd-kit/sortable": "^10.0.0",
69
70
  "@dnd-kit/utilities": "^3.2.2",
70
71
  "@internationalized/date": "^3.12.4",
72
+ "@internationalized/number": "^3.6.9",
71
73
  "@tiptap/core": "^3.31.4",
72
74
  "@tiptap/extension-image": "^3.31.4",
73
75
  "@tiptap/extensions": "^3.31.4",
@@ -88,16 +90,20 @@
88
90
  "@fontsource-variable/inter": "^5.3.0",
89
91
  "@fontsource-variable/plus-jakarta-sans": "^5.3.0",
90
92
  "@tailwindcss/vite": "^4.3.3",
93
+ "@testing-library/react": "^16.3.3",
94
+ "@testing-library/user-event": "^14.6.9",
91
95
  "@types/react": "^19.3.0",
92
96
  "@types/react-dom": "^19.3.0",
93
97
  "@vitejs/plugin-react": "^6.1.2",
94
98
  "eslint": "^10.12.0",
95
99
  "eslint-plugin-react-hooks": "^7.1.1",
100
+ "jsdom": "^31.0.0",
96
101
  "react": "^19.3.0",
97
102
  "react-dom": "^19.3.0",
98
103
  "tailwindcss": "^4.3.3",
99
104
  "typescript": "^6.0.3",
100
105
  "typescript-eslint": "^8.71.1",
101
- "vite": "^8.3.4"
106
+ "vite": "^8.3.4",
107
+ "vitest": "^5.1.0"
102
108
  }
103
109
  }