|
1
|
import { useCallback, useEffect, useState } from "react" |
|
2
|
import { |
|
3
|
Control, |
|
4
|
Controller, |
|
5
|
ControllerRenderProps, |
|
6
|
useWatch, |
|
7
|
} from "react-hook-form" |
|
8
|
import CurrencyInput, { |
|
9
|
CurrencyInputProps, |
|
10
|
formatValue, |
|
11
|
} from "react-currency-input-field" |
|
12
|
import { currencies, CurrencyInfo } from "../../../../../lib/data/currencies" |
|
13
|
import { TieredPriceControls } from "./tiered-price-controls" |
|
14
|
import { DataGridCellContainer } from "../../../../../components/data-grid/components/data-grid-cell-container" |
|
15
|
import { |
|
16
|
useDataGridCell, |
|
17
|
useDataGridCellError, |
|
18
|
} from "../../../../../components/data-grid/hooks" |
|
19
|
import { |
|
20
|
DataGridCellProps, |
|
21
|
InputProps, |
|
22
|
} from "../../../../../components/data-grid/types" |
|
23
|
import { useCombinedRefs } from "../../../../../hooks/use-combined-refs" |
|
24
|
|
|
25
|
export interface TieredPriceCellProps<TData, TValue = any> |
|
26
|
extends DataGridCellProps<TData, TValue> { |
|
27
|
code: string |
|
28
|
onOpenModal: (field: string, currency: CurrencyInfo) => void |
|
29
|
getTieredFieldName: (field: string) => string |
|
30
|
} |
|
31
|
|
|
32
|
export const TieredPriceCell = <TData, TValue = any>({ |
|
33
|
context, |
|
34
|
code, |
|
35
|
onOpenModal, |
|
36
|
getTieredFieldName, |
|
37
|
}: TieredPriceCellProps<TData, TValue>) => { |
|
38
|
const [symbolWidth, setSymbolWidth] = useState(0) |
|
39
|
|
|
40
|
const measuredRef = useCallback((node: HTMLSpanElement) => { |
|
41
|
if (node) { |
|
42
|
const width = node.offsetWidth |
|
43
|
setSymbolWidth(width) |
|
44
|
} |
|
45
|
}, []) |
|
46
|
|
|
47
|
const { field, control, renderProps } = useDataGridCell({ |
|
48
|
context, |
|
49
|
}) |
|
50
|
const errorProps = useDataGridCellError({ context }) |
|
51
|
|
|
52
|
const { container, input } = renderProps |
|
53
|
const { isAnchor } = container |
|
54
|
const currency = currencies[code.toUpperCase()] |
|
55
|
|
|
56
|
return ( |
|
57
|
<Controller |
|
58
|
control={control} |
|
59
|
name={field} |
|
60
|
render={({ field: props }) => { |
|
61
|
return ( |
|
62
|
<DataGridCellContainer |
|
63
|
{...container} |
|
64
|
{...errorProps} |
|
65
|
outerComponent={ |
|
66
|
<OuterComponent |
|
67
|
isAnchor={isAnchor} |
|
68
|
field={field} |
|
69
|
control={control} |
|
70
|
symbolWidth={symbolWidth} |
|
71
|
currency={currency} |
|
72
|
onOpenModal={onOpenModal} |
|
73
|
getTieredFieldName={getTieredFieldName} |
|
74
|
/> |
|
75
|
} |
|
76
|
> |
|
77
|
<Inner |
|
78
|
field={props} |
|
79
|
inputProps={input} |
|
80
|
currencyInfo={currency} |
|
81
|
onMeasureSymbol={measuredRef} |
|
82
|
/> |
|
83
|
</DataGridCellContainer> |
|
84
|
) |
|
85
|
}} |
|
86
|
/> |
|
87
|
) |
|
88
|
} |
|
89
|
|
|
90
|
const OuterComponent = ({ |
|
91
|
isAnchor, |
|
92
|
field, |
|
93
|
control, |
|
94
|
symbolWidth, |
|
95
|
currency, |
|
96
|
onOpenModal, |
|
97
|
getTieredFieldName, |
|
98
|
}: { |
|
99
|
isAnchor: boolean |
|
100
|
field: string |
|
101
|
control: Control<any> |
|
102
|
symbolWidth: number |
|
103
|
currency: CurrencyInfo |
|
104
|
onOpenModal: (field: string, currency: CurrencyInfo) => void |
|
105
|
getTieredFieldName: (field: string) => string |
|
106
|
}) => { |
|
107
|
const tieredFieldName = getTieredFieldName(field) |
|
108
|
const tiers = useWatch({ control, name: tieredFieldName }) |
|
109
|
|
|
110
|
return ( |
|
111
|
<TieredPriceControls |
|
112
|
isTiered={Array.isArray(tiers) && tiers.length > 0} |
|
113
|
isAnchor={isAnchor} |
|
114
|
symbolWidth={symbolWidth} |
|
115
|
onOpenModal={() => onOpenModal(field, currency)} |
|
116
|
/> |
|
117
|
) |
|
118
|
} |
|
119
|
|
|
120
|
const Inner = ({ |
|
121
|
field, |
|
122
|
onMeasureSymbol, |
|
123
|
inputProps, |
|
124
|
currencyInfo, |
|
125
|
}: { |
|
126
|
field: ControllerRenderProps<any, string> |
|
127
|
onMeasureSymbol: (node: HTMLSpanElement) => void |
|
128
|
inputProps: InputProps |
|
129
|
currencyInfo: CurrencyInfo |
|
130
|
}) => { |
|
131
|
const { value, onChange: _, onBlur, ref, ...rest } = field |
|
132
|
const { |
|
133
|
ref: inputRef, |
|
134
|
onBlur: onInputBlur, |
|
135
|
onFocus, |
|
136
|
onChange, |
|
137
|
...attributes |
|
138
|
} = inputProps |
|
139
|
|
|
140
|
const formatter = useCallback( |
|
141
|
(value?: string | number) => { |
|
142
|
const ensuredValue = |
|
143
|
typeof value === "number" ? value.toString() : value || "" |
|
144
|
|
|
145
|
return formatValue({ |
|
146
|
value: ensuredValue, |
|
147
|
decimalScale: currencyInfo.decimal_digits, |
|
148
|
disableGroupSeparators: true, |
|
149
|
decimalSeparator: ".", |
|
150
|
}) |
|
151
|
}, |
|
152
|
[currencyInfo] |
|
153
|
) |
|
154
|
|
|
155
|
const [localValue, setLocalValue] = useState<string | number>(value || "") |
|
156
|
|
|
157
|
const handleValueChange: CurrencyInputProps["onValueChange"] = ( |
|
158
|
value, |
|
159
|
_name, |
|
160
|
_values |
|
161
|
) => { |
|
162
|
if (!value) { |
|
163
|
setLocalValue("") |
|
164
|
return |
|
165
|
} |
|
166
|
|
|
167
|
setLocalValue(value) |
|
168
|
} |
|
169
|
|
|
170
|
useEffect(() => { |
|
171
|
let update = value |
|
172
|
|
|
173
|
if (!isNaN(Number(value))) { |
|
174
|
update = formatter(update) |
|
175
|
} |
|
176
|
|
|
177
|
setLocalValue(update) |
|
178
|
}, [value, formatter]) |
|
179
|
|
|
180
|
const combinedRef = useCombinedRefs(inputRef, ref) |
|
181
|
|
|
182
|
return ( |
|
183
|
<div className="relative flex size-full items-center"> |
|
184
|
<span |
|
185
|
className="txt-compact-small text-ui-fg-muted pointer-events-none absolute left-0 w-fit min-w-4" |
|
186
|
aria-hidden |
|
187
|
ref={onMeasureSymbol} |
|
188
|
> |
|
189
|
{currencyInfo.symbol_native} |
|
190
|
</span> |
|
191
|
<CurrencyInput |
|
192
|
{...rest} |
|
193
|
{...attributes} |
|
194
|
ref={combinedRef} |
|
195
|
className="txt-compact-small w-full flex-1 cursor-default appearance-none bg-transparent pl-[60px] text-right outline-none" |
|
196
|
value={localValue || undefined} |
|
197
|
onValueChange={handleValueChange} |
|
198
|
formatValueOnBlur |
|
199
|
onBlur={() => { |
|
200
|
onBlur() |
|
201
|
onInputBlur() |
|
202
|
|
|
203
|
onChange(localValue, value) |
|
204
|
}} |
|
205
|
onFocus={onFocus} |
|
206
|
decimalScale={currencyInfo.decimal_digits} |
|
207
|
decimalsLimit={currencyInfo.decimal_digits} |
|
208
|
autoComplete="off" |
|
209
|
tabIndex={-1} |
|
210
|
/> |
|
211
|
</div> |
|
212
|
) |
|
213
|
} |