@tallyui/components 3.15.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tallyui/components",
3
- "version": "3.15.0",
3
+ "version": "3.16.0",
4
4
  "type": "module",
5
5
  "description": "Universal POS UI components for Tally UI",
6
6
  "main": "./dist/index.js",
@@ -28,22 +28,24 @@
28
28
  "directory": "packages/components"
29
29
  },
30
30
  "peerDependencies": {
31
- "@tallyui/core": "^3.15.0",
32
- "@tallyui/primitives": "^3.15.0",
33
- "@tallyui/theme": "^3.15.0",
31
+ "@tallyui/core": "^3.16.0",
32
+ "@tallyui/primitives": "^3.16.0",
33
+ "@tallyui/theme": "^3.16.0",
34
34
  "react": ">=18",
35
35
  "react-native": ">=0.74",
36
36
  "react-native-svg": ">=15"
37
37
  },
38
38
  "dependencies": {
39
39
  "class-variance-authority": "^0.7.1",
40
- "@tallyui/pos": "3.15.0"
40
+ "i18next": "^26.7.1",
41
+ "react-i18next": "^17.1.12",
42
+ "@tallyui/pos": "3.16.0"
41
43
  },
42
44
  "devDependencies": {
43
- "@tallyui/connector-medusa": "3.15.0",
44
- "@tallyui/connector-vendure": "3.15.0",
45
- "@tallyui/connector-woocommerce": "3.15.0",
46
- "@tallyui/mock-api": "3.15.0",
45
+ "@tallyui/connector-medusa": "3.16.0",
46
+ "@tallyui/connector-vendure": "3.16.0",
47
+ "@tallyui/connector-woocommerce": "3.16.0",
48
+ "@tallyui/mock-api": "3.16.0",
47
49
  "@types/react": ">=18",
48
50
  "react": "^19.2.4",
49
51
  "react-native-svg": "15.12.1",
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Port of WCPOS v2 (MIT) `packages/core/src/contexts/translations/index.tsx`: one i18next instance per
3
+ * provider, a locale change applied with `changeLanguage`, v2's init options and `useT`. The regional
4
+ * fallback (`fr_CA` → `fr`) is v2's `rxdb-backend.ts` `getBaseLanguage`, as `fallbackLng`. There is no
5
+ * backend: English is bundled and the app passes other languages as `resources`.
6
+ */
7
+ import * as React from 'react';
8
+ import { createInstance, type i18n as I18n, type TFunction } from 'i18next';
9
+ import { useTranslation } from 'react-i18next';
10
+ import en from './locales/en/components.json';
11
+
12
+ /** Catalogues by language code, as the app spells it (`fr`, `fr_CA`, `fr-CA`): key → string. */
13
+ export type Catalogues = Record<string, Record<string, string>>;
14
+
15
+ export interface I18nProviderProps {
16
+ /** The store's locale (`fr_CA` or `fr-CA`). Without one, the device's locale. */
17
+ locale?: string;
18
+ /** Other languages' catalogues; English is bundled. May arrive or change after mount. */
19
+ resources?: Catalogues;
20
+ children: React.ReactNode;
21
+ }
22
+
23
+ const NS = 'components';
24
+
25
+ /** `fr_CA` or `fr-CA` → `fr`, then English. */
26
+ function fallbackLng(code: string): string[] {
27
+ const parts = code.split(/[-_]/);
28
+ return parts.length > 1 ? [parts[0].toLowerCase(), 'en'] : ['en'];
29
+ }
30
+
31
+ function createI18n(locale: string, resources: Catalogues): I18n {
32
+ const instance = createInstance();
33
+ void instance.init({
34
+ lng: locale,
35
+ fallbackLng,
36
+ load: 'currentOnly',
37
+ ns: [NS],
38
+ defaultNS: NS,
39
+ // An app's `en` overrides bundled keys and keeps the rest, as addResourceBundle's deep merge does after mount.
40
+ resources: Object.fromEntries(Object.entries({ ...resources, en: { ...en, ...resources.en } }).map(([lng, catalogue]) => [lng, { [NS]: catalogue }])),
41
+ keySeparator: false,
42
+ nsSeparator: false,
43
+ initAsync: false,
44
+ interpolation: { escapeValue: false, prefix: '{', suffix: '}' },
45
+ react: { bindI18nStore: 'added' },
46
+ });
47
+ return instance;
48
+ }
49
+
50
+ const deviceLocale = () => Intl.DateTimeFormat().resolvedOptions().locale;
51
+
52
+ /** Without a provider, components read this English-only instance. */
53
+ const I18nContext = React.createContext<I18n>(createI18n('en', {}));
54
+
55
+ export function I18nProvider({ locale, resources, children }: I18nProviderProps): React.JSX.Element {
56
+ const lng = React.useMemo(() => locale ?? deviceLocale(), [locale]);
57
+ const [instance] = React.useState(() => createI18n(lng, resources ?? {}));
58
+ const added = React.useRef(resources);
59
+
60
+ React.useEffect(() => {
61
+ if (added.current === resources) return;
62
+ added.current = resources;
63
+ for (const [code, catalogue] of Object.entries(resources ?? {})) instance.addResourceBundle(code, NS, catalogue, true, true);
64
+ }, [resources, instance]);
65
+
66
+ React.useEffect(() => {
67
+ if (instance.language !== lng) void instance.changeLanguage(lng);
68
+ }, [lng, instance]);
69
+
70
+ return <I18nContext.Provider value={instance}>{children}</I18nContext.Provider>;
71
+ }
72
+
73
+ /** The components' `t`, read from the nearest `I18nProvider`, else English. Interpolation is `{name}`. */
74
+ export function useT(): TFunction {
75
+ const i18n = React.useContext(I18nContext);
76
+ const options = React.useMemo(() => ({ i18n }), [i18n]);
77
+ return useTranslation(NS, options).t;
78
+ }
@@ -0,0 +1,30 @@
1
+ {
2
+ "common.back": "Back",
3
+ "common.backspace": "Delete last digit",
4
+ "common.total": "Total",
5
+ "pos_checkout.cancel_payment": "Cancel payment",
6
+ "pos_checkout.cancelled": "Cancelled",
7
+ "pos_checkout.change_due": "Change {amount}",
8
+ "pos_checkout.clear_amount": "Clear",
9
+ "pos_checkout.exact_amount": "Exact {amount}",
10
+ "pos_checkout.n_of_ways": "{n} of {ways}",
11
+ "pos_checkout.next_leg": "Next",
12
+ "pos_checkout.no_change_for_method": "Only {amount} is due — {method} can't give change",
13
+ "pos_checkout.no_payments_yet": "No payments yet",
14
+ "pos_checkout.paid_in_full": "Paid in full",
15
+ "pos_checkout.paid_of_total": "{paid} of {total} paid",
16
+ "pos_checkout.part_payment_left": "Part payment · {left} left after this",
17
+ "pos_checkout.remaining": "Remaining",
18
+ "pos_checkout.rounded_from": "Rounded from {amount}",
19
+ "pos_checkout.status_authorized": "Authorized",
20
+ "pos_checkout.status_captured": "Captured",
21
+ "pos_checkout.status_failed": "Failed",
22
+ "pos_checkout.status_pending": "Pending",
23
+ "pos_checkout.status_voided": "Voided",
24
+ "pos_checkout.take_amount_in": "Take {amount} in {method}",
25
+ "tender.complete_sale": "Complete sale",
26
+ "tender.continue": "Continue",
27
+ "tender.not_taken": "That payment wasn't taken: nothing is due, or the amount isn't valid.",
28
+ "tender.stored_will_send": "This sale is stored and will be sent. Continue to the next sale.",
29
+ "tender.terminal_reference": "Terminal reference"
30
+ }
package/src/index.ts CHANGED
@@ -227,3 +227,6 @@ export {
227
227
 
228
228
  // Fonts
229
229
  export { FontProvider, useFontType, type FontProviderProps, type FontState, type FontStatus } from './fonts';
230
+
231
+ // i18n
232
+ export { I18nProvider, useT, type I18nProviderProps, type Catalogues } from './i18n';
@@ -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,7 @@ 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';
16
17
  import { DIGIT_ROWS, Keypad } from '../input/keypad';
17
18
 
18
19
  type Sale = ReturnType<typeof useSale>;
@@ -29,17 +30,18 @@ export interface TenderPaneProps {
29
30
 
30
31
  /** order.create's bound on `payments[].reference`, in UTF-16 units. */
31
32
  const REFERENCE_MAX = 255;
32
- /** v2 labels.ts statusLabel. */
33
+ /** v2 labels.ts statusLabel, as catalogue keys. */
33
34
  const STATUS_LABEL: Record<PaymentLegStatus, string> = {
34
- 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',
35
37
  };
36
38
  /** v2 labels.ts statusVariant: coloured by what the cashier has to do about the leg. */
37
39
  const STATUS_TONE: Record<PaymentLegStatus, string> = {
38
40
  pending: 'text-muted-foreground', authorized: 'text-success', captured: 'text-success', failed: 'text-destructive', voided: 'text-muted-foreground',
39
41
  };
40
42
  /** v2 tender-pane.tsx keypadRows: 123 / 456 / 789, then C, 0, backspace. No decimal key: digits shift in from the right. */
41
- const KEYPAD_ROWS = [...DIGIT_ROWS,
42
- [{ name: 'clear', label: 'Clear', text: 'C' }, { name: '0', label: '0' }, { name: 'backspace', label: 'Delete last digit', text: '⌫' }]];
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: '⌫' }]];
43
45
  const button = 'min-h-11 justify-center rounded-md border border-border bg-card px-4 py-2';
44
46
 
45
47
  /** The captured legs since the plan began, as the reducer's `tender-recorded` and `planLegs` read them. */
@@ -48,8 +50,9 @@ const rowsSince = (payments: readonly Leg[], from: number) =>
48
50
 
49
51
  export function TenderPane({ sale, state, dispatch, methods }: TenderPaneProps): JSX.Element | null {
50
52
  const { order, stage } = sale;
53
+ const t = useT();
51
54
  const [reference, setReference] = useState('');
52
- const [refusal, setRefusal] = useState<string | null>(null);
55
+ const [refusal, setRefusal] = useState(false);
53
56
  const balance = order.balanceDueMinor;
54
57
  // The ledger changed (a leg taken or voided) and the entry is untouched: pre-type the next leg or plan share.
55
58
  useEffect(() => {
@@ -76,9 +79,9 @@ export function TenderPane({ sale, state, dispatch, methods }: TenderPaneProps):
76
79
  const [onlyLeg] = order.payments;
77
80
  // startTender('external') records the card leg for the total: its reference is edited in place, as before.
78
81
  const cardReference = order.payments.length === 1 && onlyLeg.method === 'external' && legStatus(onlyLeg) === 'captured' && balance === 0;
79
- const hint = noChange ? `Only ${money(balance)} is due — ${method?.title} can't give change`
80
- : entryChange > 0 ? `Change ${money(entryChange)}`
81
- : 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) }) : '';
82
85
  const canComplete = balance === 0 && !order.payments.some(isInMotion);
83
86
  const cannotTake = figures.appliedMinor <= 0 || noChange;
84
87
 
@@ -87,22 +90,22 @@ export function TenderPane({ sale, state, dispatch, methods }: TenderPaneProps):
87
90
  const id = method.id === 'cash'
88
91
  ? sale.addTender({ method: 'cash', amountMinor: figures.entryMinor })
89
92
  : sale.addTender({ method: 'external', amountMinor: figures.appliedMinor, ...(reference ? { reference } : {}) });
90
- if (id === null) { setRefusal("That payment wasn't taken: nothing is due, or the amount isn't valid."); return; }
91
- setRefusal(null);
93
+ if (id === null) { setRefusal(true); return; }
94
+ setRefusal(false);
92
95
  setReference('');
93
96
  dispatch({ type: 'tender-started' });
94
97
  }
95
98
 
96
99
  return <View dataSet={{ print: 'hide' }} testID="tender-pane" className="gap-4 p-4">
97
100
  <View testID="tender-headline" className="gap-1">
98
- <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>
99
102
  <Text testID="tender-balance" className="text-4xl font-bold text-foreground">{money(balance)}</Text>
100
- {order.paidMinor > 0 ? <Text testID="tender-paid" className="text-sm text-muted-foreground">{`${money(order.paidMinor)} of ${money(order.totalMinor)} paid`}</Text> : null}
101
- {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}
102
105
  </View>
103
106
 
104
107
  <View testID="tender-ledger" className="gap-1">
105
- {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) => {
106
109
  const status = legStatus(leg);
107
110
  const voided = status === 'voided';
108
111
  const descriptor = descriptorOf(leg.method);
@@ -110,20 +113,20 @@ export function TenderPane({ sale, state, dispatch, methods }: TenderPaneProps):
110
113
  <View className="flex-row items-center gap-2">
111
114
  <Text className={cn('flex-1 font-semibold text-foreground', voided && 'line-through')}>{descriptor?.title ?? leg.method}</Text>
112
115
  <Text className={cn('text-foreground', voided && 'line-through')}>{money(leg.amountMinor)}</Text>
113
- <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>
114
117
  </View>
115
118
  {leg.reference ? <Text className="text-sm text-muted-foreground">{leg.reference}</Text> : null}
116
- {leg === firstPending ? <Text className="text-sm text-muted-foreground">Next</Text> : null}
117
- {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')}
118
121
  onPress={() => sale.voidTender(leg.id)} className="self-start min-h-11 justify-center px-2">
119
- <Text className="text-destructive">Cancel payment</Text>
122
+ <Text className="text-destructive">{t('pos_checkout.cancel_payment')}</Text>
120
123
  </Pressable> : null}
121
124
  </View>;
122
125
  })}
123
- {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}
124
127
  </View>
125
128
 
126
- {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}
127
130
  value={onlyLeg.reference ?? ''} onChangeText={(text) => sale.setTender({ method: 'external', amountMinor: onlyLeg.amountMinor, reference: text })}
128
131
  className="rounded-md border border-border px-3 py-2 text-foreground" /> : null}
129
132
 
@@ -135,37 +138,37 @@ export function TenderPane({ sale, state, dispatch, methods }: TenderPaneProps):
135
138
  <Text className={cn('text-center font-semibold', tile.id === state.methodId ? 'text-primary-foreground' : 'text-foreground')}>{tile.title}</Text>
136
139
  </Pressable>)}
137
140
  </View>
138
- {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}
139
142
  <Text testID="tender-entry-amount" className={cn('text-3xl font-bold text-foreground', !state.entryDirty && 'opacity-70')}>{money(figures.entryMinor)}</Text>
140
- {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}
141
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>
142
- {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}
143
146
  value={reference} onChangeText={setReference} className="rounded-md border border-border px-3 py-2 text-foreground" /> : null}
144
147
  {chips.length ? <View testID="tender-quick" className="flex-row flex-wrap gap-2">
145
148
  {chips.map((minor, index) => <Pressable key={minor} testID={index === 0 ? 'tender-quick-exact' : `tender-quick-${minor}`} accessibilityRole="button"
146
149
  onPress={() => dispatch({ type: 'set-entry', minor })} className={cn(button, state.entryDirty && figures.entryMinor === minor && 'border-primary')}>
147
- <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>
148
151
  </Pressable>)}
149
152
  </View> : null}
150
- <Keypad testID="tender-keypad" rows={KEYPAD_ROWS} onPress={(name) => dispatch({ type: 'key', key: name as TenderKey })} />
151
- {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}
152
155
  {method ? <Pressable testID="tender-take" accessibilityRole="button" disabled={cannotTake}
153
156
  onPress={take} className={cn('rounded-md bg-primary px-4 py-3', cannotTake && 'opacity-50')}>
154
- <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>
155
158
  </Pressable> : null}
156
159
  </View> : null}
157
160
 
158
161
  <Pressable testID="tender-complete" accessibilityRole="button" disabled={!canComplete} onPress={sale.complete}
159
162
  className={cn('rounded-md bg-primary px-4 py-3', !canComplete && 'opacity-50')}>
160
- <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>
161
164
  </Pressable>
162
165
  {sale.error ? <Text accessibilityRole="alert" className="text-destructive">{sale.error}</Text> : null}
163
166
  {sale.canContinue ? <>
164
- <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>
165
168
  <Pressable accessibilityRole="button" onPress={() => sale.continueSale()} className="rounded-md bg-primary px-4 py-3">
166
- <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>
167
170
  </Pressable>
168
171
  </> : null}
169
- <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>
170
173
  </View>;
171
174
  }