@blocklet/payment-react 1.19.18 → 1.19.20

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.
Files changed (55) hide show
  1. package/README.md +313 -0
  2. package/es/checkout/form.js +18 -2
  3. package/es/components/auto-topup/index.d.ts +14 -0
  4. package/es/components/auto-topup/index.js +417 -0
  5. package/es/components/auto-topup/modal.d.ts +35 -0
  6. package/es/components/auto-topup/modal.js +734 -0
  7. package/es/components/auto-topup/product-card.d.ts +13 -0
  8. package/es/components/auto-topup/product-card.js +173 -0
  9. package/es/components/collapse.d.ts +13 -0
  10. package/es/components/collapse.js +76 -0
  11. package/es/components/input.d.ts +2 -1
  12. package/es/components/input.js +64 -13
  13. package/es/components/label.d.ts +2 -1
  14. package/es/components/label.js +2 -1
  15. package/es/index.d.ts +4 -1
  16. package/es/index.js +7 -1
  17. package/es/libs/util.js +2 -1
  18. package/es/locales/en.js +56 -0
  19. package/es/locales/zh.js +56 -0
  20. package/es/payment/form/index.js +17 -1
  21. package/es/payment/product-item.js +17 -10
  22. package/lib/checkout/form.js +18 -2
  23. package/lib/components/auto-topup/index.d.ts +14 -0
  24. package/lib/components/auto-topup/index.js +451 -0
  25. package/lib/components/auto-topup/modal.d.ts +35 -0
  26. package/lib/components/auto-topup/modal.js +803 -0
  27. package/lib/components/auto-topup/product-card.d.ts +13 -0
  28. package/lib/components/auto-topup/product-card.js +149 -0
  29. package/lib/components/collapse.d.ts +13 -0
  30. package/lib/components/collapse.js +74 -0
  31. package/lib/components/input.d.ts +2 -1
  32. package/lib/components/input.js +66 -24
  33. package/lib/components/label.d.ts +2 -1
  34. package/lib/components/label.js +3 -1
  35. package/lib/index.d.ts +4 -1
  36. package/lib/index.js +24 -0
  37. package/lib/libs/util.js +2 -1
  38. package/lib/locales/en.js +56 -0
  39. package/lib/locales/zh.js +56 -0
  40. package/lib/payment/form/index.js +17 -1
  41. package/lib/payment/product-item.js +18 -10
  42. package/package.json +9 -9
  43. package/src/checkout/form.tsx +21 -2
  44. package/src/components/auto-topup/index.tsx +449 -0
  45. package/src/components/auto-topup/modal.tsx +773 -0
  46. package/src/components/auto-topup/product-card.tsx +156 -0
  47. package/src/components/collapse.tsx +82 -0
  48. package/src/components/input.tsx +71 -22
  49. package/src/components/label.tsx +8 -2
  50. package/src/index.ts +7 -0
  51. package/src/libs/util.ts +1 -0
  52. package/src/locales/en.tsx +59 -0
  53. package/src/locales/zh.tsx +57 -0
  54. package/src/payment/form/index.tsx +19 -1
  55. package/src/payment/product-item.tsx +18 -11
@@ -54,7 +54,7 @@ export default function ProductItem({
54
54
  onQuantityChange = () => {},
55
55
  }: Props) {
56
56
  const { t, locale } = useLocaleContext();
57
- const { settings } = usePaymentContext();
57
+ const { settings, setPayable } = usePaymentContext();
58
58
  const pricing = formatLineItemPricing(item, currency, { trialEnd, trialInDays }, locale);
59
59
  const saving = formatUpsellSaving(items, currency);
60
60
  const metered = item.price?.recurring?.usage_type === 'metered' ? t('common.metered') : '';
@@ -68,15 +68,22 @@ export default function ProductItem({
68
68
  const validDuration = item.price.metadata?.credit_config?.valid_duration_value;
69
69
  const validDurationUnit = item.price.metadata?.credit_config?.valid_duration_unit || 'days';
70
70
 
71
- const [localQuantity, setLocalQuantity] = useState(item.quantity);
71
+ const [localQuantity, setLocalQuantity] = useState<number | undefined>(item.quantity);
72
72
  const canAdjustQuantity = adjustableQuantity.enabled && mode === 'normal';
73
73
  const minQuantity = Math.max(adjustableQuantity.minimum || 1, 1);
74
74
  const quantityAvailable = Math.min(item.price.quantity_limit_per_checkout, item.price.quantity_available);
75
75
  const maxQuantity = quantityAvailable
76
76
  ? Math.min(adjustableQuantity.maximum || Infinity, quantityAvailable)
77
77
  : adjustableQuantity.maximum || Infinity;
78
+ const localQuantityNum = localQuantity || 0;
78
79
 
79
80
  const handleQuantityChange = (newQuantity: number) => {
81
+ if (!newQuantity) {
82
+ setLocalQuantity(undefined);
83
+ setPayable(false);
84
+ return;
85
+ }
86
+ setPayable(true);
80
87
  if (newQuantity >= minQuantity && newQuantity <= maxQuantity) {
81
88
  if (formatQuantityInventory(item.price, newQuantity, locale)) {
82
89
  return;
@@ -87,19 +94,19 @@ export default function ProductItem({
87
94
  };
88
95
 
89
96
  const handleQuantityIncrease = () => {
90
- if (localQuantity < maxQuantity) {
91
- handleQuantityChange(localQuantity + 1);
97
+ if (localQuantityNum < maxQuantity) {
98
+ handleQuantityChange(localQuantityNum + 1);
92
99
  }
93
100
  };
94
101
 
95
102
  const handleQuantityDecrease = () => {
96
- if (localQuantity > minQuantity) {
97
- handleQuantityChange(localQuantity - 1);
103
+ if (localQuantityNum > minQuantity) {
104
+ handleQuantityChange(localQuantityNum - 1);
98
105
  }
99
106
  };
100
107
 
101
108
  const handleQuantityInputChange = (event: React.ChangeEvent<HTMLInputElement>) => {
102
- const value = parseInt(event.target.value, 10);
109
+ const value = parseInt(event.target.value || '0', 10);
103
110
  if (!Number.isNaN(value)) {
104
111
  handleQuantityChange(value);
105
112
  }
@@ -110,7 +117,7 @@ export default function ProductItem({
110
117
  if (!isCreditProduct) return null;
111
118
 
112
119
  const isRecurring = item.price.type === 'recurring';
113
- const totalCredit = formatNumber(creditAmount * localQuantity);
120
+ const totalCredit = formatNumber(creditAmount * (localQuantity || 0));
114
121
 
115
122
  let message = '';
116
123
  if (isRecurring) {
@@ -145,7 +152,7 @@ export default function ProductItem({
145
152
  return pricing.primary;
146
153
  }, [trialInDays, trialEnd, pricing, item, locale]);
147
154
 
148
- const quantityInventoryError = formatQuantityInventory(item.price, localQuantity, locale);
155
+ const quantityInventoryError = formatQuantityInventory(item.price, localQuantityNum, locale);
149
156
 
150
157
  return (
151
158
  <Stack
@@ -235,7 +242,7 @@ export default function ProductItem({
235
242
  <IconButton
236
243
  size="small"
237
244
  onClick={handleQuantityDecrease}
238
- disabled={localQuantity <= minQuantity}
245
+ disabled={localQuantityNum <= minQuantity}
239
246
  sx={{ minWidth: 32, width: 32, height: 32 }}>
240
247
  <Remove fontSize="small" />
241
248
  </IconButton>
@@ -255,7 +262,7 @@ export default function ProductItem({
255
262
  <IconButton
256
263
  size="small"
257
264
  onClick={handleQuantityIncrease}
258
- disabled={localQuantity >= maxQuantity}
265
+ disabled={localQuantityNum >= maxQuantity}
259
266
  sx={{ minWidth: 32, width: 32, height: 32 }}>
260
267
  <Add fontSize="small" />
261
268
  </IconButton>