@babylonlabs-io/core-ui 1.24.0 → 1.26.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.
@@ -16,6 +16,7 @@ interface Props {
16
16
  min?: string;
17
17
  step?: string;
18
18
  autoFocus?: boolean;
19
+ decimals?: number;
19
20
  }
20
- export declare const AmountSubsection: ({ fieldName, currencyIcon, currencyName, displayBalance, placeholder, balanceDetails, prefix, min, step, autoFocus, }: Props) => import("react/jsx-runtime").JSX.Element;
21
+ export declare const AmountSubsection: ({ fieldName, currencyIcon, currencyName, displayBalance, placeholder, balanceDetails, prefix, min, step, autoFocus, decimals, }: Props) => import("react/jsx-runtime").JSX.Element;
21
22
  export {};
@@ -5,7 +5,7 @@ import { useFormContext, useWatch } from "react-hook-form";
5
5
  import { AmountItem } from "../../../components/AmountItem/AmountItem";
6
6
  import { calculateTokenValueInCurrency, maxDecimals } from "@/utils/helpers";
7
7
  import { BTC_DECIMAL_PLACES } from "@/utils/constants";
8
- export const AmountSubsection = ({ fieldName, currencyIcon, currencyName, displayBalance, placeholder = "Enter Amount", balanceDetails, prefix, min = "0", step = "any", autoFocus = true, }) => {
8
+ export const AmountSubsection = ({ fieldName, currencyIcon, currencyName, displayBalance, placeholder = "Enter Amount", balanceDetails, prefix, min = "0", step = "any", autoFocus = true, decimals, }) => {
9
9
  const amount = useWatch({ name: fieldName, defaultValue: "" });
10
10
  const { setValue } = useFormContext();
11
11
  const amountValue = parseFloat(amount || "0");
@@ -13,7 +13,25 @@ export const AmountSubsection = ({ fieldName, currencyIcon, currencyName, displa
13
13
  zeroDisplay: "$0.00",
14
14
  });
15
15
  const handleInputChange = (e) => {
16
- setValue(fieldName, e.target.value, {
16
+ let value = e.target.value;
17
+ // If decimals is specified, validate and restrict input
18
+ if (decimals !== undefined && decimals >= 0) {
19
+ // Handle multiple decimal points by taking only the first one
20
+ const [integer, decimal] = value.split('.', 2);
21
+ if (decimal !== undefined && decimal.length > decimals) {
22
+ // Truncate decimal part to specified length
23
+ value = integer + '.' + decimal.slice(0, decimals);
24
+ }
25
+ else if (decimal !== undefined) {
26
+ // Ensure we only have one decimal point
27
+ value = integer + '.' + decimal;
28
+ }
29
+ else if (value.includes('.')) {
30
+ // Handle case where there are multiple dots but no decimal part
31
+ value = integer;
32
+ }
33
+ }
34
+ setValue(fieldName, value, {
17
35
  shouldValidate: true,
18
36
  shouldDirty: true,
19
37
  shouldTouch: true,
@@ -13,3 +13,4 @@ export declare const SmallBalance: Story;
13
13
  export declare const NoAutoFocus: Story;
14
14
  export declare const CustomMinStep: Story;
15
15
  export declare const NoPrefix: Story;
16
+ export declare const RestrictedDecimals: Story;
@@ -207,3 +207,24 @@ export const NoPrefix = {
207
207
  (Story) => (_jsx(Form, { onChange: console.log, schema: schema, children: _jsx(Story, {}) })),
208
208
  ],
209
209
  };
210
+ export const RestrictedDecimals = {
211
+ args: {
212
+ fieldName: "amount",
213
+ currencyIcon: "https://placehold.co/40x40/FF6B6B/FFFFFF?text=₿",
214
+ currencyName: "BTC",
215
+ placeholder: "Enter BTC amount (max 2 decimals)",
216
+ prefix: "Available",
217
+ displayBalance: true,
218
+ decimals: 2, // Restrict to 2 decimal places
219
+ balanceDetails: {
220
+ balance: 1.23456789,
221
+ symbol: "BTC",
222
+ price: 65000,
223
+ displayUSD: true,
224
+ decimals: 8,
225
+ },
226
+ },
227
+ decorators: [
228
+ (Story) => (_jsx(Form, { onChange: console.log, schema: schema, children: _jsx(Story, {}) })),
229
+ ],
230
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@babylonlabs-io/core-ui",
3
- "version": "1.24.0",
3
+ "version": "1.26.0",
4
4
  "type": "module",
5
5
  "types": "dist/index.d.ts",
6
6
  "publishConfig": {