@tallyui/components 3.12.0 → 3.14.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/dist/index.d.ts +466 -28
- package/dist/index.js +3194 -1574
- package/package.json +9 -9
- package/src/cart/cart-columns.tsx +57 -0
- package/src/cart/cart-line.tsx +32 -17
- package/src/cart/index.ts +1 -0
- package/src/checkout/cash-tendered.tsx +5 -7
- package/src/fonts/font-provider.tsx +65 -0
- package/src/fonts/index.ts +1 -0
- package/src/index.ts +40 -2
- package/src/lib/error-text.ts +5 -0
- package/src/order/index.ts +2 -0
- package/src/order/refund-form.tsx +205 -0
- package/src/order/refund-modal.tsx +32 -0
- package/src/product/category-tile.tsx +43 -0
- package/src/product/in-cart-badge.tsx +26 -0
- package/src/product/index.ts +2 -0
- package/src/register/closure-sheet.tsx +13 -2
- package/src/register/handover-slip.tsx +46 -0
- package/src/register/index.ts +2 -0
- package/src/register/movement-sheet.tsx +2 -1
- package/src/register/open-register-card.tsx +2 -1
- package/src/register/register-bar.tsx +57 -11
- package/src/register/register-column.tsx +2 -1
- package/src/register/register-count.tsx +30 -11
- package/src/register/register-panel.tsx +45 -4
- package/src/register/switch-confirm.tsx +102 -0
- package/src/reports/cards.tsx +197 -0
- package/src/reports/filters-bar.tsx +95 -0
- package/src/reports/hero.tsx +111 -0
- package/src/reports/index.ts +4 -0
- package/src/reports/sales-room.tsx +28 -0
- package/src/sale/cart.tsx +37 -21
- package/src/sale/catalogue.tsx +34 -5
- package/src/sale/customer-display-screen.tsx +92 -0
- package/src/sale/index.ts +5 -2
- package/src/sale/open-sale-tabs.tsx +67 -0
- package/src/sale/order-reference.ts +3 -0
- package/src/sale/orders-list.tsx +4 -2
- package/src/sale/parked-sales.tsx +2 -1
- package/src/sale/price-form.tsx +28 -4
- package/src/sale/product-filter-bar.tsx +58 -0
- package/src/sale/receipt.tsx +8 -2
- package/src/sale/tender-pane.tsx +179 -0
- package/src/sale/tender.tsx +20 -35
- package/src/scale/index.ts +3 -0
- package/src/scale/scale-provider.tsx +70 -0
- package/src/scale/use-pointer.ts +23 -0
- package/src/settings/display-settings.tsx +122 -0
- package/src/settings/index.ts +1 -0
- package/src/shell/app-shell.tsx +71 -0
- package/src/shell/index.ts +4 -0
- package/src/shell/nav-drawer.tsx +130 -0
- package/src/shell/nav-item.tsx +70 -0
- package/src/shell/nav-rail.tsx +58 -0
- package/src/shell/shell-context.ts +18 -0
- package/src/ui/icon/menu-icon.tsx +10 -0
- package/src/ui/text/font-text.tsx +25 -0
- package/src/ui/text/index.tsx +5 -4
- package/src/sale/split-tender.tsx +0 -81
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
// Port of WCPOS v2 `packages/core/src/screens/main/components/drawer-content/index.tsx` (the `front`
|
|
2
|
+
// presentation) and the drawer options of `apps/main/app/(app)/(drawer)/_layout.tsx`, MIT, WCPOS.
|
|
3
|
+
import * as React from 'react';
|
|
4
|
+
import { Animated, Platform, Pressable, View } from 'react-native';
|
|
5
|
+
import { NavButton, splitGroups, type ShellNavItem } from './nav-item';
|
|
6
|
+
|
|
7
|
+
/** v2's phone drawer width, a literal there too (`drawerStyle.width: phone ? 200 : railWidth`). */
|
|
8
|
+
const DRAWER_WIDTH = 200;
|
|
9
|
+
const SLIDE_MS = 200;
|
|
10
|
+
const FOCUSABLE = 'button, [href], input, [tabindex]:not([tabindex="-1"])';
|
|
11
|
+
|
|
12
|
+
export interface NavDrawerProps {
|
|
13
|
+
open: boolean;
|
|
14
|
+
onOpenChange: (open: boolean) => void;
|
|
15
|
+
items: ShellNavItem[];
|
|
16
|
+
activeId?: string;
|
|
17
|
+
onNavigate: (id: string) => void;
|
|
18
|
+
/** Drawn above the items (a store name or logo). */
|
|
19
|
+
header?: React.ReactNode;
|
|
20
|
+
testID?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Keeps Tab inside `panel`, moves focus in on open and hands it back on close (web only). */
|
|
24
|
+
function useFocusTrap(panel: React.RefObject<View | null>, active: boolean, onEscape: () => void) {
|
|
25
|
+
const escape = React.useRef(onEscape);
|
|
26
|
+
escape.current = onEscape;
|
|
27
|
+
React.useEffect(() => {
|
|
28
|
+
if (!active || Platform.OS !== 'web' || typeof document === 'undefined') return;
|
|
29
|
+
const root = panel.current as unknown as HTMLElement | null;
|
|
30
|
+
const previous = document.activeElement as HTMLElement | null;
|
|
31
|
+
const focusables = () => Array.from(root?.querySelectorAll<HTMLElement>(FOCUSABLE) ?? []);
|
|
32
|
+
focusables()[0]?.focus();
|
|
33
|
+
const onKeyDown = (event: KeyboardEvent) => {
|
|
34
|
+
if (event.key === 'Escape') {
|
|
35
|
+
escape.current();
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
if (event.key !== 'Tab') return;
|
|
39
|
+
const list = focusables();
|
|
40
|
+
if (list.length === 0) return;
|
|
41
|
+
const first = list[0];
|
|
42
|
+
const last = list[list.length - 1];
|
|
43
|
+
const inside = root?.contains(document.activeElement) ?? false;
|
|
44
|
+
if (event.shiftKey && (document.activeElement === first || !inside)) {
|
|
45
|
+
event.preventDefault();
|
|
46
|
+
last.focus();
|
|
47
|
+
} else if (!event.shiftKey && (document.activeElement === last || !inside)) {
|
|
48
|
+
event.preventDefault();
|
|
49
|
+
first.focus();
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
document.addEventListener('keydown', onKeyDown);
|
|
53
|
+
return () => {
|
|
54
|
+
document.removeEventListener('keydown', onKeyDown);
|
|
55
|
+
previous?.focus?.();
|
|
56
|
+
};
|
|
57
|
+
}, [active, panel]);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The phone's navigation: a 200-wide panel that slides over the screen from the left, v2's
|
|
62
|
+
* `drawerType: 'front'`, with a backdrop that closes it. Pressing the current page only closes it.
|
|
63
|
+
*/
|
|
64
|
+
export function NavDrawer({ open, onOpenChange, items, activeId, onNavigate, header, testID = 'nav-drawer' }: NavDrawerProps) {
|
|
65
|
+
const [mounted, setMounted] = React.useState(open);
|
|
66
|
+
const offset = React.useRef(new Animated.Value(open ? 0 : -DRAWER_WIDTH)).current;
|
|
67
|
+
const panel = React.useRef<View>(null);
|
|
68
|
+
if (open && !mounted) setMounted(true);
|
|
69
|
+
|
|
70
|
+
React.useEffect(() => {
|
|
71
|
+
if (!mounted) return;
|
|
72
|
+
const animation = Animated.timing(offset, {
|
|
73
|
+
toValue: open ? 0 : -DRAWER_WIDTH,
|
|
74
|
+
duration: SLIDE_MS,
|
|
75
|
+
useNativeDriver: Platform.OS !== 'web',
|
|
76
|
+
});
|
|
77
|
+
animation.start(({ finished }) => {
|
|
78
|
+
if (finished && !open) setMounted(false);
|
|
79
|
+
});
|
|
80
|
+
return () => animation.stop();
|
|
81
|
+
}, [open, mounted, offset]);
|
|
82
|
+
|
|
83
|
+
const close = React.useCallback(() => onOpenChange(false), [onOpenChange]);
|
|
84
|
+
useFocusTrap(panel, open && mounted, close);
|
|
85
|
+
|
|
86
|
+
if (!mounted) return null;
|
|
87
|
+
const [top, bottom] = splitGroups(items);
|
|
88
|
+
const renderItem = (item: ShellNavItem) => (
|
|
89
|
+
<NavButton
|
|
90
|
+
key={item.id}
|
|
91
|
+
item={item}
|
|
92
|
+
labelled
|
|
93
|
+
active={item.id === activeId}
|
|
94
|
+
onPress={() => {
|
|
95
|
+
if (item.id !== activeId) onNavigate(item.id);
|
|
96
|
+
close();
|
|
97
|
+
}}
|
|
98
|
+
/>
|
|
99
|
+
);
|
|
100
|
+
return (
|
|
101
|
+
<View testID={testID} className="absolute inset-0 z-50" pointerEvents={open ? 'auto' : 'none'}>
|
|
102
|
+
<Animated.View
|
|
103
|
+
className="absolute inset-0"
|
|
104
|
+
style={{ opacity: offset.interpolate({ inputRange: [-DRAWER_WIDTH, 0], outputRange: [0, 1] }) }}
|
|
105
|
+
>
|
|
106
|
+
<Pressable
|
|
107
|
+
testID={`${testID}-backdrop`}
|
|
108
|
+
accessibilityLabel="Close menu"
|
|
109
|
+
className="flex-1 bg-black/50"
|
|
110
|
+
onPress={close}
|
|
111
|
+
/>
|
|
112
|
+
</Animated.View>
|
|
113
|
+
<Animated.View
|
|
114
|
+
ref={panel}
|
|
115
|
+
role="dialog"
|
|
116
|
+
aria-modal
|
|
117
|
+
aria-label="Menu"
|
|
118
|
+
testID={`${testID}-panel`}
|
|
119
|
+
className="absolute bottom-0 left-0 top-0 border-r border-border bg-card shadow-lg"
|
|
120
|
+
style={{ width: DRAWER_WIDTH, transform: [{ translateX: offset }] }}
|
|
121
|
+
>
|
|
122
|
+
{header}
|
|
123
|
+
<View role="navigation" aria-label="Main" className="flex-1 p-2">
|
|
124
|
+
<View>{top.map(renderItem)}</View>
|
|
125
|
+
{bottom.length > 0 && <View className="mt-auto">{bottom.map(renderItem)}</View>}
|
|
126
|
+
</View>
|
|
127
|
+
</Animated.View>
|
|
128
|
+
</View>
|
|
129
|
+
);
|
|
130
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// Port of WCPOS v2 `packages/components/src/nav-item/index.tsx` (the `drawer` size), MIT, WCPOS.
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { Pressable, View, type PressableProps } from 'react-native';
|
|
4
|
+
import { cn } from '@tallyui/theme';
|
|
5
|
+
import { Text } from '../ui/text';
|
|
6
|
+
|
|
7
|
+
export interface ShellNavItem {
|
|
8
|
+
id: string;
|
|
9
|
+
label: string;
|
|
10
|
+
/** An icon element drawn in currentColor (`createSvgIcon`), 24 px. */
|
|
11
|
+
icon: React.ReactNode;
|
|
12
|
+
/** A count or short text on the icon's shoulder; nothing when undefined, 0 or ''. */
|
|
13
|
+
badge?: number | string;
|
|
14
|
+
/** `bottom` items sit in the group pinned to the end of the rail or drawer (v2's `marginTop: 'auto'`). */
|
|
15
|
+
group?: 'top' | 'bottom';
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** The items in their two groups, in the order given: v2's top group and its bottom group. */
|
|
19
|
+
export function splitGroups(items: ShellNavItem[]): [ShellNavItem[], ShellNavItem[]] {
|
|
20
|
+
return [items.filter((item) => item.group !== 'bottom'), items.filter((item) => item.group === 'bottom')];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
type NavButtonProps = Omit<PressableProps, 'children'> & {
|
|
24
|
+
item: ShellNavItem;
|
|
25
|
+
active: boolean;
|
|
26
|
+
/** The drawer's labelled row; the rail draws the icon-only square. */
|
|
27
|
+
labelled?: boolean;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/** One nav entry: `h-12 rounded-lg` as v2's drawer size, lifted onto the primary tint when active. */
|
|
31
|
+
export const NavButton = React.forwardRef<View, NavButtonProps>(function NavButton(
|
|
32
|
+
{ item, active, labelled = false, className, ...props },
|
|
33
|
+
ref,
|
|
34
|
+
) {
|
|
35
|
+
const tint = active ? 'text-primary' : 'text-muted-foreground';
|
|
36
|
+
return (
|
|
37
|
+
<Pressable
|
|
38
|
+
ref={ref}
|
|
39
|
+
role="button"
|
|
40
|
+
aria-current={active ? 'page' : undefined}
|
|
41
|
+
accessibilityLabel={item.label}
|
|
42
|
+
testID={`nav-item-${item.id}`}
|
|
43
|
+
className={cn(
|
|
44
|
+
'h-12 flex-row items-center rounded-lg web:transition-colors',
|
|
45
|
+
labelled ? 'gap-3 px-3' : 'w-12 justify-center',
|
|
46
|
+
active ? 'bg-primary/10' : 'web:hover:bg-accent active:bg-accent',
|
|
47
|
+
'web:focus-visible:outline-none web:focus-visible:ring-2 web:focus-visible:ring-ring',
|
|
48
|
+
typeof className === 'string' ? className : undefined,
|
|
49
|
+
)}
|
|
50
|
+
{...props}
|
|
51
|
+
>
|
|
52
|
+
<View className={cn('h-6 w-6 items-center justify-center', tint)}>
|
|
53
|
+
{item.icon}
|
|
54
|
+
{item.badge !== undefined && item.badge !== 0 && item.badge !== '' && (
|
|
55
|
+
<View
|
|
56
|
+
testID={`nav-item-${item.id}-badge`}
|
|
57
|
+
className="absolute -right-2 -top-1 h-4 min-w-4 items-center justify-center rounded-full bg-destructive px-1"
|
|
58
|
+
>
|
|
59
|
+
<Text className="text-2xs font-semibold text-destructive-foreground">{item.badge}</Text>
|
|
60
|
+
</View>
|
|
61
|
+
)}
|
|
62
|
+
</View>
|
|
63
|
+
{labelled && (
|
|
64
|
+
<Text className={cn('flex-1 text-base', active ? 'font-semibold text-primary' : 'text-foreground')} numberOfLines={1}>
|
|
65
|
+
{item.label}
|
|
66
|
+
</Text>
|
|
67
|
+
)}
|
|
68
|
+
</Pressable>
|
|
69
|
+
);
|
|
70
|
+
});
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// Port of WCPOS v2 `packages/core/src/screens/main/components/drawer-content/index.tsx` (the `rail`
|
|
2
|
+
// presentation) and `drawer-content/rail/index.tsx`, MIT, WCPOS.
|
|
3
|
+
import * as React from 'react';
|
|
4
|
+
import { View } from 'react-native';
|
|
5
|
+
import { cn } from '@tallyui/theme';
|
|
6
|
+
import { Text } from '../ui/text';
|
|
7
|
+
import { NavButton, splitGroups, type ShellNavItem } from './nav-item';
|
|
8
|
+
|
|
9
|
+
export interface NavRailProps {
|
|
10
|
+
items: ShellNavItem[];
|
|
11
|
+
activeId?: string;
|
|
12
|
+
onNavigate: (id: string) => void;
|
|
13
|
+
className?: string;
|
|
14
|
+
testID?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The wide layout's permanent icon column. Its width is 16 spacing units, the `h-12 w-12` item
|
|
19
|
+
* plus 2 units each side (64 at Regular), so it follows the scale step as v2's does. Icon-only:
|
|
20
|
+
* the label shows to the right on hover or keyboard focus, v2's web tooltip.
|
|
21
|
+
*/
|
|
22
|
+
export function NavRail({ items, activeId, onNavigate, className, testID = 'nav-rail' }: NavRailProps) {
|
|
23
|
+
const [hinted, setHinted] = React.useState<string | null>(null);
|
|
24
|
+
const renderItem = (item: ShellNavItem) => (
|
|
25
|
+
<View key={item.id} className="relative z-10">
|
|
26
|
+
<NavButton
|
|
27
|
+
item={item}
|
|
28
|
+
active={item.id === activeId}
|
|
29
|
+
onPress={() => onNavigate(item.id)}
|
|
30
|
+
onHoverIn={() => setHinted(item.id)}
|
|
31
|
+
onHoverOut={() => setHinted((id) => (id === item.id ? null : id))}
|
|
32
|
+
onFocus={() => setHinted(item.id)}
|
|
33
|
+
onBlur={() => setHinted((id) => (id === item.id ? null : id))}
|
|
34
|
+
/>
|
|
35
|
+
{hinted === item.id && (
|
|
36
|
+
<View
|
|
37
|
+
testID={`nav-rail-label-${item.id}`}
|
|
38
|
+
pointerEvents="none"
|
|
39
|
+
className="absolute left-full top-2 z-50 ml-3 rounded-md border border-border bg-popover px-3 py-1.5 shadow-md"
|
|
40
|
+
>
|
|
41
|
+
<Text className="text-sm text-popover-foreground" numberOfLines={1}>{item.label}</Text>
|
|
42
|
+
</View>
|
|
43
|
+
)}
|
|
44
|
+
</View>
|
|
45
|
+
);
|
|
46
|
+
const [top, bottom] = splitGroups(items);
|
|
47
|
+
return (
|
|
48
|
+
<View
|
|
49
|
+
role="navigation"
|
|
50
|
+
aria-label="Main"
|
|
51
|
+
testID={testID}
|
|
52
|
+
className={cn('z-10 w-16 items-center border-r border-border bg-card py-2', className)}
|
|
53
|
+
>
|
|
54
|
+
<View className="gap-2">{top.map(renderItem)}</View>
|
|
55
|
+
{bottom.length > 0 && <View className="mt-auto gap-2">{bottom.map(renderItem)}</View>}
|
|
56
|
+
</View>
|
|
57
|
+
);
|
|
58
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* What a bar inside the shell reads: v2's `useBarMenu` (`packages/core/src/screens/main/components/
|
|
5
|
+
* bar-controls/index.tsx`, MIT, WCPOS) gives the menu only below the wide boundary, where there is
|
|
6
|
+
* a drawer to open; on wide the rail is always on screen.
|
|
7
|
+
*/
|
|
8
|
+
export interface ShellState {
|
|
9
|
+
phone: boolean;
|
|
10
|
+
openMenu?: () => void;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const ShellContext = React.createContext<ShellState | null>(null);
|
|
14
|
+
|
|
15
|
+
/** The enclosing `AppShell`'s state, or `null` outside one. */
|
|
16
|
+
export function useShell(): ShellState | null {
|
|
17
|
+
return React.useContext(ShellContext);
|
|
18
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Line } from 'react-native-svg';
|
|
2
|
+
import { createSvgIcon } from './svg-icon';
|
|
3
|
+
|
|
4
|
+
export const MenuIcon = createSvgIcon('MenuIcon', () => (
|
|
5
|
+
<>
|
|
6
|
+
<Line x1={4} y1={6} x2={20} y2={6} />
|
|
7
|
+
<Line x1={4} y1={12} x2={20} y2={12} />
|
|
8
|
+
<Line x1={4} y1={18} x2={20} y2={18} />
|
|
9
|
+
</>
|
|
10
|
+
));
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { Platform, type TextProps } from 'react-native';
|
|
3
|
+
import { fontFamilyStyle } from '@tallyui/theme';
|
|
4
|
+
import { useFontType } from '../../fonts';
|
|
5
|
+
|
|
6
|
+
const wrappers = new Map<React.ElementType, React.ComponentType<TextProps>>();
|
|
7
|
+
|
|
8
|
+
/** Wraps a text component (RN Text or Slot) so it draws the active type set's face. Cached: the same Base always returns the same wrapper. */
|
|
9
|
+
export function withFont<C extends React.ElementType>(Base: C): C {
|
|
10
|
+
let Wrapper = wrappers.get(Base);
|
|
11
|
+
if (!Wrapper) {
|
|
12
|
+
Wrapper = function FontText({ className, style, ...props }: TextProps) {
|
|
13
|
+
const { type } = useFontType();
|
|
14
|
+
const family = /(^|\s)font-(mono|sans|serif)(\s|$)/.test(className ?? '')
|
|
15
|
+
? undefined
|
|
16
|
+
: /(^|\s)tabular-nums(\s|$)/.test(className ?? '') ? type.num ?? type.body : type.body;
|
|
17
|
+
return React.createElement(Base, {
|
|
18
|
+
...props, className,
|
|
19
|
+
style: family ? [fontFamilyStyle(family, Platform.OS === 'web'), style] : style,
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
wrappers.set(Base, Wrapper);
|
|
23
|
+
}
|
|
24
|
+
return Wrapper as unknown as C;
|
|
25
|
+
}
|
package/src/ui/text/index.tsx
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import { Text as RNText } from 'react-native';
|
|
3
3
|
import { Slot, TextClassContext } from '@tallyui/primitives';
|
|
4
|
-
import { cn } from '@tallyui/theme';
|
|
4
|
+
import { cn, MAX_FONT_SCALE } from '@tallyui/theme';
|
|
5
5
|
import { cva, type VariantProps } from 'class-variance-authority';
|
|
6
|
+
import { withFont } from './font-text';
|
|
6
7
|
|
|
7
8
|
const textVariants = cva('text-foreground web:select-text text-base', {
|
|
8
9
|
variants: {
|
|
@@ -21,12 +22,12 @@ type TextProps = React.ComponentPropsWithoutRef<typeof RNText> &
|
|
|
21
22
|
asChild?: boolean;
|
|
22
23
|
};
|
|
23
24
|
|
|
24
|
-
function Text({ className, variant, asChild = false, children, ...props }: TextProps) {
|
|
25
|
+
function Text({ className, variant, asChild = false, children, maxFontSizeMultiplier = MAX_FONT_SCALE, ...props }: TextProps) {
|
|
25
26
|
const textClass = React.useContext(TextClassContext);
|
|
26
|
-
const Component = asChild ? Slot : RNText;
|
|
27
|
+
const Component = withFont(asChild ? Slot : RNText);
|
|
27
28
|
|
|
28
29
|
return (
|
|
29
|
-
<Component className={cn(textVariants({ variant }), textClass, className)} {...props}>
|
|
30
|
+
<Component className={cn(textVariants({ variant }), textClass, className)} {...props} maxFontSizeMultiplier={maxFontSizeMultiplier}>
|
|
30
31
|
{children}
|
|
31
32
|
</Component>
|
|
32
33
|
);
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { useState, type JSX } from 'react';
|
|
2
|
-
import { Pressable, Text, TextInput, View } from 'react-native';
|
|
3
|
-
import { formatMoney, minorUnitDigits } from '@tallyui/core';
|
|
4
|
-
import type { useSale } from '@tallyui/pos';
|
|
5
|
-
import { tenderLabel } from '../register/register-count';
|
|
6
|
-
import { parsePrice } from './price-form';
|
|
7
|
-
|
|
8
|
-
export function SplitTender({ sale }: { sale: ReturnType<typeof useSale> }): JSX.Element | null {
|
|
9
|
-
const { order, stage } = sale;
|
|
10
|
-
const [method, setMethod] = useState<'cash' | 'external'>('cash');
|
|
11
|
-
const [amount, setAmount] = useState<string | null>(null);
|
|
12
|
-
const [reference, setReference] = useState('');
|
|
13
|
-
const [error, setError] = useState<string | null>(null);
|
|
14
|
-
if (stage.kind !== 'tender') return null;
|
|
15
|
-
const money = (value: number) => formatMoney({ amount: value, currency: order.currency });
|
|
16
|
-
const digits = minorUnitDigits(order.currency);
|
|
17
|
-
const amountText = amount ?? (order.balanceDueMinor / 10 ** digits).toFixed(digits);
|
|
18
|
-
const incomplete = order.balanceDueMinor > 0 || order.payments.length === 0;
|
|
19
|
-
function add() {
|
|
20
|
-
const parsed = parsePrice(amountText, order.currency);
|
|
21
|
-
if (typeof parsed === 'string') { setError(parsed); return; }
|
|
22
|
-
const id = sale.addTender({ method, amountMinor: parsed,
|
|
23
|
-
...(method === 'external' && reference ? { reference } : {}) });
|
|
24
|
-
if (id === null) {
|
|
25
|
-
setError("That tender wasn't added: nothing is due, or the amount isn't valid.");
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
|
-
setError(null);
|
|
29
|
-
setReference('');
|
|
30
|
-
setAmount(null);
|
|
31
|
-
}
|
|
32
|
-
return <View dataSet={{ print: 'hide' }} className="gap-4 p-4">
|
|
33
|
-
<View testID="split-tender-summary" className="gap-2">
|
|
34
|
-
<Text className="text-foreground">Total: {money(order.totalMinor)}</Text>
|
|
35
|
-
<Text className="text-foreground">Paid: {money(order.paidMinor)}</Text>
|
|
36
|
-
<Text className="text-foreground">Remaining: {money(order.balanceDueMinor)}</Text>
|
|
37
|
-
{order.changeDueMinor > 0 ? <Text className="text-foreground">Change: {money(order.changeDueMinor)}</Text> : null}
|
|
38
|
-
</View>
|
|
39
|
-
<View testID="split-tender-list" className="gap-2">
|
|
40
|
-
{order.payments.map((payment) => <View key={payment.id} testID={`split-tender-row-${payment.id}`} className="gap-2">
|
|
41
|
-
<Text className="text-foreground">{tenderLabel(payment.method)} {money(payment.amountMinor)}</Text>
|
|
42
|
-
{payment.reference ? <Text className="text-foreground">{payment.reference}</Text> : null}
|
|
43
|
-
<Pressable testID={`split-tender-remove-${payment.id}`} accessibilityRole="button" accessibilityLabel="Remove tender"
|
|
44
|
-
onPress={() => sale.removeTender(payment.id)} className="rounded-md border border-border bg-card px-4 py-3">
|
|
45
|
-
<Text className="text-center text-foreground">Remove</Text>
|
|
46
|
-
</Pressable>
|
|
47
|
-
</View>)}
|
|
48
|
-
</View>
|
|
49
|
-
<View testID="split-tender-add" className="gap-2">
|
|
50
|
-
<View className="flex-row gap-2">
|
|
51
|
-
{(['cash', 'external'] as const).map((value) => <Pressable key={value}
|
|
52
|
-
testID={`split-tender-method-${value === 'cash' ? 'cash' : 'card'}`} accessibilityRole="button"
|
|
53
|
-
accessibilityState={{ selected: method === value }} onPress={() => setMethod(value)}
|
|
54
|
-
className={`rounded-md border border-border px-4 py-3 ${method === value ? 'bg-primary' : 'bg-card'}`}>
|
|
55
|
-
<Text className={`text-center ${method === value ? 'text-primary-foreground' : 'text-foreground'}`}>{tenderLabel(value)}</Text>
|
|
56
|
-
</Pressable>)}
|
|
57
|
-
</View>
|
|
58
|
-
<TextInput testID="split-tender-amount" accessibilityLabel="Tender amount" inputMode="decimal"
|
|
59
|
-
value={amountText} onChangeText={setAmount} className="rounded-md border border-border px-3 py-2 text-foreground" />
|
|
60
|
-
{method === 'external' ? <TextInput testID="split-tender-reference" accessibilityLabel="Terminal reference"
|
|
61
|
-
placeholder="Terminal reference" maxLength={255} value={reference} onChangeText={setReference}
|
|
62
|
-
className="rounded-md border border-border px-3 py-2 text-foreground" /> : null}
|
|
63
|
-
{error ? <Text testID="split-tender-error" accessibilityRole="alert" className="text-destructive">{error}</Text> : null}
|
|
64
|
-
<Pressable testID="split-tender-add-button" accessibilityRole="button" onPress={add} className="rounded-md bg-primary px-4 py-3">
|
|
65
|
-
<Text className="text-center font-semibold text-primary-foreground">Add</Text>
|
|
66
|
-
</Pressable>
|
|
67
|
-
</View>
|
|
68
|
-
<Pressable testID="split-tender-complete" accessibilityRole="button" disabled={incomplete} onPress={sale.complete}
|
|
69
|
-
className={`rounded-md bg-primary px-4 py-3 ${incomplete ? 'opacity-50' : ''}`}>
|
|
70
|
-
<Text className="text-center font-semibold text-primary-foreground">Complete sale</Text>
|
|
71
|
-
</Pressable>
|
|
72
|
-
{sale.error ? <Text accessibilityRole="alert" className="text-destructive">{sale.error}</Text> : null}
|
|
73
|
-
{sale.canContinue ? <>
|
|
74
|
-
<Text className="text-foreground">This sale is stored and will be sent. Continue to the next sale.</Text>
|
|
75
|
-
<Pressable accessibilityRole="button" onPress={() => sale.continueSale()} className="rounded-md bg-primary px-4 py-3">
|
|
76
|
-
<Text className="text-center font-semibold text-primary-foreground">Continue</Text>
|
|
77
|
-
</Pressable>
|
|
78
|
-
</> : null}
|
|
79
|
-
<Pressable accessibilityRole="button" onPress={sale.cancelTender} className="rounded-md border border-border bg-card px-4 py-3"><Text className="text-center text-foreground">Back</Text></Pressable>
|
|
80
|
-
</View>;
|
|
81
|
-
}
|