tekivex-ui 2.0.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/LICENSE +21 -0
- package/README.md +201 -0
- package/dist/index.cjs +70 -0
- package/dist/index.d.ts +89 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +11002 -0
- package/dist/src/a11y/index.d.ts +24 -0
- package/dist/src/a11y/index.d.ts.map +1 -0
- package/dist/src/components/TkxAccordion.d.ts +32 -0
- package/dist/src/components/TkxAccordion.d.ts.map +1 -0
- package/dist/src/components/TkxAlert.d.ts +14 -0
- package/dist/src/components/TkxAlert.d.ts.map +1 -0
- package/dist/src/components/TkxAvatar.d.ts +14 -0
- package/dist/src/components/TkxAvatar.d.ts.map +1 -0
- package/dist/src/components/TkxBadge.d.ts +12 -0
- package/dist/src/components/TkxBadge.d.ts.map +1 -0
- package/dist/src/components/TkxButton.d.ts +17 -0
- package/dist/src/components/TkxButton.d.ts.map +1 -0
- package/dist/src/components/TkxCard.d.ts +20 -0
- package/dist/src/components/TkxCard.d.ts.map +1 -0
- package/dist/src/components/TkxCarousel.d.ts +32 -0
- package/dist/src/components/TkxCarousel.d.ts.map +1 -0
- package/dist/src/components/TkxChat.d.ts +32 -0
- package/dist/src/components/TkxChat.d.ts.map +1 -0
- package/dist/src/components/TkxCheckbox.d.ts +13 -0
- package/dist/src/components/TkxCheckbox.d.ts.map +1 -0
- package/dist/src/components/TkxClock.d.ts +23 -0
- package/dist/src/components/TkxClock.d.ts.map +1 -0
- package/dist/src/components/TkxColorPicker.d.ts +18 -0
- package/dist/src/components/TkxColorPicker.d.ts.map +1 -0
- package/dist/src/components/TkxCommand.d.ts +34 -0
- package/dist/src/components/TkxCommand.d.ts.map +1 -0
- package/dist/src/components/TkxDatePicker.d.ts +47 -0
- package/dist/src/components/TkxDatePicker.d.ts.map +1 -0
- package/dist/src/components/TkxDivider.d.ts +8 -0
- package/dist/src/components/TkxDivider.d.ts.map +1 -0
- package/dist/src/components/TkxDrawer.d.ts +16 -0
- package/dist/src/components/TkxDrawer.d.ts.map +1 -0
- package/dist/src/components/TkxFileUpload.d.ts +16 -0
- package/dist/src/components/TkxFileUpload.d.ts.map +1 -0
- package/dist/src/components/TkxIcon.d.ts +16 -0
- package/dist/src/components/TkxIcon.d.ts.map +1 -0
- package/dist/src/components/TkxImage.d.ts +16 -0
- package/dist/src/components/TkxImage.d.ts.map +1 -0
- package/dist/src/components/TkxInput.d.ts +13 -0
- package/dist/src/components/TkxInput.d.ts.map +1 -0
- package/dist/src/components/TkxLogo.d.ts +21 -0
- package/dist/src/components/TkxLogo.d.ts.map +1 -0
- package/dist/src/components/TkxMenu.d.ts +64 -0
- package/dist/src/components/TkxMenu.d.ts.map +1 -0
- package/dist/src/components/TkxModal.d.ts +14 -0
- package/dist/src/components/TkxModal.d.ts.map +1 -0
- package/dist/src/components/TkxNumberInput.d.ts +32 -0
- package/dist/src/components/TkxNumberInput.d.ts.map +1 -0
- package/dist/src/components/TkxOTP.d.ts +24 -0
- package/dist/src/components/TkxOTP.d.ts.map +1 -0
- package/dist/src/components/TkxPagination.d.ts +19 -0
- package/dist/src/components/TkxPagination.d.ts.map +1 -0
- package/dist/src/components/TkxProgress.d.ts +13 -0
- package/dist/src/components/TkxProgress.d.ts.map +1 -0
- package/dist/src/components/TkxRadio.d.ts +22 -0
- package/dist/src/components/TkxRadio.d.ts.map +1 -0
- package/dist/src/components/TkxRating.d.ts +18 -0
- package/dist/src/components/TkxRating.d.ts.map +1 -0
- package/dist/src/components/TkxSelect.d.ts +37 -0
- package/dist/src/components/TkxSelect.d.ts.map +1 -0
- package/dist/src/components/TkxSkeleton.d.ts +12 -0
- package/dist/src/components/TkxSkeleton.d.ts.map +1 -0
- package/dist/src/components/TkxSlider.d.ts +33 -0
- package/dist/src/components/TkxSlider.d.ts.map +1 -0
- package/dist/src/components/TkxStepper.d.ts +31 -0
- package/dist/src/components/TkxStepper.d.ts.map +1 -0
- package/dist/src/components/TkxTable.d.ts +23 -0
- package/dist/src/components/TkxTable.d.ts.map +1 -0
- package/dist/src/components/TkxTabs.d.ts +32 -0
- package/dist/src/components/TkxTabs.d.ts.map +1 -0
- package/dist/src/components/TkxTag.d.ts +30 -0
- package/dist/src/components/TkxTag.d.ts.map +1 -0
- package/dist/src/components/TkxTimeline.d.ts +20 -0
- package/dist/src/components/TkxTimeline.d.ts.map +1 -0
- package/dist/src/components/TkxToast.d.ts +23 -0
- package/dist/src/components/TkxToast.d.ts.map +1 -0
- package/dist/src/components/TkxToggle.d.ts +11 -0
- package/dist/src/components/TkxToggle.d.ts.map +1 -0
- package/dist/src/components/TkxTooltip.d.ts +10 -0
- package/dist/src/components/TkxTooltip.d.ts.map +1 -0
- package/dist/src/components/TkxVideoPlayer.d.ts +43 -0
- package/dist/src/components/TkxVideoPlayer.d.ts.map +1 -0
- package/dist/src/components/index.d.ts +41 -0
- package/dist/src/components/index.d.ts.map +1 -0
- package/dist/src/engine/css.d.ts +24 -0
- package/dist/src/engine/css.d.ts.map +1 -0
- package/dist/src/engine/index.d.ts +10 -0
- package/dist/src/engine/index.d.ts.map +1 -0
- package/dist/src/engine/quantum.d.ts +47 -0
- package/dist/src/engine/quantum.d.ts.map +1 -0
- package/dist/src/engine/security.d.ts +54 -0
- package/dist/src/engine/security.d.ts.map +1 -0
- package/dist/src/engine/tkx.d.ts +29 -0
- package/dist/src/engine/tkx.d.ts.map +1 -0
- package/dist/src/engine/wcag.d.ts +34 -0
- package/dist/src/engine/wcag.d.ts.map +1 -0
- package/dist/src/hooks/index.d.ts +8 -0
- package/dist/src/hooks/index.d.ts.map +1 -0
- package/dist/src/themes/index.d.ts +26 -0
- package/dist/src/themes/index.d.ts.map +1 -0
- package/dist/style.css +1 -0
- package/dist/tests/TkxButton.test.d.ts +2 -0
- package/dist/tests/TkxButton.test.d.ts.map +1 -0
- package/dist/tests/a11y/components.test.d.ts +2 -0
- package/dist/tests/a11y/components.test.d.ts.map +1 -0
- package/dist/tests/quantum.test.d.ts +2 -0
- package/dist/tests/quantum.test.d.ts.map +1 -0
- package/dist/tests/security.test.d.ts +2 -0
- package/dist/tests/security.test.d.ts.map +1 -0
- package/dist/tests/setup.d.ts +2 -0
- package/dist/tests/setup.d.ts.map +1 -0
- package/dist/tests/tkx.test.d.ts +2 -0
- package/dist/tests/tkx.test.d.ts.map +1 -0
- package/dist/tests/wcag.test.d.ts +2 -0
- package/dist/tests/wcag.test.d.ts.map +1 -0
- package/package.json +78 -0
- package/src/a11y/index.tsx +87 -0
- package/src/components/TkxAccordion.tsx +588 -0
- package/src/components/TkxAlert.tsx +91 -0
- package/src/components/TkxAvatar.tsx +101 -0
- package/src/components/TkxBadge.tsx +73 -0
- package/src/components/TkxButton.tsx +153 -0
- package/src/components/TkxCard.tsx +92 -0
- package/src/components/TkxCarousel.tsx +552 -0
- package/src/components/TkxChat.tsx +354 -0
- package/src/components/TkxCheckbox.tsx +218 -0
- package/src/components/TkxClock.tsx +601 -0
- package/src/components/TkxColorPicker.tsx +633 -0
- package/src/components/TkxCommand.tsx +644 -0
- package/src/components/TkxDatePicker.tsx +1567 -0
- package/src/components/TkxDivider.tsx +49 -0
- package/src/components/TkxDrawer.tsx +234 -0
- package/src/components/TkxFileUpload.tsx +371 -0
- package/src/components/TkxIcon.tsx +683 -0
- package/src/components/TkxImage.tsx +277 -0
- package/src/components/TkxInput.tsx +101 -0
- package/src/components/TkxLogo.tsx +297 -0
- package/src/components/TkxMenu.tsx +1042 -0
- package/src/components/TkxModal.tsx +119 -0
- package/src/components/TkxNumberInput.tsx +408 -0
- package/src/components/TkxOTP.tsx +295 -0
- package/src/components/TkxPagination.tsx +388 -0
- package/src/components/TkxProgress.tsx +114 -0
- package/src/components/TkxRadio.tsx +284 -0
- package/src/components/TkxRating.tsx +286 -0
- package/src/components/TkxSelect.tsx +923 -0
- package/src/components/TkxSkeleton.tsx +105 -0
- package/src/components/TkxSlider.tsx +492 -0
- package/src/components/TkxStepper.tsx +485 -0
- package/src/components/TkxTable.tsx +148 -0
- package/src/components/TkxTabs.tsx +124 -0
- package/src/components/TkxTag.tsx +319 -0
- package/src/components/TkxTimeline.tsx +303 -0
- package/src/components/TkxToast.tsx +284 -0
- package/src/components/TkxToggle.tsx +93 -0
- package/src/components/TkxTooltip.tsx +69 -0
- package/src/components/TkxVideoPlayer.tsx +1013 -0
- package/src/components/index.ts +40 -0
- package/src/engine/css.ts +126 -0
- package/src/engine/index.ts +10 -0
- package/src/engine/quantum.ts +207 -0
- package/src/engine/security.ts +217 -0
- package/src/engine/tkx.ts +874 -0
- package/src/engine/wcag.ts +275 -0
- package/src/hooks/index.ts +120 -0
- package/src/styles/global.css +167 -0
- package/src/themes/index.ts +111 -0
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { useEffect, useId, type ReactNode } from 'react';
|
|
2
|
+
import { createPortal } from 'react-dom';
|
|
3
|
+
import { useTheme } from '../themes';
|
|
4
|
+
import { sanitizeString } from '../engine/security';
|
|
5
|
+
import { useFocusTrap, useEscapeKey, useAnnounce } from '../hooks';
|
|
6
|
+
import { tkx } from '../engine/tkx';
|
|
7
|
+
|
|
8
|
+
export type ModalSize = 'sm' | 'md' | 'lg' | 'full';
|
|
9
|
+
|
|
10
|
+
export interface TkxModalProps {
|
|
11
|
+
isOpen: boolean;
|
|
12
|
+
onClose: () => void;
|
|
13
|
+
title: string;
|
|
14
|
+
size?: ModalSize;
|
|
15
|
+
closeOnOverlayClick?: boolean;
|
|
16
|
+
closeOnEscape?: boolean;
|
|
17
|
+
children: ReactNode;
|
|
18
|
+
footer?: ReactNode;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const SIZE_W: Record<ModalSize, string> = { sm: '400px', md: '560px', lg: '720px', full: '100vw' };
|
|
22
|
+
|
|
23
|
+
let scrollLockCount = 0;
|
|
24
|
+
function lockScroll() { if (++scrollLockCount === 1) document.body.style.overflow = 'hidden'; }
|
|
25
|
+
function unlockScroll() { if (--scrollLockCount <= 0) { scrollLockCount = 0; document.body.style.overflow = ''; } }
|
|
26
|
+
|
|
27
|
+
export function TkxModal({ isOpen, onClose, title, size = 'md', closeOnOverlayClick = true, closeOnEscape = true, children, footer }: TkxModalProps) {
|
|
28
|
+
const theme = useTheme();
|
|
29
|
+
const titleId = useId();
|
|
30
|
+
const trapRef = useFocusTrap(isOpen);
|
|
31
|
+
const announce = useAnnounce();
|
|
32
|
+
const safeTitle = sanitizeString(title);
|
|
33
|
+
const isFull = size === 'full';
|
|
34
|
+
|
|
35
|
+
useEscapeKey(onClose, closeOnEscape && isOpen);
|
|
36
|
+
|
|
37
|
+
useEffect(() => {
|
|
38
|
+
if (isOpen) { lockScroll(); announce(safeTitle, 'polite'); }
|
|
39
|
+
else { unlockScroll(); }
|
|
40
|
+
return () => { if (isOpen) unlockScroll(); };
|
|
41
|
+
}, [isOpen, safeTitle, announce]);
|
|
42
|
+
|
|
43
|
+
if (!isOpen || typeof document === 'undefined') return null;
|
|
44
|
+
|
|
45
|
+
return createPortal(
|
|
46
|
+
<div
|
|
47
|
+
role="presentation"
|
|
48
|
+
className={tkx('fixed inset-0 z-[9000] flex', isFull ? 'items-stretch' : 'items-center justify-center', isFull ? '' : 'p-4')}
|
|
49
|
+
>
|
|
50
|
+
{/* Backdrop */}
|
|
51
|
+
<div
|
|
52
|
+
aria-hidden="true"
|
|
53
|
+
onClick={closeOnOverlayClick ? onClose : undefined}
|
|
54
|
+
className={tkx('absolute inset-0 animate-fade-in')}
|
|
55
|
+
style={{ backgroundColor: 'rgba(0,0,0,0.6)', backdropFilter: 'blur(4px)' }}
|
|
56
|
+
/>
|
|
57
|
+
|
|
58
|
+
{/* Dialog */}
|
|
59
|
+
<div
|
|
60
|
+
ref={trapRef as React.RefObject<HTMLDivElement>}
|
|
61
|
+
role="dialog"
|
|
62
|
+
aria-modal="true"
|
|
63
|
+
aria-labelledby={titleId}
|
|
64
|
+
className={tkx('relative z-10 flex flex-col overflow-hidden animate-slide-up', isFull ? '' : 'rounded-xl')}
|
|
65
|
+
style={{
|
|
66
|
+
backgroundColor: theme.surface,
|
|
67
|
+
border: `1px solid ${theme.border}`,
|
|
68
|
+
width: SIZE_W[size],
|
|
69
|
+
maxWidth: '100%',
|
|
70
|
+
maxHeight: isFull ? '100vh' : '90vh',
|
|
71
|
+
boxShadow: `0 20px 60px ${theme.bg}80`,
|
|
72
|
+
}}
|
|
73
|
+
>
|
|
74
|
+
{/* Header */}
|
|
75
|
+
<div
|
|
76
|
+
className={tkx('flex items-center justify-between px-6 py-5 shrink-0')}
|
|
77
|
+
style={{ borderBottom: `1px solid ${theme.border}` }}
|
|
78
|
+
>
|
|
79
|
+
<h2
|
|
80
|
+
id={titleId}
|
|
81
|
+
className={tkx('m-0 text-lg font-semibold font-sans')}
|
|
82
|
+
style={{ color: theme.text }}
|
|
83
|
+
>
|
|
84
|
+
{safeTitle}
|
|
85
|
+
</h2>
|
|
86
|
+
<button
|
|
87
|
+
onClick={onClose}
|
|
88
|
+
aria-label="Close dialog"
|
|
89
|
+
className={tkx('bg-transparent border-none cursor-pointer rounded p-1 flex items-center justify-center focus-visible:focus-ring')}
|
|
90
|
+
style={{ color: theme.textMuted }}
|
|
91
|
+
>
|
|
92
|
+
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={2} aria-hidden="true">
|
|
93
|
+
<line x1="18" y1="6" x2="6" y2="18" /><line x1="6" y1="6" x2="18" y2="18" />
|
|
94
|
+
</svg>
|
|
95
|
+
</button>
|
|
96
|
+
</div>
|
|
97
|
+
|
|
98
|
+
{/* Body */}
|
|
99
|
+
<div
|
|
100
|
+
className={tkx('flex-1 overflow-y-auto px-6 py-6 font-sans')}
|
|
101
|
+
style={{ color: theme.text }}
|
|
102
|
+
>
|
|
103
|
+
{children}
|
|
104
|
+
</div>
|
|
105
|
+
|
|
106
|
+
{/* Footer */}
|
|
107
|
+
{footer && (
|
|
108
|
+
<div
|
|
109
|
+
className={tkx('flex justify-end gap-2 px-6 py-4 shrink-0')}
|
|
110
|
+
style={{ borderTop: `1px solid ${theme.border}` }}
|
|
111
|
+
>
|
|
112
|
+
{footer}
|
|
113
|
+
</div>
|
|
114
|
+
)}
|
|
115
|
+
</div>
|
|
116
|
+
</div>,
|
|
117
|
+
document.body,
|
|
118
|
+
);
|
|
119
|
+
}
|
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useState,
|
|
3
|
+
useRef,
|
|
4
|
+
useEffect,
|
|
5
|
+
useCallback,
|
|
6
|
+
useId,
|
|
7
|
+
type CSSProperties,
|
|
8
|
+
type KeyboardEvent,
|
|
9
|
+
type WheelEvent,
|
|
10
|
+
} from 'react';
|
|
11
|
+
import { useTheme } from '../themes';
|
|
12
|
+
import { tkx, cx } from '../engine/tkx';
|
|
13
|
+
import { sanitizeString } from '../engine/security';
|
|
14
|
+
|
|
15
|
+
// ── Types ─────────────────────────────────────────────────────────────────────
|
|
16
|
+
|
|
17
|
+
export interface TkxNumberInputProps {
|
|
18
|
+
value?: number;
|
|
19
|
+
defaultValue?: number;
|
|
20
|
+
onChange?: (value: number | null) => void;
|
|
21
|
+
min?: number;
|
|
22
|
+
max?: number;
|
|
23
|
+
step?: number;
|
|
24
|
+
precision?: number;
|
|
25
|
+
prefix?: string;
|
|
26
|
+
suffix?: string;
|
|
27
|
+
format?: 'decimal' | 'currency' | 'percent';
|
|
28
|
+
currency?: string;
|
|
29
|
+
locale?: string;
|
|
30
|
+
clampOnBlur?: boolean;
|
|
31
|
+
allowMouseWheel?: boolean;
|
|
32
|
+
size?: 'sm' | 'md' | 'lg';
|
|
33
|
+
label?: string;
|
|
34
|
+
hint?: string;
|
|
35
|
+
isDisabled?: boolean;
|
|
36
|
+
isReadOnly?: boolean;
|
|
37
|
+
isInvalid?: boolean;
|
|
38
|
+
errorMessage?: string;
|
|
39
|
+
id?: string;
|
|
40
|
+
className?: string;
|
|
41
|
+
style?: CSSProperties;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// ── Size maps ─────────────────────────────────────────────────────────────────
|
|
45
|
+
|
|
46
|
+
const SIZE_MAP = {
|
|
47
|
+
sm: { height: '32px', fontSize: '0.8125rem', px: '8px', btnW: '28px', iconSz: 14 },
|
|
48
|
+
md: { height: '40px', fontSize: '0.875rem', px: '12px', btnW: '34px', iconSz: 16 },
|
|
49
|
+
lg: { height: '48px', fontSize: '1rem', px: '14px', btnW: '40px', iconSz: 18 },
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
// ── Helpers ───────────────────────────────────────────────────────────────────
|
|
53
|
+
|
|
54
|
+
function clamp(v: number, min?: number, max?: number): number {
|
|
55
|
+
let result = v;
|
|
56
|
+
if (min !== undefined) result = Math.max(result, min);
|
|
57
|
+
if (max !== undefined) result = Math.min(result, max);
|
|
58
|
+
return result;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function roundToPrecision(v: number, precision?: number): number {
|
|
62
|
+
if (precision === undefined) return v;
|
|
63
|
+
const factor = Math.pow(10, precision);
|
|
64
|
+
return Math.round(v * factor) / factor;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function formatValue(
|
|
68
|
+
value: number,
|
|
69
|
+
format?: TkxNumberInputProps['format'],
|
|
70
|
+
currency?: string,
|
|
71
|
+
locale?: string,
|
|
72
|
+
precision?: number,
|
|
73
|
+
): string {
|
|
74
|
+
const loc = locale ?? 'en-US';
|
|
75
|
+
if (format === 'currency') {
|
|
76
|
+
return new Intl.NumberFormat(loc, {
|
|
77
|
+
style: 'currency',
|
|
78
|
+
currency: currency ?? 'USD',
|
|
79
|
+
minimumFractionDigits: precision ?? 2,
|
|
80
|
+
maximumFractionDigits: precision ?? 2,
|
|
81
|
+
}).format(value);
|
|
82
|
+
}
|
|
83
|
+
if (format === 'percent') {
|
|
84
|
+
return new Intl.NumberFormat(loc, {
|
|
85
|
+
style: 'percent',
|
|
86
|
+
minimumFractionDigits: precision ?? 0,
|
|
87
|
+
maximumFractionDigits: precision ?? 0,
|
|
88
|
+
}).format(value / 100);
|
|
89
|
+
}
|
|
90
|
+
if (format === 'decimal' || precision !== undefined) {
|
|
91
|
+
return new Intl.NumberFormat(loc, {
|
|
92
|
+
minimumFractionDigits: precision ?? 0,
|
|
93
|
+
maximumFractionDigits: precision ?? 20,
|
|
94
|
+
}).format(value);
|
|
95
|
+
}
|
|
96
|
+
return String(value);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// ── Stepper button ────────────────────────────────────────────────────────────
|
|
100
|
+
|
|
101
|
+
interface StepperBtnProps {
|
|
102
|
+
direction: 'inc' | 'dec';
|
|
103
|
+
isDisabled: boolean;
|
|
104
|
+
btnW: string;
|
|
105
|
+
height: string;
|
|
106
|
+
primaryColor: string;
|
|
107
|
+
borderColor: string;
|
|
108
|
+
textMuted: string;
|
|
109
|
+
iconSz: number;
|
|
110
|
+
onStep: (dir: 1 | -1) => void;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function StepperBtn({ direction, isDisabled, btnW, height, primaryColor, borderColor, textMuted, iconSz, onStep }: StepperBtnProps) {
|
|
114
|
+
const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
115
|
+
const intervalRef = useRef<ReturnType<typeof setInterval> | null>(null);
|
|
116
|
+
const dir = direction === 'inc' ? 1 : -1;
|
|
117
|
+
|
|
118
|
+
const startHold = useCallback(() => {
|
|
119
|
+
onStep(dir);
|
|
120
|
+
timerRef.current = setTimeout(() => {
|
|
121
|
+
intervalRef.current = setInterval(() => onStep(dir), 60);
|
|
122
|
+
}, 400);
|
|
123
|
+
}, [dir, onStep]);
|
|
124
|
+
|
|
125
|
+
const stopHold = useCallback(() => {
|
|
126
|
+
if (timerRef.current) { clearTimeout(timerRef.current); timerRef.current = null; }
|
|
127
|
+
if (intervalRef.current) { clearInterval(intervalRef.current); intervalRef.current = null; }
|
|
128
|
+
}, []);
|
|
129
|
+
|
|
130
|
+
useEffect(() => () => stopHold(), [stopHold]);
|
|
131
|
+
|
|
132
|
+
const isInc = direction === 'inc';
|
|
133
|
+
|
|
134
|
+
return (
|
|
135
|
+
<button
|
|
136
|
+
type="button"
|
|
137
|
+
aria-label={isInc ? 'Increment' : 'Decrement'}
|
|
138
|
+
disabled={isDisabled}
|
|
139
|
+
onMouseDown={startHold}
|
|
140
|
+
onMouseUp={stopHold}
|
|
141
|
+
onMouseLeave={stopHold}
|
|
142
|
+
onTouchStart={(e) => { e.preventDefault(); startHold(); }}
|
|
143
|
+
onTouchEnd={stopHold}
|
|
144
|
+
style={{
|
|
145
|
+
width: btnW,
|
|
146
|
+
height,
|
|
147
|
+
minWidth: btnW,
|
|
148
|
+
flexShrink: 0,
|
|
149
|
+
display: 'flex',
|
|
150
|
+
alignItems: 'center',
|
|
151
|
+
justifyContent: 'center',
|
|
152
|
+
background: 'transparent',
|
|
153
|
+
border: 'none',
|
|
154
|
+
borderRight: isInc ? undefined : `1px solid ${borderColor}`,
|
|
155
|
+
borderLeft: isInc ? `1px solid ${borderColor}` : undefined,
|
|
156
|
+
cursor: isDisabled ? 'not-allowed' : 'pointer',
|
|
157
|
+
color: isDisabled ? textMuted : primaryColor,
|
|
158
|
+
transition: 'color 120ms ease, background 120ms ease',
|
|
159
|
+
outline: 'none',
|
|
160
|
+
}}
|
|
161
|
+
onFocus={(e) => { if (!isDisabled) (e.currentTarget as HTMLElement).style.background = `${primaryColor}12`; }}
|
|
162
|
+
onBlur={(e) => { (e.currentTarget as HTMLElement).style.background = 'transparent'; }}
|
|
163
|
+
>
|
|
164
|
+
{isInc ? (
|
|
165
|
+
<svg width={iconSz} height={iconSz} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={2.5} aria-hidden="true">
|
|
166
|
+
<line x1="12" y1="5" x2="12" y2="19" />
|
|
167
|
+
<line x1="5" y1="12" x2="19" y2="12" />
|
|
168
|
+
</svg>
|
|
169
|
+
) : (
|
|
170
|
+
<svg width={iconSz} height={iconSz} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={2.5} aria-hidden="true">
|
|
171
|
+
<line x1="5" y1="12" x2="19" y2="12" />
|
|
172
|
+
</svg>
|
|
173
|
+
)}
|
|
174
|
+
</button>
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// ── Main component ────────────────────────────────────────────────────────────
|
|
179
|
+
|
|
180
|
+
export function TkxNumberInput({
|
|
181
|
+
value,
|
|
182
|
+
defaultValue = 0,
|
|
183
|
+
onChange,
|
|
184
|
+
min,
|
|
185
|
+
max,
|
|
186
|
+
step = 1,
|
|
187
|
+
precision,
|
|
188
|
+
prefix,
|
|
189
|
+
suffix,
|
|
190
|
+
format,
|
|
191
|
+
currency,
|
|
192
|
+
locale,
|
|
193
|
+
clampOnBlur = true,
|
|
194
|
+
allowMouseWheel = false,
|
|
195
|
+
size = 'md',
|
|
196
|
+
label,
|
|
197
|
+
hint,
|
|
198
|
+
isDisabled = false,
|
|
199
|
+
isReadOnly = false,
|
|
200
|
+
isInvalid = false,
|
|
201
|
+
errorMessage,
|
|
202
|
+
id: idProp,
|
|
203
|
+
className,
|
|
204
|
+
style,
|
|
205
|
+
}: TkxNumberInputProps) {
|
|
206
|
+
const theme = useTheme();
|
|
207
|
+
const autoId = useId();
|
|
208
|
+
const id = idProp ?? autoId;
|
|
209
|
+
const hintId = `${id}-hint`;
|
|
210
|
+
const errorId = `${id}-error`;
|
|
211
|
+
|
|
212
|
+
const isControlled = value !== undefined;
|
|
213
|
+
const [internalValue, setInternalValue] = useState<number>(defaultValue);
|
|
214
|
+
const numericValue = isControlled ? value! : internalValue;
|
|
215
|
+
|
|
216
|
+
const [focused, setFocused] = useState(false);
|
|
217
|
+
const [rawInput, setRawInput] = useState('');
|
|
218
|
+
|
|
219
|
+
const sz = SIZE_MAP[size];
|
|
220
|
+
const hasError = isInvalid || !!errorMessage;
|
|
221
|
+
const borderColor = hasError ? theme.danger : focused ? theme.primary : theme.border;
|
|
222
|
+
const describedBy = [hint && hintId, hasError && errorId].filter(Boolean).join(' ') || undefined;
|
|
223
|
+
|
|
224
|
+
const safeLabel = label ? sanitizeString(label) : undefined;
|
|
225
|
+
const safeHint = hint ? sanitizeString(hint) : undefined;
|
|
226
|
+
const safeError = errorMessage ? sanitizeString(errorMessage) : undefined;
|
|
227
|
+
const safePrefix = prefix ? sanitizeString(prefix) : undefined;
|
|
228
|
+
const safeSuffix = suffix ? sanitizeString(suffix) : undefined;
|
|
229
|
+
|
|
230
|
+
const setVal = useCallback((v: number) => {
|
|
231
|
+
const rounded = roundToPrecision(v, precision);
|
|
232
|
+
if (!isControlled) setInternalValue(rounded);
|
|
233
|
+
onChange?.(rounded);
|
|
234
|
+
}, [isControlled, onChange, precision]);
|
|
235
|
+
|
|
236
|
+
const step_ = useCallback((dir: 1 | -1) => {
|
|
237
|
+
if (isDisabled || isReadOnly) return;
|
|
238
|
+
const next = clamp(roundToPrecision(numericValue + dir * step, precision), min, max);
|
|
239
|
+
setVal(next);
|
|
240
|
+
}, [isDisabled, isReadOnly, numericValue, step, precision, min, max, setVal]);
|
|
241
|
+
|
|
242
|
+
const handleFocus = () => {
|
|
243
|
+
setFocused(true);
|
|
244
|
+
setRawInput(String(numericValue));
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
const handleBlur = () => {
|
|
248
|
+
setFocused(false);
|
|
249
|
+
const parsed = parseFloat(rawInput);
|
|
250
|
+
if (isNaN(parsed)) {
|
|
251
|
+
onChange?.(null);
|
|
252
|
+
setRawInput(String(numericValue));
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
let next = roundToPrecision(parsed, precision);
|
|
256
|
+
if (clampOnBlur) next = clamp(next, min, max);
|
|
257
|
+
setVal(next);
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
261
|
+
setRawInput(e.target.value);
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
const handleKeyDown = (e: KeyboardEvent<HTMLInputElement>) => {
|
|
265
|
+
if (e.key === 'ArrowUp') { e.preventDefault(); step_(1); }
|
|
266
|
+
if (e.key === 'ArrowDown') { e.preventDefault(); step_(-1); }
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
const handleWheel = (e: WheelEvent<HTMLInputElement>) => {
|
|
270
|
+
if (!allowMouseWheel || !focused) return;
|
|
271
|
+
e.preventDefault();
|
|
272
|
+
step_(e.deltaY < 0 ? 1 : -1);
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
const displayValue = focused ? rawInput : formatValue(numericValue, format, currency, locale, precision);
|
|
276
|
+
|
|
277
|
+
const showAdornment = (safePrefix || safeSuffix) && !focused;
|
|
278
|
+
|
|
279
|
+
return (
|
|
280
|
+
<div className={cx(tkx('flex flex-col gap-1 w-full'), className)} style={style}>
|
|
281
|
+
{safeLabel && (
|
|
282
|
+
<label
|
|
283
|
+
htmlFor={id}
|
|
284
|
+
className={tkx('text-sm font-medium font-sans')}
|
|
285
|
+
style={{ color: theme.text }}
|
|
286
|
+
>
|
|
287
|
+
{safeLabel}
|
|
288
|
+
</label>
|
|
289
|
+
)}
|
|
290
|
+
|
|
291
|
+
<div
|
|
292
|
+
className={tkx('flex items-stretch overflow-hidden rounded-lg transition-colors duration-150')}
|
|
293
|
+
style={{
|
|
294
|
+
border: `1.5px solid ${borderColor}`,
|
|
295
|
+
backgroundColor: isDisabled ? theme.surfaceAlt : theme.surface,
|
|
296
|
+
opacity: isDisabled ? 0.65 : 1,
|
|
297
|
+
height: sz.height,
|
|
298
|
+
}}
|
|
299
|
+
>
|
|
300
|
+
{/* Decrement */}
|
|
301
|
+
<StepperBtn
|
|
302
|
+
direction="dec"
|
|
303
|
+
isDisabled={isDisabled || isReadOnly}
|
|
304
|
+
btnW={sz.btnW}
|
|
305
|
+
height={sz.height}
|
|
306
|
+
primaryColor={theme.primary}
|
|
307
|
+
borderColor={theme.border}
|
|
308
|
+
textMuted={theme.textMuted}
|
|
309
|
+
iconSz={sz.iconSz}
|
|
310
|
+
onStep={step_}
|
|
311
|
+
/>
|
|
312
|
+
|
|
313
|
+
{/* Input area */}
|
|
314
|
+
<div className={tkx('relative flex-1 flex items-center overflow-hidden')}>
|
|
315
|
+
{showAdornment && safePrefix && (
|
|
316
|
+
<span
|
|
317
|
+
style={{
|
|
318
|
+
position: 'absolute',
|
|
319
|
+
left: sz.px,
|
|
320
|
+
fontSize: sz.fontSize,
|
|
321
|
+
color: theme.textMuted,
|
|
322
|
+
pointerEvents: 'none',
|
|
323
|
+
userSelect: 'none',
|
|
324
|
+
}}
|
|
325
|
+
>
|
|
326
|
+
{safePrefix}
|
|
327
|
+
</span>
|
|
328
|
+
)}
|
|
329
|
+
<input
|
|
330
|
+
id={id}
|
|
331
|
+
type="text"
|
|
332
|
+
inputMode="decimal"
|
|
333
|
+
value={focused ? rawInput : displayValue}
|
|
334
|
+
readOnly={isReadOnly}
|
|
335
|
+
disabled={isDisabled}
|
|
336
|
+
aria-invalid={hasError}
|
|
337
|
+
aria-describedby={describedBy}
|
|
338
|
+
aria-valuemin={min}
|
|
339
|
+
aria-valuemax={max}
|
|
340
|
+
aria-valuenow={numericValue}
|
|
341
|
+
style={{
|
|
342
|
+
width: '100%',
|
|
343
|
+
height: '100%',
|
|
344
|
+
border: 'none',
|
|
345
|
+
background: 'transparent',
|
|
346
|
+
outline: 'none',
|
|
347
|
+
textAlign: 'center',
|
|
348
|
+
fontSize: sz.fontSize,
|
|
349
|
+
fontFamily: 'inherit',
|
|
350
|
+
color: theme.text,
|
|
351
|
+
paddingLeft: showAdornment && safePrefix ? `calc(${sz.px} + 1.2em)` : sz.px,
|
|
352
|
+
paddingRight: showAdornment && safeSuffix ? `calc(${sz.px} + 1.2em)` : sz.px,
|
|
353
|
+
cursor: isDisabled ? 'not-allowed' : isReadOnly ? 'default' : 'text',
|
|
354
|
+
}}
|
|
355
|
+
onFocus={handleFocus}
|
|
356
|
+
onBlur={handleBlur}
|
|
357
|
+
onChange={handleChange}
|
|
358
|
+
onKeyDown={handleKeyDown}
|
|
359
|
+
onWheel={handleWheel}
|
|
360
|
+
/>
|
|
361
|
+
{showAdornment && safeSuffix && (
|
|
362
|
+
<span
|
|
363
|
+
style={{
|
|
364
|
+
position: 'absolute',
|
|
365
|
+
right: sz.px,
|
|
366
|
+
fontSize: sz.fontSize,
|
|
367
|
+
color: theme.textMuted,
|
|
368
|
+
pointerEvents: 'none',
|
|
369
|
+
userSelect: 'none',
|
|
370
|
+
}}
|
|
371
|
+
>
|
|
372
|
+
{safeSuffix}
|
|
373
|
+
</span>
|
|
374
|
+
)}
|
|
375
|
+
</div>
|
|
376
|
+
|
|
377
|
+
{/* Increment */}
|
|
378
|
+
<StepperBtn
|
|
379
|
+
direction="inc"
|
|
380
|
+
isDisabled={isDisabled || isReadOnly}
|
|
381
|
+
btnW={sz.btnW}
|
|
382
|
+
height={sz.height}
|
|
383
|
+
primaryColor={theme.primary}
|
|
384
|
+
borderColor={theme.border}
|
|
385
|
+
textMuted={theme.textMuted}
|
|
386
|
+
iconSz={sz.iconSz}
|
|
387
|
+
onStep={step_}
|
|
388
|
+
/>
|
|
389
|
+
</div>
|
|
390
|
+
|
|
391
|
+
{safeHint && !safeError && (
|
|
392
|
+
<span id={hintId} className={tkx('text-xs')} style={{ color: theme.textMuted }}>
|
|
393
|
+
{safeHint}
|
|
394
|
+
</span>
|
|
395
|
+
)}
|
|
396
|
+
{safeError && (
|
|
397
|
+
<span id={errorId} role="alert" className={tkx('text-xs flex items-center gap-1')} style={{ color: theme.danger }}>
|
|
398
|
+
<svg width="12" height="12" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">
|
|
399
|
+
<path d="M12 2C6.477 2 2 6.477 2 12s4.477 10 10 10 10-4.477 10-10S17.523 2 12 2zm1 15h-2v-2h2v2zm0-4h-2V7h2v6z" />
|
|
400
|
+
</svg>
|
|
401
|
+
{safeError}
|
|
402
|
+
</span>
|
|
403
|
+
)}
|
|
404
|
+
</div>
|
|
405
|
+
);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
TkxNumberInput.displayName = 'TkxNumberInput';
|