@tallyui/components 3.14.0 → 3.16.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.
@@ -19,9 +19,24 @@ const button = 'rounded-md border border-border px-3 py-2 min-h-11 items-center
19
19
  const QUICK_KEYS = 3;
20
20
 
21
21
  /** `currentMinor` less `percent` (to two places), half up at the minor unit, as v2's `draftPrice` rounds. */
22
- const lessPercent = (currentMinor: number, percent: number) =>
22
+ export const lessPercent = (currentMinor: number, percent: number) =>
23
23
  Math.round(currentMinor * (10000 - Math.round(percent * 100)) / 10000);
24
24
 
25
+ /** The quick keys drawn: the first three valid percentages (0 < n ≤ 100). */
26
+ export const quickKeys = (list: readonly number[] = []) => list.filter((n) => Number.isFinite(n) && n > 0 && n <= 100).slice(0, QUICK_KEYS);
27
+
28
+ /** v2's quick-discount key row; `percent` is the lit key. Shared by PriceForm and LinePopover. */
29
+ export function QuickDiscountKeys({ keys, percent, onPress }: { keys: readonly number[]; percent?: number; onPress: (key: number) => void }) {
30
+ return <View testID="price-quick-discounts" className="flex-row gap-2">
31
+ {keys.map((key) => <Pressable key={key} testID={`price-discount-${key}`} accessibilityRole="button"
32
+ accessibilityLabel={`Discount (${key}%)`} accessibilityState={{ selected: key === percent }}
33
+ aria-selected={key === percent} onPress={() => onPress(key)}
34
+ className={`${button} flex-1 ${key === percent ? 'bg-primary' : 'bg-card'}`}>
35
+ <Text className={key === percent ? 'text-primary-foreground' : 'text-foreground'}>{`−${key}%`}</Text>
36
+ </Pressable>)}
37
+ </View>;
38
+ }
39
+
25
40
  /** `onApply` returns a refusal to show inline, or null once applied. */
26
41
  export function PriceForm({ lineName, currency, currentMinor, onApply, onClose, quickDiscounts = [] }: {
27
42
  lineName: string; currency: string; currentMinor: number;
@@ -34,7 +49,7 @@ export function PriceForm({ lineName, currency, currentMinor, onApply, onClose,
34
49
  const [error, setError] = useState<string | null>(null);
35
50
  // The lit key (v2's reducer): pressing it again puts the current price back; typing starts over.
36
51
  const [percent, setPercent] = useState<number | undefined>();
37
- const keys = quickDiscounts.filter((n) => Number.isFinite(n) && n > 0 && n <= 100).slice(0, QUICK_KEYS);
52
+ const keys = quickKeys(quickDiscounts);
38
53
  const title = `Price for ${lineName}`;
39
54
  const current = formatMoney({ amount: currentMinor, currency });
40
55
  const draft = percent === undefined ? undefined : lessPercent(currentMinor, percent);
@@ -49,14 +64,8 @@ export function PriceForm({ lineName, currency, currentMinor, onApply, onClose,
49
64
  {draft === undefined ? <Text className="text-muted-foreground">{current}</Text>
50
65
  : <Text testID="price-quick-draft" className="text-muted-foreground">
51
66
  {`${current} −${percent}% → ${formatMoney({ amount: draft, currency })}`}</Text>}
52
- {keys.length ? <View testID="price-quick-discounts" className="flex-row gap-2">
53
- {keys.map((key) => <Pressable key={key} testID={`price-discount-${key}`} accessibilityRole="button"
54
- accessibilityLabel={`Discount (${key}%)`} accessibilityState={{ selected: key === percent }}
55
- aria-selected={key === percent} onPress={() => { setPercent(key === percent ? undefined : key); setText(''); setError(null); }}
56
- className={`${button} flex-1 ${key === percent ? 'bg-primary' : 'bg-card'}`}>
57
- <Text className={key === percent ? 'text-primary-foreground' : 'text-foreground'}>{`−${key}%`}</Text>
58
- </Pressable>)}
59
- </View> : null}
67
+ {keys.length ? <QuickDiscountKeys keys={keys} percent={percent}
68
+ onPress={(key) => { setPercent(key === percent ? undefined : key); setText(''); setError(null); }} /> : null}
60
69
  <TextInput testID="price-input" accessibilityLabel="Price value" value={text}
61
70
  onChangeText={(next) => { setText(next); setPercent(undefined); }}
62
71
  onSubmitEditing={apply} inputMode="decimal" placeholder={currency}
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * The tender pane, laid out as WCPOS v2's (monorepo main, packages/core/src/screens/main/pos/checkout/tender/):
3
3
  * BalanceHeadline and LedgerLegs from ledger-pane.tsx, then the method tiles, entry, quick-cash chips,
4
- * keypad and commit from tender-pane.tsx's TenderKeypad. Copy is v2's English catalogue. Every figure
4
+ * keypad and commit from tender-pane.tsx's TenderKeypad. Copy is read with useT, under v2's catalogue keys. Every figure
5
5
  * comes from pos (useSale's captured-only totals, tenderRounding, quickTenderedAmounts, planLegs).
6
6
  */
7
7
  import { useEffect, useState, type JSX } from 'react';
@@ -13,6 +13,8 @@ import {
13
13
  } from '@tallyui/pos';
14
14
  import { cn } from '@tallyui/theme';
15
15
  import { QUICK_CASH_STEPS } from '../checkout/cash-tendered';
16
+ import { useT } from '../i18n';
17
+ import { DIGIT_ROWS, Keypad } from '../input/keypad';
16
18
 
17
19
  type Sale = ReturnType<typeof useSale>;
18
20
  type Leg = Sale['order']['payments'][number];
@@ -28,19 +30,18 @@ export interface TenderPaneProps {
28
30
 
29
31
  /** order.create's bound on `payments[].reference`, in UTF-16 units. */
30
32
  const REFERENCE_MAX = 255;
31
- /** v2 labels.ts statusLabel. */
33
+ /** v2 labels.ts statusLabel, as catalogue keys. */
32
34
  const STATUS_LABEL: Record<PaymentLegStatus, string> = {
33
- pending: 'Pending', authorized: 'Authorized', captured: 'Captured', failed: 'Failed', voided: 'Voided',
35
+ pending: 'pos_checkout.status_pending', authorized: 'pos_checkout.status_authorized', captured: 'pos_checkout.status_captured',
36
+ failed: 'pos_checkout.status_failed', voided: 'pos_checkout.status_voided',
34
37
  };
35
38
  /** v2 labels.ts statusVariant: coloured by what the cashier has to do about the leg. */
36
39
  const STATUS_TONE: Record<PaymentLegStatus, string> = {
37
40
  pending: 'text-muted-foreground', authorized: 'text-success', captured: 'text-success', failed: 'text-destructive', voided: 'text-muted-foreground',
38
41
  };
39
42
  /** v2 tender-pane.tsx keypadRows: 123 / 456 / 789, then C, 0, backspace. No decimal key: digits shift in from the right. */
40
- const KEYPAD_ROWS: { key: TenderKey; label: string; name?: string }[][] = [
41
- ...['123', '456', '789'].map((row) => [...row].map((digit) => ({ key: digit as TenderKey, label: digit }))),
42
- [{ key: 'clear', label: 'C', name: 'Clear' }, { key: '0', label: '0' }, { key: 'backspace', label: '⌫', name: 'Delete last digit' }],
43
- ];
43
+ const keypadRows = (t: ReturnType<typeof useT>) => [...DIGIT_ROWS,
44
+ [{ name: 'clear', label: t('pos_checkout.clear_amount'), text: 'C' }, { name: '0', label: '0' }, { name: 'backspace', label: t('common.backspace'), text: '⌫' }]];
44
45
  const button = 'min-h-11 justify-center rounded-md border border-border bg-card px-4 py-2';
45
46
 
46
47
  /** The captured legs since the plan began, as the reducer's `tender-recorded` and `planLegs` read them. */
@@ -49,8 +50,9 @@ const rowsSince = (payments: readonly Leg[], from: number) =>
49
50
 
50
51
  export function TenderPane({ sale, state, dispatch, methods }: TenderPaneProps): JSX.Element | null {
51
52
  const { order, stage } = sale;
53
+ const t = useT();
52
54
  const [reference, setReference] = useState('');
53
- const [refusal, setRefusal] = useState<string | null>(null);
55
+ const [refusal, setRefusal] = useState(false);
54
56
  const balance = order.balanceDueMinor;
55
57
  // The ledger changed (a leg taken or voided) and the entry is untouched: pre-type the next leg or plan share.
56
58
  useEffect(() => {
@@ -77,9 +79,9 @@ export function TenderPane({ sale, state, dispatch, methods }: TenderPaneProps):
77
79
  const [onlyLeg] = order.payments;
78
80
  // startTender('external') records the card leg for the total: its reference is edited in place, as before.
79
81
  const cardReference = order.payments.length === 1 && onlyLeg.method === 'external' && legStatus(onlyLeg) === 'captured' && balance === 0;
80
- const hint = noChange ? `Only ${money(balance)} is due — ${method?.title} can't give change`
81
- : entryChange > 0 ? `Change ${money(entryChange)}`
82
- : figures.appliedMinor < balance && figures.entryMinor > 0 ? `Part payment · ${money(left)} left after this` : '';
82
+ const hint = noChange ? t('pos_checkout.no_change_for_method', { amount: money(balance), method: method?.title })
83
+ : entryChange > 0 ? t('pos_checkout.change_due', { amount: money(entryChange) })
84
+ : figures.appliedMinor < balance && figures.entryMinor > 0 ? t('pos_checkout.part_payment_left', { left: money(left) }) : '';
83
85
  const canComplete = balance === 0 && !order.payments.some(isInMotion);
84
86
  const cannotTake = figures.appliedMinor <= 0 || noChange;
85
87
 
@@ -88,22 +90,22 @@ export function TenderPane({ sale, state, dispatch, methods }: TenderPaneProps):
88
90
  const id = method.id === 'cash'
89
91
  ? sale.addTender({ method: 'cash', amountMinor: figures.entryMinor })
90
92
  : sale.addTender({ method: 'external', amountMinor: figures.appliedMinor, ...(reference ? { reference } : {}) });
91
- if (id === null) { setRefusal("That payment wasn't taken: nothing is due, or the amount isn't valid."); return; }
92
- setRefusal(null);
93
+ if (id === null) { setRefusal(true); return; }
94
+ setRefusal(false);
93
95
  setReference('');
94
96
  dispatch({ type: 'tender-started' });
95
97
  }
96
98
 
97
99
  return <View dataSet={{ print: 'hide' }} testID="tender-pane" className="gap-4 p-4">
98
100
  <View testID="tender-headline" className="gap-1">
99
- <Text className="text-sm text-muted-foreground">{order.paidMinor > 0 ? 'Remaining' : 'Total'}</Text>
101
+ <Text className="text-sm text-muted-foreground">{order.paidMinor > 0 ? t('pos_checkout.remaining') : t('common.total')}</Text>
100
102
  <Text testID="tender-balance" className="text-4xl font-bold text-foreground">{money(balance)}</Text>
101
- {order.paidMinor > 0 ? <Text testID="tender-paid" className="text-sm text-muted-foreground">{`${money(order.paidMinor)} of ${money(order.totalMinor)} paid`}</Text> : null}
102
- {order.changeDueMinor > 0 ? <Text testID="tender-change" className="text-lg font-semibold text-success">{`Change ${money(order.changeDueMinor)}`}</Text> : null}
103
+ {order.paidMinor > 0 ? <Text testID="tender-paid" className="text-sm text-muted-foreground">{t('pos_checkout.paid_of_total', { paid: money(order.paidMinor), total: money(order.totalMinor) })}</Text> : null}
104
+ {order.changeDueMinor > 0 ? <Text testID="tender-change" className="text-lg font-semibold text-success">{t('pos_checkout.change_due', { amount: money(order.changeDueMinor) })}</Text> : null}
103
105
  </View>
104
106
 
105
107
  <View testID="tender-ledger" className="gap-1">
106
- {order.payments.length === 0 ? <Text className="text-muted-foreground">No payments yet</Text> : order.payments.map((leg) => {
108
+ {order.payments.length === 0 ? <Text className="text-muted-foreground">{t('pos_checkout.no_payments_yet')}</Text> : order.payments.map((leg) => {
107
109
  const status = legStatus(leg);
108
110
  const voided = status === 'voided';
109
111
  const descriptor = descriptorOf(leg.method);
@@ -111,20 +113,20 @@ export function TenderPane({ sale, state, dispatch, methods }: TenderPaneProps):
111
113
  <View className="flex-row items-center gap-2">
112
114
  <Text className={cn('flex-1 font-semibold text-foreground', voided && 'line-through')}>{descriptor?.title ?? leg.method}</Text>
113
115
  <Text className={cn('text-foreground', voided && 'line-through')}>{money(leg.amountMinor)}</Text>
114
- <Text testID={`tender-leg-${leg.id}-status`} className={cn('text-sm font-semibold', STATUS_TONE[status])}>{voided ? 'Cancelled' : STATUS_LABEL[status]}</Text>
116
+ <Text testID={`tender-leg-${leg.id}-status`} className={cn('text-sm font-semibold', STATUS_TONE[status])}>{voided ? t('pos_checkout.cancelled') : t(STATUS_LABEL[status])}</Text>
115
117
  </View>
116
118
  {leg.reference ? <Text className="text-sm text-muted-foreground">{leg.reference}</Text> : null}
117
- {leg === firstPending ? <Text className="text-sm text-muted-foreground">Next</Text> : null}
118
- {descriptor && canVoidLeg(leg, descriptor) ? <Pressable testID={`tender-leg-${leg.id}-void`} accessibilityRole="button" accessibilityLabel="Cancel payment"
119
+ {leg === firstPending ? <Text className="text-sm text-muted-foreground">{t('pos_checkout.next_leg')}</Text> : null}
120
+ {descriptor && canVoidLeg(leg, descriptor) ? <Pressable testID={`tender-leg-${leg.id}-void`} accessibilityRole="button" accessibilityLabel={t('pos_checkout.cancel_payment')}
119
121
  onPress={() => sale.voidTender(leg.id)} className="self-start min-h-11 justify-center px-2">
120
- <Text className="text-destructive">Cancel payment</Text>
122
+ <Text className="text-destructive">{t('pos_checkout.cancel_payment')}</Text>
121
123
  </Pressable> : null}
122
124
  </View>;
123
125
  })}
124
- {balance === 0 && settled ? <Text className="text-sm text-success">Paid in full</Text> : null}
126
+ {balance === 0 && settled ? <Text className="text-sm text-success">{t('pos_checkout.paid_in_full')}</Text> : null}
125
127
  </View>
126
128
 
127
- {cardReference ? <TextInput accessibilityLabel="Terminal reference" placeholder="Terminal reference" maxLength={REFERENCE_MAX}
129
+ {cardReference ? <TextInput accessibilityLabel={t('tender.terminal_reference')} placeholder={t('tender.terminal_reference')} maxLength={REFERENCE_MAX}
128
130
  value={onlyLeg.reference ?? ''} onChangeText={(text) => sale.setTender({ method: 'external', amountMinor: onlyLeg.amountMinor, reference: text })}
129
131
  className="rounded-md border border-border px-3 py-2 text-foreground" /> : null}
130
132
 
@@ -136,44 +138,37 @@ export function TenderPane({ sale, state, dispatch, methods }: TenderPaneProps):
136
138
  <Text className={cn('text-center font-semibold', tile.id === state.methodId ? 'text-primary-foreground' : 'text-foreground')}>{tile.title}</Text>
137
139
  </Pressable>)}
138
140
  </View>
139
- {planned ? <Text testID="tender-plan" className="text-sm text-muted-foreground">{`${planned.label.n} of ${planned.label.ways}`}</Text> : null}
141
+ {planned ? <Text testID="tender-plan" className="text-sm text-muted-foreground">{t('pos_checkout.n_of_ways', { n: planned.label.n, ways: planned.label.ways })}</Text> : null}
140
142
  <Text testID="tender-entry-amount" className={cn('text-3xl font-bold text-foreground', !state.entryDirty && 'opacity-70')}>{money(figures.entryMinor)}</Text>
141
- {figures.roundingMinor !== 0 ? <Text className="text-sm text-muted-foreground">{`Rounded from ${money(balance)}`}</Text> : null}
143
+ {figures.roundingMinor !== 0 ? <Text className="text-sm text-muted-foreground">{t('pos_checkout.rounded_from', { amount: money(balance) })}</Text> : null}
142
144
  <Text testID="tender-entry-hint" className={cn('min-h-6 text-sm', noChange ? 'text-warning' : entryChange > 0 ? 'text-success' : 'text-muted-foreground')}>{hint}</Text>
143
- {method?.id === 'external' ? <TextInput accessibilityLabel="Terminal reference" placeholder="Terminal reference" maxLength={REFERENCE_MAX}
145
+ {method?.id === 'external' ? <TextInput accessibilityLabel={t('tender.terminal_reference')} placeholder={t('tender.terminal_reference')} maxLength={REFERENCE_MAX}
144
146
  value={reference} onChangeText={setReference} className="rounded-md border border-border px-3 py-2 text-foreground" /> : null}
145
147
  {chips.length ? <View testID="tender-quick" className="flex-row flex-wrap gap-2">
146
148
  {chips.map((minor, index) => <Pressable key={minor} testID={index === 0 ? 'tender-quick-exact' : `tender-quick-${minor}`} accessibilityRole="button"
147
149
  onPress={() => dispatch({ type: 'set-entry', minor })} className={cn(button, state.entryDirty && figures.entryMinor === minor && 'border-primary')}>
148
- <Text className="font-semibold text-foreground">{index === 0 ? `Exact ${money(minor)}` : money(minor)}</Text>
150
+ <Text className="font-semibold text-foreground">{index === 0 ? t('pos_checkout.exact_amount', { amount: money(minor) }) : money(minor)}</Text>
149
151
  </Pressable>)}
150
152
  </View> : null}
151
- <View testID="tender-keypad" className="gap-2">
152
- {KEYPAD_ROWS.map((row) => <View key={row[0].key} className="flex-row gap-2">
153
- {row.map(({ key, label, name }) => <Pressable key={key} accessibilityRole="button" accessibilityLabel={name ?? label}
154
- onPress={() => dispatch({ type: 'key', key })} className={cn(button, 'flex-1 items-center')}>
155
- <Text className="text-xl font-semibold text-foreground">{label}</Text>
156
- </Pressable>)}
157
- </View>)}
158
- </View>
159
- {refusal ? <Text accessibilityRole="alert" className="text-destructive">{refusal}</Text> : null}
153
+ <Keypad testID="tender-keypad" rows={keypadRows(t)} onPress={(name) => dispatch({ type: 'key', key: name as TenderKey })} />
154
+ {refusal ? <Text accessibilityRole="alert" className="text-destructive">{t('tender.not_taken')}</Text> : null}
160
155
  {method ? <Pressable testID="tender-take" accessibilityRole="button" disabled={cannotTake}
161
156
  onPress={take} className={cn('rounded-md bg-primary px-4 py-3', cannotTake && 'opacity-50')}>
162
- <Text className="text-center font-semibold text-primary-foreground">{`Take ${money(figures.appliedMinor)} in ${method.title}`}</Text>
157
+ <Text className="text-center font-semibold text-primary-foreground">{t('pos_checkout.take_amount_in', { amount: money(figures.appliedMinor), method: method.title })}</Text>
163
158
  </Pressable> : null}
164
159
  </View> : null}
165
160
 
166
161
  <Pressable testID="tender-complete" accessibilityRole="button" disabled={!canComplete} onPress={sale.complete}
167
162
  className={cn('rounded-md bg-primary px-4 py-3', !canComplete && 'opacity-50')}>
168
- <Text className="text-center font-semibold text-primary-foreground">Complete sale</Text>
163
+ <Text className="text-center font-semibold text-primary-foreground">{t('tender.complete_sale')}</Text>
169
164
  </Pressable>
170
165
  {sale.error ? <Text accessibilityRole="alert" className="text-destructive">{sale.error}</Text> : null}
171
166
  {sale.canContinue ? <>
172
- <Text className="text-foreground">This sale is stored and will be sent. Continue to the next sale.</Text>
167
+ <Text className="text-foreground">{t('tender.stored_will_send')}</Text>
173
168
  <Pressable accessibilityRole="button" onPress={() => sale.continueSale()} className="rounded-md bg-primary px-4 py-3">
174
- <Text className="text-center font-semibold text-primary-foreground">Continue</Text>
169
+ <Text className="text-center font-semibold text-primary-foreground">{t('tender.continue')}</Text>
175
170
  </Pressable>
176
171
  </> : null}
177
- <Pressable accessibilityRole="button" onPress={sale.cancelTender} className={button}><Text className="text-center text-foreground">Back</Text></Pressable>
172
+ <Pressable accessibilityRole="button" onPress={sale.cancelTender} className={button}><Text className="text-center text-foreground">{t('common.back')}</Text></Pressable>
178
173
  </View>;
179
174
  }