|
1 |
| -import { Input, Text, clx } from "@medusajs/ui" |
| 1 | +import { clx, Input, Text } from "@medusajs/ui" |
| 2 | +import { getNumberOfDecimalPlaces } from "../../../lib/number-helper" |
2 | 3 | import { ComponentProps, ElementRef, forwardRef } from "react"
|
3 | 4 | import Primitive from "react-currency-input-field"
|
4 | 5 |
|
5 |
| -/** |
6 |
| - * @deprecated Use `PercentageInput` instead |
7 |
| - */ |
| 6 | +const MIN_DECIMAL_SCALE = 2 |
| 7 | + |
| 8 | +function resolveDecimalScale( |
| 9 | + value: string | readonly string[] | number | undefined | null |
| 10 | +): number | undefined { |
| 11 | + if (value == null || Array.isArray(value)) { |
| 12 | + return MIN_DECIMAL_SCALE |
| 13 | + } |
| 14 | + return Math.max( |
| 15 | + getNumberOfDecimalPlaces(parseFloat(value.toString())), |
| 16 | + MIN_DECIMAL_SCALE |
| 17 | + ) |
| 18 | +} |
| 19 | + |
8 | 20 | export const DeprecatedPercentageInput = forwardRef<
|
9 | 21 | ElementRef<typeof Input>,
|
10 | 22 | Omit<ComponentProps<typeof Input>, "type">
|
@@ -38,38 +50,56 @@ DeprecatedPercentageInput.displayName = "PercentageInput"
|
38 | 50 | export const PercentageInput = forwardRef<
|
39 | 51 | ElementRef<"input">,
|
40 | 52 | ComponentProps<typeof Primitive>
|
41 |
| ->(({ min = 0, decimalScale = 2, className, ...props }, ref) => { |
42 |
| - return ( |
43 |
| - <div className="relative"> |
44 |
| - <Primitive |
45 |
| - ref={ref as any} // dependency is typed incorrectly |
46 |
| - min={min} |
47 |
| - autoComplete="off" |
48 |
| - decimalScale={decimalScale} |
49 |
| - decimalsLimit={decimalScale} |
50 |
| - {...props} |
51 |
| - className={clx( |
52 |
| - "caret-ui-fg-base bg-ui-bg-field shadow-buttons-neutral transition-fg txt-compact-small flex w-full select-none appearance-none items-center justify-between rounded-md px-2 py-1.5 pr-10 text-right outline-none", |
53 |
| - "placeholder:text-ui-fg-muted text-ui-fg-base", |
54 |
| - "hover:bg-ui-bg-field-hover", |
55 |
| - "focus-visible:shadow-borders-interactive-with-active data-[state=open]:!shadow-borders-interactive-with-active", |
56 |
| - "aria-[invalid=true]:border-ui-border-error aria-[invalid=true]:shadow-borders-error", |
57 |
| - "invalid::border-ui-border-error invalid:shadow-borders-error", |
58 |
| - "disabled:!bg-ui-bg-disabled disabled:!text-ui-fg-disabled", |
59 |
| - className |
60 |
| - )} |
61 |
| - /> |
62 |
| - <div className="absolute inset-y-0 right-0 z-10 flex w-8 items-center justify-center border-l"> |
63 |
| - <Text |
64 |
| - className="text-ui-fg-muted" |
65 |
| - size="small" |
66 |
| - leading="compact" |
67 |
| - weight="plus" |
68 |
| - > |
69 |
| - % |
70 |
| - </Text> |
| 53 | +>( |
| 54 | + ( |
| 55 | + { |
| 56 | + min = 0, |
| 57 | + max = 100, |
| 58 | + decimalScale, |
| 59 | + decimalsLimit, |
| 60 | + value, |
| 61 | + className, |
| 62 | + ...props |
| 63 | + }, |
| 64 | + ref |
| 65 | + ) => { |
| 66 | + const resolvedDecimalScale = decimalScale ?? resolveDecimalScale(value) |
| 67 | + const resolvedDecimalsLimit = decimalsLimit ?? resolvedDecimalScale |
| 68 | + |
| 69 | + return ( |
| 70 | + <div className="relative"> |
| 71 | + <Primitive |
| 72 | + ref={ref as any} // dependency is typed incorrectly |
| 73 | + min={min} |
| 74 | + max={max} |
| 75 | + autoComplete="off" |
| 76 | + decimalScale={resolvedDecimalScale} |
| 77 | + decimalsLimit={resolvedDecimalsLimit} |
| 78 | + value={value} |
| 79 | + {...props} |
| 80 | + className={clx( |
| 81 | + "caret-ui-fg-base bg-ui-bg-field shadow-buttons-neutral transition-fg txt-compact-small flex w-full select-none appearance-none items-center justify-between rounded-md px-2 py-1.5 pl-10 text-left outline-none", |
| 82 | + "placeholder:text-ui-fg-muted text-ui-fg-base", |
| 83 | + "hover:bg-ui-bg-field-hover", |
| 84 | + "focus-visible:shadow-borders-interactive-with-active data-[state=open]:!shadow-borders-interactive-with-active", |
| 85 | + "aria-[invalid=true]:border-ui-border-error aria-[invalid=true]:shadow-borders-error", |
| 86 | + "invalid::border-ui-border-error invalid:shadow-borders-error", |
| 87 | + "disabled:!bg-ui-bg-disabled disabled:!text-ui-fg-disabled", |
| 88 | + className |
| 89 | + )} |
| 90 | + /> |
| 91 | + <div className="absolute inset-y-0 left-0 z-10 flex w-8 items-center justify-center border-r"> |
| 92 | + <Text |
| 93 | + className="text-ui-fg-muted" |
| 94 | + size="small" |
| 95 | + leading="compact" |
| 96 | + weight="plus" |
| 97 | + > |
| 98 | + % |
| 99 | + </Text> |
| 100 | + </div> |
71 | 101 | </div>
|
72 |
| - </div> |
73 |
| - ) |
74 |
| -}) |
| 102 | + ) |
| 103 | + } |
| 104 | +) |
75 | 105 | PercentageInput.displayName = "PercentageInput"
|
0 commit comments