forked from Creditra/Creditra-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAmountRangeChips.tsx
More file actions
278 lines (252 loc) · 8.48 KB
/
Copy pathAmountRangeChips.tsx
File metadata and controls
278 lines (252 loc) · 8.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import { useEffect, useId, useRef, useState } from "react";
import { useBodyScrollLock } from "../hooks/useBodyScrollLock";
import { useFocusTrap } from "../hooks/useFocusTrap";
import { useInertBackdrop } from "../hooks/useInertBackdrop";
import "./AmountRangeChips.css";
export const AMOUNT_RANGE_OPTIONS = [
{ value: "all", label: "All amounts" },
{ value: "under-5k", label: "Under $5k" },
{ value: "5k-25k", label: "$5k-$25k" },
{ value: "25k-plus", label: "$25k+" },
] as const;
export type AmountRangePreset = (typeof AMOUNT_RANGE_OPTIONS)[number]["value"];
interface AmountRangeValue {
min: number | null;
max: number | null;
}
interface AmountRangeChipsProps {
selectedPreset: AmountRangePreset;
customMin: string;
customMax: string;
isCustomActive: boolean;
onPresetChange: (preset: AmountRangePreset) => void;
onCustomRangeApply: (range: AmountRangeValue) => void;
onCustomRangeClear: () => void;
}
const formatAmountLabel = (value: string): string => {
const amount = Number(value);
if (!Number.isFinite(amount)) return value;
return new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
maximumFractionDigits: amount % 1 === 0 ? 0 : 2,
}).format(amount);
};
const parseAmountInput = (value: string): number | null => {
const trimmed = value.trim();
if (!trimmed) return null;
const parsed = Number(trimmed);
if (!Number.isFinite(parsed) || parsed < 0) return null;
return parsed;
};
export function AmountRangeChips({
selectedPreset,
customMin,
customMax,
isCustomActive,
onPresetChange,
onCustomRangeApply,
onCustomRangeClear,
}: AmountRangeChipsProps) {
const labelId = useId();
const modalId = `${labelId.replace(/:/g, "")}-amount-range-modal`;
const minInputRef = useRef<HTMLInputElement>(null);
const triggerRef = useRef<HTMLButtonElement>(null);
const [isModalOpen, setIsModalOpen] = useState(false);
const [draftMin, setDraftMin] = useState(customMin);
const [draftMax, setDraftMax] = useState(customMax);
const activeDialogRef = useFocusTrap({
isActive: isModalOpen,
triggerRef,
onEscape: () => setIsModalOpen(false),
});
useBodyScrollLock({ isLocked: isModalOpen });
useInertBackdrop({ isInert: isModalOpen, modalId });
useEffect(() => {
if (!isModalOpen) return;
setDraftMin(customMin);
setDraftMax(customMax);
}, [customMin, customMax, isModalOpen]);
useEffect(() => {
if (!isModalOpen) return;
const timer = window.setTimeout(() => {
minInputRef.current?.focus();
}, 60);
return () => window.clearTimeout(timer);
}, [isModalOpen]);
const parsedMin = parseAmountInput(draftMin);
const parsedMax = parseAmountInput(draftMax);
const hasInvalidDraft = [draftMin, draftMax].some((value) => {
const trimmed = value.trim();
return trimmed.length > 0 && parseAmountInput(value) === null;
});
const isRangeInverted =
parsedMin !== null && parsedMax !== null && parsedMin > parsedMax;
const isApplyDisabled =
hasInvalidDraft ||
isRangeInverted ||
(draftMin.trim() === "" && draftMax.trim() === "");
const customSummary = [
customMin ? `Min ${formatAmountLabel(customMin)}` : null,
customMax ? `Max ${formatAmountLabel(customMax)}` : null,
]
.filter(Boolean)
.join(" · ");
const handleApply = () => {
if (isApplyDisabled) return;
onCustomRangeApply({ min: parsedMin, max: parsedMax });
setIsModalOpen(false);
};
const handleOpenModal = () => {
setIsModalOpen(true);
};
return (
<div className="amount-range-chips">
<span className="th-filter-label" id={labelId}>
Amount Range
</span>
<div className="th-chip-group" role="group" aria-labelledby={labelId}>
{AMOUNT_RANGE_OPTIONS.map((option) => (
<button
key={option.value}
type="button"
className="th-filter-chip"
aria-pressed={!isCustomActive && selectedPreset === option.value}
onClick={() => onPresetChange(option.value)}
>
{option.label}
</button>
))}
</div>
<div className="amount-range-actions">
<button
ref={triggerRef}
type="button"
className={`amount-range-custom-trigger ${isCustomActive ? "is-active" : ""}`}
aria-pressed={isCustomActive}
aria-haspopup="dialog"
onClick={handleOpenModal}
>
{isCustomActive ? `Custom: ${customSummary}` : "Custom range"}
</button>
{isCustomActive && (
<button
type="button"
className="amount-range-clear"
onClick={onCustomRangeClear}
>
Clear custom
</button>
)}
</div>
{isModalOpen && (
<div id={modalId} className="amount-range-modal-root">
<div
className="amount-range-modal-backdrop"
aria-hidden="true"
onClick={() => setIsModalOpen(false)}
/>
<div
ref={activeDialogRef}
className="amount-range-modal"
role="dialog"
aria-modal="true"
aria-labelledby="amount-range-modal-title"
aria-describedby="amount-range-modal-description"
>
<div className="amount-range-modal-header">
<div>
<p className="amount-range-modal-kicker">Fine-grained filter</p>
<h3 id="amount-range-modal-title">
Choose a custom amount range
</h3>
<p id="amount-range-modal-description">
Filter transaction amounts by a minimum, maximum, or both.
</p>
</div>
<button
type="button"
className="amount-range-modal-close"
aria-label="Close custom amount range dialog"
onClick={() => setIsModalOpen(false)}
>
×
</button>
</div>
<div className="amount-range-modal-body">
<label className="amount-range-field">
<span>Minimum amount</span>
<input
ref={minInputRef}
type="number"
min="0"
step="0.01"
inputMode="decimal"
value={draftMin}
onChange={(event) => setDraftMin(event.target.value)}
placeholder="0.00"
/>
</label>
<label className="amount-range-field">
<span>Maximum amount</span>
<input
type="number"
min="0"
step="0.01"
inputMode="decimal"
value={draftMax}
onChange={(event) => setDraftMax(event.target.value)}
placeholder="50000.00"
/>
</label>
<p className="amount-range-modal-hint">
Leave either field blank to filter with only a minimum or
maximum value.
</p>
{hasInvalidDraft && (
<p className="amount-range-modal-error" role="alert">
Enter a valid non-negative amount.
</p>
)}
{!hasInvalidDraft && isRangeInverted && (
<p className="amount-range-modal-error" role="alert">
Minimum amount must be less than or equal to the maximum
amount.
</p>
)}
</div>
<div className="amount-range-modal-footer">
{isCustomActive && (
<button
type="button"
className="amount-range-secondary"
onClick={() => {
onCustomRangeClear();
setIsModalOpen(false);
}}
>
Clear custom range
</button>
)}
<button
type="button"
className="amount-range-secondary"
onClick={() => setIsModalOpen(false)}
>
Cancel
</button>
<button
type="button"
className="amount-range-primary"
onClick={handleApply}
disabled={isApplyDisabled}
>
Apply range
</button>
</div>
</div>
</div>
)}
</div>
);
}