@skalfa/skalfa-app 1.0.0 → 1.0.1

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 (87) hide show
  1. package/.env.example +43 -43
  2. package/.github/workflows/publish.yml +39 -0
  3. package/CONTRIBUTING.md +45 -0
  4. package/LICENSE +21 -0
  5. package/README.md +91 -28
  6. package/app/auth/edit/page.tsx +65 -65
  7. package/app/auth/login/page.tsx +63 -63
  8. package/app/auth/me/page.tsx +58 -58
  9. package/app/auth/register/page.tsx +69 -69
  10. package/app/auth/verify/page.tsx +53 -53
  11. package/app/dashboard/user/page.tsx +76 -76
  12. package/app/layout.tsx +37 -37
  13. package/app/manifest.ts +25 -0
  14. package/app/page.tsx +13 -13
  15. package/barrels.json +5 -5
  16. package/blueprints/starter.blueprint.json +102 -102
  17. package/bun.lock +916 -0
  18. package/components/base.components/chip/Chip.component.tsx +39 -39
  19. package/components/base.components/document/DocumentViewer.component.tsx +163 -163
  20. package/components/base.components/document/ExportExcel.component.tsx +340 -340
  21. package/components/base.components/document/ImportExcel.component.tsx +315 -315
  22. package/components/base.components/document/PrintTable.component.tsx +204 -204
  23. package/components/base.components/document/RenderPDF.component.tsx +415 -415
  24. package/components/base.components/input/Checkbox.component.tsx +109 -109
  25. package/components/base.components/input/Input.component.tsx +332 -332
  26. package/components/base.components/input/InputCheckbox.component.tsx +174 -174
  27. package/components/base.components/input/InputCurrency.component.tsx +163 -163
  28. package/components/base.components/input/InputDate.component.tsx +352 -352
  29. package/components/base.components/input/InputDatetime.component.tsx +260 -260
  30. package/components/base.components/input/InputDocument.component.tsx +351 -351
  31. package/components/base.components/input/InputImage.component.tsx +533 -533
  32. package/components/base.components/input/InputMap.component.tsx +317 -317
  33. package/components/base.components/input/InputNumber.component.tsx +192 -192
  34. package/components/base.components/input/InputOtp.component.tsx +169 -169
  35. package/components/base.components/input/InputPassword.component.tsx +236 -236
  36. package/components/base.components/input/InputRadio.component.tsx +175 -175
  37. package/components/base.components/input/InputTime.component.tsx +275 -275
  38. package/components/base.components/input/InputValues.component.tsx +68 -68
  39. package/components/base.components/input/Radio.component.tsx +102 -102
  40. package/components/base.components/input/Select.component.tsx +541 -541
  41. package/components/base.components/modal/BottomSheet.component.tsx +245 -245
  42. package/components/base.components/supervision/FormSupervision.component.tsx +433 -433
  43. package/components/base.components/supervision/TableSupervision.component.tsx +697 -697
  44. package/components/base.components/table/ControlBar.component.tsx +497 -497
  45. package/components/base.components/table/FilterComponent.tsx +518 -518
  46. package/components/base.components/table/Table.component.tsx +469 -469
  47. package/components/base.components/typography/TypographyArticle.component.tsx +26 -26
  48. package/components/base.components/typography/TypographyColumn.component.tsx +20 -20
  49. package/components/base.components/typography/TypographyContent.component.tsx +20 -20
  50. package/components/base.components/typography/TypographyTips.component.tsx +20 -20
  51. package/components/base.components/wrap/Draggable.component.tsx +303 -303
  52. package/components/base.components/wrap/IDBProvider.tsx +12 -12
  53. package/components/base.components/wrap/Image.component.tsx +9 -9
  54. package/components/base.components/wrap/ShortcutProvider.tsx +57 -57
  55. package/components/base.components/wrap/Swipe.component.tsx +93 -93
  56. package/components/index.ts +2 -2
  57. package/contexts/AppProvider.tsx +11 -11
  58. package/contexts/Auth.context.tsx +64 -64
  59. package/contexts/Toggle.context.tsx +44 -44
  60. package/next.config.ts +15 -1
  61. package/package.json +14 -13
  62. package/public/204.svg +19 -19
  63. package/public/500.svg +39 -39
  64. package/public/icon-192.png +0 -0
  65. package/public/icon-512.png +0 -0
  66. package/public/images/logo-fill.png +0 -0
  67. package/public/images/logo-full-fill.png +0 -0
  68. package/public/images/logo-full.png +0 -0
  69. package/public/images/logo.png +0 -0
  70. package/schema/idb/app.schema.ts +8 -8
  71. package/src-tauri/Cargo.toml +14 -0
  72. package/src-tauri/build.rs +3 -0
  73. package/src-tauri/capabilities/default.json +11 -0
  74. package/src-tauri/icons/128x128.png +0 -0
  75. package/src-tauri/icons/128x128@2x.png +0 -0
  76. package/src-tauri/icons/32x32.png +0 -0
  77. package/src-tauri/icons/icon.icns +0 -0
  78. package/src-tauri/icons/icon.ico +0 -0
  79. package/src-tauri/src/main.rs +7 -0
  80. package/src-tauri/tauri.conf.json +36 -0
  81. package/styles/globals.css +231 -231
  82. package/styles/tailwind.safelist +68 -68
  83. package/utils/commands/barrels.ts +27 -27
  84. package/utils/commands/light.ts +21 -21
  85. package/utils/commands/logger.ts +42 -42
  86. package/utils/commands/stubs/table-blueprint.stub +12 -12
  87. package/utils/commands/use-pdf.ts +29 -29
@@ -1,533 +1,533 @@
1
- "use client"
2
-
3
- import { useEffect, useRef, useState } from "react";
4
- import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
5
- import { faHandHolding, faImages, faTimes } from "@fortawesome/free-solid-svg-icons";
6
- import { cn, pcn, useInputHandler, useInputRandomId, useResponsive, useValidation, validation, ValidationRules } from "@utils";
7
- import { BottomSheetComponent, ButtonComponent, IconButtonComponent, ModalComponent } from "@components";
8
-
9
-
10
- type CT = "label" | "error" | "input" | "tip";
11
-
12
- export interface InputImageProps {
13
- name : string;
14
- label ?: string;
15
- tip ?: string;
16
-
17
- value ?: string | File;
18
- aspect ?: string;
19
- invalid ?: string;
20
- disabled ?: boolean;
21
- validations ?: ValidationRules;
22
-
23
- onChange?: (file?: File | null) => void;
24
- register?: (name: string, validations?: ValidationRules) => void;
25
- unregister?: (name: string) => void;
26
-
27
- className?: string;
28
- }
29
-
30
-
31
-
32
- export const InputImageComponent: React.FC<InputImageProps> = ({
33
- name,
34
- label,
35
- tip,
36
-
37
- value,
38
- disabled,
39
- aspect = "1/1",
40
- invalid,
41
- validations,
42
-
43
- onChange,
44
- register,
45
- unregister,
46
- className = "",
47
- }) => {
48
- const { isSm } = useResponsive();
49
-
50
- const randomId = useInputRandomId();
51
- const inputRef = useRef<HTMLInputElement>(null);
52
-
53
- const [preview, setPreview] = useState("");
54
- const [drag, setDrag] = useState(false);
55
- const [cropSrc, setCropSrc] = useState<string | null>(null);
56
- const [openCrop, setOpenCrop] = useState(false);
57
-
58
- const inputHandler = useInputHandler(name, value, validations, register, unregister, true);
59
- const [invalidMessage, setInvalidMessage] = useValidation(inputHandler.value, validations, invalid, inputHandler.idle);
60
-
61
- useEffect(() => {
62
- if (value) {
63
- const url = typeof value === "object" ? URL.createObjectURL(value) : value;
64
- setPreview(url);
65
-
66
- return () => {
67
- typeof value === "object" && URL.revokeObjectURL(url);
68
- };
69
- } else {
70
- setPreview("");
71
- }
72
- }, [value]);
73
-
74
- const openCropper = (file: File) => {
75
- const reader = new FileReader();
76
- reader.onload = () => {
77
- setCropSrc(reader.result as string);
78
- setOpenCrop(true);
79
- };
80
- reader.readAsDataURL(file);
81
- };
82
-
83
- const onUpload = (e: React.ChangeEvent<HTMLInputElement>) => {
84
- const file = e.target.files?.[0];
85
- if (!file) return;
86
-
87
- const allowed = ["image/jpeg", "image/jpg", "image/png", "image/webp"];
88
- if (!allowed.includes(file.type)) {
89
- setInvalidMessage("Format gambar tidak diperbolehkan (JPG/PNG/WebP)");
90
- return;
91
- }
92
-
93
- openCropper(file);
94
- };
95
-
96
- const onCropDone = (file: File) => {
97
- const url = URL.createObjectURL(file);
98
- setPreview(url);
99
- onChange?.(file);
100
- setOpenCrop(false);
101
- };
102
-
103
- const onCropCancel = () => {
104
- setOpenCrop(false);
105
- inputRef.current!.value = "";
106
- };
107
-
108
- const remove = () => {
109
- setPreview("");
110
- onChange?.(null);
111
- inputRef.current && (inputRef.current.value = "");
112
- };
113
-
114
- const onDropFile = (e: React.DragEvent<HTMLDivElement>) => {
115
- e.preventDefault();
116
- setDrag(false);
117
-
118
- const file = e.dataTransfer.files?.[0];
119
- file && openCropper(file);
120
- };
121
-
122
- return (
123
- <div className="w-full relative">
124
- {label && (
125
- <label
126
- htmlFor={randomId}
127
- className={cn(
128
- "input-label",
129
- pcn<CT>(className, "label"),
130
- disabled && "opacity-50",
131
- inputHandler.focus && "text-primary",
132
- !!invalidMessage && "text-danger",
133
- )}
134
- >
135
- {label}
136
- {validations && validation.hasRules(validations, "required") && <span className="text-danger ml-1">*</span>}
137
- </label>
138
- )}
139
-
140
- {tip && (
141
- <small
142
- className={cn(
143
- "input-tip",
144
- pcn<CT>(className, "tip"),
145
- disabled && "opacity-60",
146
- )}
147
- >{tip}</small>
148
- )}
149
-
150
- <label htmlFor={randomId}>
151
- <div
152
- className={cn(
153
- "border-2 border-dashed rounded-xl p-2 w-full bg-background cursor-pointer overflow-hidden",
154
- drag ? "border-primary" : "border-gray-300",
155
- invalidMessage && "border-danger",
156
- pcn<CT>(className, "input")
157
- )}
158
- style={{
159
- aspectRatio: aspect,
160
- backgroundImage: preview ? `url(${preview})` : undefined,
161
- backgroundSize: "cover",
162
- backgroundPosition: "center",
163
- }}
164
- onDragOver={(e) => e.preventDefault()}
165
- onDragEnter={(e) => {
166
- e.preventDefault();
167
- setDrag(true);
168
- }}
169
- onDragLeave={() => setDrag(false)}
170
- onDrop={onDropFile}
171
- >
172
- <div className="absolute top-1/2 left-1/2 -translate-1/2 flex flex-col items-center text-light">
173
- <FontAwesomeIcon icon={drag ? faHandHolding : faImages} className="text-3xl" />
174
- <p className="text-xs text-center mt-4">{drag ? "Letakkan di sini" : preview ? "Klik atau seret untuk ganti Gambar" : "Klik atau seret gambar"}</p>
175
- </div>
176
-
177
- <input
178
- id={randomId}
179
- ref={inputRef}
180
- type="file"
181
- accept="image/*"
182
- className="hidden"
183
- disabled={disabled}
184
- onChange={onUpload}
185
- />
186
- </div>
187
- </label>
188
-
189
- {preview && !disabled && (
190
- <IconButtonComponent
191
- icon={faTimes}
192
- paint="danger"
193
- size="sm"
194
- className="absolute top-10 right-4"
195
- onClick={remove}
196
- />
197
- )}
198
-
199
- {invalidMessage && (
200
- <small className={cn("input-error-message", pcn<CT>(className, "error"))}>
201
- {invalidMessage}
202
- </small>
203
- )}
204
-
205
- {!isSm ? (
206
- <ModalComponent show={openCrop} onClose={onCropCancel} title="Sesuaikan Gambar" className="w-max max-w-[350px]">
207
- {cropSrc && (
208
- <div className="p-4">
209
- <CanvasCropper
210
- src={cropSrc}
211
- aspect={eval(aspect.replace(":", "/"))}
212
- onDone={onCropDone}
213
- />
214
- </div>
215
- )}
216
- </ModalComponent>
217
- ) : (
218
- <BottomSheetComponent show={openCrop} onClose={onCropCancel} size={400}>
219
- {cropSrc && (
220
- <div className="p-4">
221
- <CanvasCropper
222
- src={cropSrc}
223
- aspect={eval(aspect.replace(":", "/"))}
224
- onDone={onCropDone}
225
- />
226
- </div>
227
- )}
228
- </BottomSheetComponent>
229
- )}
230
-
231
- </div>
232
- );
233
- };
234
-
235
-
236
-
237
- interface CropperProps {
238
- src : string;
239
- aspect : number;
240
- onDone ?: (file: File) => void;
241
- }
242
-
243
- export const CanvasCropper: React.FC<CropperProps> = ({
244
- src,
245
- aspect,
246
- onDone,
247
- }) => {
248
- const canvasRef = useRef<HTMLCanvasElement>(null);
249
- const previewRef = useRef<HTMLCanvasElement>(null);
250
- const pinchDistRef = useRef(0);
251
- const pinchStartZoomRef = useRef(1);
252
-
253
- const [img, setImg] = useState<HTMLImageElement | null>(null);
254
- const [zoom, setZoom] = useState(1);
255
- const [minZoom, setMinZoom] = useState(1);
256
- const [maxZoom, setMaxZoom] = useState(4);
257
- const [pos, setPos] = useState({ x: 0, y: 0 });
258
- const [dragging, setDragging] = useState(false);
259
- const [start, setStart] = useState({ x: 0, y: 0 });
260
-
261
- const CROP_SIZE = 280;
262
- const CROP_W = CROP_SIZE * aspect;
263
-
264
- const clamp = (v: number, min: number, max: number) => Math.max(min, Math.min(max, v));
265
-
266
- useEffect(() => {
267
- const i = new Image();
268
- i.onload = () => setImg(i);
269
- i.src = src;
270
- }, [src]);
271
-
272
- const renderCanvas = () => {
273
- if (!img) return;
274
-
275
- const canvas = canvasRef.current!;
276
- const ctx = canvas.getContext("2d")!;
277
-
278
- canvas.width = CROP_W;
279
- canvas.height = CROP_SIZE;
280
-
281
- ctx.clearRect(0, 0, canvas.width, canvas.height);
282
-
283
- const scaledW = img.width * zoom;
284
- const scaledH = img.height * zoom;
285
-
286
- const x = pos.x - scaledW / 2 + CROP_W / 2;
287
- const y = pos.y - scaledH / 2 + CROP_SIZE / 2;
288
-
289
- ctx.drawImage(img, x, y, scaledW, scaledH);
290
- };
291
-
292
- useEffect(() => {
293
- renderCanvas();
294
- }, [img, zoom, pos]);
295
-
296
- useEffect(() => {
297
- if (!img) return;
298
-
299
- const imgW = img.naturalWidth;
300
- const imgH = img.naturalHeight;
301
-
302
- const scaleW = CROP_W / imgW;
303
- const scaleH = CROP_SIZE / imgH;
304
-
305
- const base = Math.max(scaleW, scaleH);
306
-
307
- setMinZoom(base);
308
- setZoom(base);
309
- setMaxZoom(base * 4);
310
-
311
- }, [img]);
312
-
313
- const handleDown = (e: React.MouseEvent) => {
314
- setDragging(true);
315
- setStart({ x: e.clientX, y: e.clientY });
316
- };
317
-
318
- const handleUp = () => setDragging(false);
319
-
320
- const handleMove = (e: React.MouseEvent) => {
321
- if (!dragging) return;
322
- const dx = e.clientX - start.x;
323
- const dy = e.clientY - start.y;
324
-
325
- setStart({ x: e.clientX, y: e.clientY });
326
- setPos((p) => {
327
- const scaledW = img!.naturalWidth * zoom;
328
- const scaledH = img!.naturalHeight * zoom;
329
-
330
- const minX = (scaledW - CROP_W) / 2;
331
- const maxX = -(scaledW - CROP_W) / 2;
332
- const minY = (scaledH - CROP_SIZE) / 2;
333
- const maxY = -(scaledH - CROP_SIZE) / 2;
334
-
335
- return {
336
- x: clamp(p.x + dx, maxX, minX),
337
- y: clamp(p.y + dy, maxY, minY),
338
- };
339
- });
340
- };
341
-
342
- const performCrop = () => {
343
- if (!img) return;
344
-
345
- const preview = previewRef.current!;
346
- const pctx = preview.getContext("2d")!;
347
-
348
- preview.width = CROP_W;
349
- preview.height = CROP_SIZE;
350
-
351
- const scaledW = img.width * zoom;
352
- const scaledH = img.height * zoom;
353
-
354
- const x = pos.x - scaledW / 2 + CROP_W / 2;
355
- const y = pos.y - scaledH / 2 + CROP_SIZE / 2;
356
-
357
- pctx.drawImage(img, x, y, scaledW, scaledH);
358
-
359
- preview.toBlob((blob) => {
360
- const file = new File([blob!], "cropped.png", { type: "image/png" });
361
- onDone?.(file);
362
- });
363
- };
364
-
365
-
366
- const handleWheel = (e: React.WheelEvent) => {
367
- e.preventDefault();
368
-
369
- setZoom((z) => {
370
- let next = z - e.deltaY * 0.002;
371
- next = Math.max(minZoom, Math.min(maxZoom, next));
372
-
373
- const scaledW = img!.naturalWidth * next;
374
- const scaledH = img!.naturalHeight * next;
375
-
376
- const minX = (scaledW - CROP_W) / 2;
377
- const maxX = -(scaledW - CROP_W) / 2;
378
-
379
- const minY = (scaledH - CROP_SIZE) / 2;
380
- const maxY = -(scaledH - CROP_SIZE) / 2;
381
-
382
- setPos((p) => ({
383
- x: clamp(p.x, maxX, minX),
384
- y: clamp(p.y, maxY, minY),
385
- }));
386
-
387
- return next;
388
- });
389
- };
390
-
391
-
392
- const handleTouchStart = (e: React.TouchEvent) => {
393
- if (e.touches.length === 2) {
394
- const t1 = e.touches[0];
395
- const t2 = e.touches[1];
396
-
397
- const dist = Math.hypot(
398
- t2.clientX - t1.clientX,
399
- t2.clientY - t1.clientY
400
- );
401
-
402
- pinchDistRef.current = dist;
403
- pinchStartZoomRef.current = zoom;
404
-
405
- e.preventDefault();
406
- }
407
-
408
- // Jika satu jari → drag
409
- if (e.touches.length === 1) {
410
- setDragging(true);
411
- setStart({
412
- x: e.touches[0].clientX,
413
- y: e.touches[0].clientY,
414
- });
415
- }
416
- };
417
-
418
-
419
- const handleTouchMove = (e: React.TouchEvent) => {
420
- if (e.touches.length === 2) {
421
- const t1 = e.touches[0];
422
- const t2 = e.touches[1];
423
- const currDist = Math.hypot(t2.clientX - t1.clientX, t2.clientY - t1.clientY);
424
-
425
- const diff = currDist - pinchDistRef.current;
426
- let nextZoom = pinchStartZoomRef.current + diff * 0.005;
427
-
428
- nextZoom = Math.max(minZoom, Math.min(maxZoom, nextZoom));
429
-
430
- setZoom(nextZoom);
431
-
432
- setPos((p) => {
433
- const scaledW = img!.naturalWidth * nextZoom;
434
- const scaledH = img!.naturalHeight * nextZoom;
435
-
436
- const minX = (scaledW - CROP_W) / 2;
437
- const maxX = -(scaledW - CROP_W) / 2;
438
-
439
- const minY = (scaledH - CROP_SIZE) / 2;
440
- const maxY = -(scaledH - CROP_SIZE) / 2;
441
-
442
- return {
443
- x: Math.max(maxX, Math.min(minX, p.x)),
444
- y: Math.max(maxY, Math.min(minY, p.y)),
445
- };
446
- });
447
-
448
- e.preventDefault();
449
- return;
450
- }
451
-
452
- if (dragging && e.touches.length === 1) {
453
- const { clientX, clientY } = e.touches[0];
454
-
455
- const dx = clientX - start.x;
456
- const dy = clientY - start.y;
457
-
458
- setStart({ x: clientX, y: clientY });
459
-
460
- setPos((p) => {
461
- const scaledW = img!.naturalWidth * zoom;
462
- const scaledH = img!.naturalHeight * zoom;
463
-
464
- const minX = (scaledW - CROP_W) / 2;
465
- const maxX = -(scaledW - CROP_W) / 2;
466
-
467
- const minY = (scaledH - CROP_SIZE) / 2;
468
- const maxY = -(scaledH - CROP_SIZE) / 2;
469
-
470
- return {
471
- x: Math.max(maxX, Math.min(minX, p.x + dx)),
472
- y: Math.max(maxY, Math.min(minY, p.y + dy)),
473
- };
474
- });
475
-
476
- e.preventDefault();
477
- }
478
- };
479
-
480
-
481
- const handleTouchEnd = () => {
482
- setDragging(false);
483
- pinchDistRef.current = 0;
484
- };
485
-
486
- return (
487
- <div style={{ touchAction: "none" }}>
488
- <div
489
- className="relative bg-background rounded-lg overflow-hidden mx-auto"
490
- style={{ width: CROP_W, height: CROP_SIZE }}
491
- onMouseMove={handleMove}
492
- onMouseUp={handleUp}
493
- onMouseLeave={handleUp}
494
- onWheel={handleWheel}
495
- onTouchStart={handleTouchStart}
496
- onTouchMove={handleTouchMove}
497
- onTouchEnd={handleTouchEnd}
498
- >
499
- <canvas
500
- ref={canvasRef}
501
- width={CROP_W}
502
- height={CROP_SIZE}
503
- onMouseDown={handleDown}
504
- className="cursor-move"
505
- />
506
-
507
- <div className="absolute top-0 left-0 pointer-events-none" style={{ width: CROP_W, height: CROP_SIZE }}>
508
- <div className="absolute inset-0 border-2 border-slate-200"></div>
509
- <div className="absolute inset-0">
510
- <div className="absolute top-1/3 left-0 w-full border-t border-slate-200"></div>
511
- <div className="absolute top-2/3 left-0 w-full border-t border-slate-200"></div>
512
- <div className="absolute left-1/3 top-0 h-full border-l border-slate-200"></div>
513
- <div className="absolute left-2/3 top-0 h-full border-l border-slate-200"></div>
514
- </div>
515
- </div>
516
- </div>
517
-
518
- <div className="p-4 pb-2">
519
- <ButtonComponent
520
- label="Selesai"
521
- variant="outline"
522
- onClick={performCrop}
523
- block
524
- />
525
- </div>
526
-
527
- <canvas ref={previewRef} className="hidden" />
528
- </div>
529
- );
530
- };
531
-
532
-
533
-
1
+ "use client"
2
+
3
+ import { useEffect, useRef, useState } from "react";
4
+ import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
5
+ import { faHandHolding, faImages, faTimes } from "@fortawesome/free-solid-svg-icons";
6
+ import { cn, pcn, useInputHandler, useInputRandomId, useResponsive, useValidation, validation, ValidationRules } from "@utils";
7
+ import { BottomSheetComponent, ButtonComponent, IconButtonComponent, ModalComponent } from "@components";
8
+
9
+
10
+ type CT = "label" | "error" | "input" | "tip";
11
+
12
+ export interface InputImageProps {
13
+ name : string;
14
+ label ?: string;
15
+ tip ?: string;
16
+
17
+ value ?: string | File;
18
+ aspect ?: string;
19
+ invalid ?: string;
20
+ disabled ?: boolean;
21
+ validations ?: ValidationRules;
22
+
23
+ onChange?: (file?: File | null) => void;
24
+ register?: (name: string, validations?: ValidationRules) => void;
25
+ unregister?: (name: string) => void;
26
+
27
+ className?: string;
28
+ }
29
+
30
+
31
+
32
+ export const InputImageComponent: React.FC<InputImageProps> = ({
33
+ name,
34
+ label,
35
+ tip,
36
+
37
+ value,
38
+ disabled,
39
+ aspect = "1/1",
40
+ invalid,
41
+ validations,
42
+
43
+ onChange,
44
+ register,
45
+ unregister,
46
+ className = "",
47
+ }) => {
48
+ const { isSm } = useResponsive();
49
+
50
+ const randomId = useInputRandomId();
51
+ const inputRef = useRef<HTMLInputElement>(null);
52
+
53
+ const [preview, setPreview] = useState("");
54
+ const [drag, setDrag] = useState(false);
55
+ const [cropSrc, setCropSrc] = useState<string | null>(null);
56
+ const [openCrop, setOpenCrop] = useState(false);
57
+
58
+ const inputHandler = useInputHandler(name, value, validations, register, unregister, true);
59
+ const [invalidMessage, setInvalidMessage] = useValidation(inputHandler.value, validations, invalid, inputHandler.idle);
60
+
61
+ useEffect(() => {
62
+ if (value) {
63
+ const url = typeof value === "object" ? URL.createObjectURL(value) : value;
64
+ setPreview(url);
65
+
66
+ return () => {
67
+ typeof value === "object" && URL.revokeObjectURL(url);
68
+ };
69
+ } else {
70
+ setPreview("");
71
+ }
72
+ }, [value]);
73
+
74
+ const openCropper = (file: File) => {
75
+ const reader = new FileReader();
76
+ reader.onload = () => {
77
+ setCropSrc(reader.result as string);
78
+ setOpenCrop(true);
79
+ };
80
+ reader.readAsDataURL(file);
81
+ };
82
+
83
+ const onUpload = (e: React.ChangeEvent<HTMLInputElement>) => {
84
+ const file = e.target.files?.[0];
85
+ if (!file) return;
86
+
87
+ const allowed = ["image/jpeg", "image/jpg", "image/png", "image/webp"];
88
+ if (!allowed.includes(file.type)) {
89
+ setInvalidMessage("Format gambar tidak diperbolehkan (JPG/PNG/WebP)");
90
+ return;
91
+ }
92
+
93
+ openCropper(file);
94
+ };
95
+
96
+ const onCropDone = (file: File) => {
97
+ const url = URL.createObjectURL(file);
98
+ setPreview(url);
99
+ onChange?.(file);
100
+ setOpenCrop(false);
101
+ };
102
+
103
+ const onCropCancel = () => {
104
+ setOpenCrop(false);
105
+ inputRef.current!.value = "";
106
+ };
107
+
108
+ const remove = () => {
109
+ setPreview("");
110
+ onChange?.(null);
111
+ inputRef.current && (inputRef.current.value = "");
112
+ };
113
+
114
+ const onDropFile = (e: React.DragEvent<HTMLDivElement>) => {
115
+ e.preventDefault();
116
+ setDrag(false);
117
+
118
+ const file = e.dataTransfer.files?.[0];
119
+ file && openCropper(file);
120
+ };
121
+
122
+ return (
123
+ <div className="w-full relative">
124
+ {label && (
125
+ <label
126
+ htmlFor={randomId}
127
+ className={cn(
128
+ "input-label",
129
+ pcn<CT>(className, "label"),
130
+ disabled && "opacity-50",
131
+ inputHandler.focus && "text-primary",
132
+ !!invalidMessage && "text-danger",
133
+ )}
134
+ >
135
+ {label}
136
+ {validations && validation.hasRules(validations, "required") && <span className="text-danger ml-1">*</span>}
137
+ </label>
138
+ )}
139
+
140
+ {tip && (
141
+ <small
142
+ className={cn(
143
+ "input-tip",
144
+ pcn<CT>(className, "tip"),
145
+ disabled && "opacity-60",
146
+ )}
147
+ >{tip}</small>
148
+ )}
149
+
150
+ <label htmlFor={randomId}>
151
+ <div
152
+ className={cn(
153
+ "border-2 border-dashed rounded-xl p-2 w-full bg-background cursor-pointer overflow-hidden",
154
+ drag ? "border-primary" : "border-gray-300",
155
+ invalidMessage && "border-danger",
156
+ pcn<CT>(className, "input")
157
+ )}
158
+ style={{
159
+ aspectRatio: aspect,
160
+ backgroundImage: preview ? `url(${preview})` : undefined,
161
+ backgroundSize: "cover",
162
+ backgroundPosition: "center",
163
+ }}
164
+ onDragOver={(e) => e.preventDefault()}
165
+ onDragEnter={(e) => {
166
+ e.preventDefault();
167
+ setDrag(true);
168
+ }}
169
+ onDragLeave={() => setDrag(false)}
170
+ onDrop={onDropFile}
171
+ >
172
+ <div className="absolute top-1/2 left-1/2 -translate-1/2 flex flex-col items-center text-light">
173
+ <FontAwesomeIcon icon={drag ? faHandHolding : faImages} className="text-3xl" />
174
+ <p className="text-xs text-center mt-4">{drag ? "Letakkan di sini" : preview ? "Klik atau seret untuk ganti Gambar" : "Klik atau seret gambar"}</p>
175
+ </div>
176
+
177
+ <input
178
+ id={randomId}
179
+ ref={inputRef}
180
+ type="file"
181
+ accept="image/*"
182
+ className="hidden"
183
+ disabled={disabled}
184
+ onChange={onUpload}
185
+ />
186
+ </div>
187
+ </label>
188
+
189
+ {preview && !disabled && (
190
+ <IconButtonComponent
191
+ icon={faTimes}
192
+ paint="danger"
193
+ size="sm"
194
+ className="absolute top-10 right-4"
195
+ onClick={remove}
196
+ />
197
+ )}
198
+
199
+ {invalidMessage && (
200
+ <small className={cn("input-error-message", pcn<CT>(className, "error"))}>
201
+ {invalidMessage}
202
+ </small>
203
+ )}
204
+
205
+ {!isSm ? (
206
+ <ModalComponent show={openCrop} onClose={onCropCancel} title="Sesuaikan Gambar" className="w-max max-w-[350px]">
207
+ {cropSrc && (
208
+ <div className="p-4">
209
+ <CanvasCropper
210
+ src={cropSrc}
211
+ aspect={eval(aspect.replace(":", "/"))}
212
+ onDone={onCropDone}
213
+ />
214
+ </div>
215
+ )}
216
+ </ModalComponent>
217
+ ) : (
218
+ <BottomSheetComponent show={openCrop} onClose={onCropCancel} size={400}>
219
+ {cropSrc && (
220
+ <div className="p-4">
221
+ <CanvasCropper
222
+ src={cropSrc}
223
+ aspect={eval(aspect.replace(":", "/"))}
224
+ onDone={onCropDone}
225
+ />
226
+ </div>
227
+ )}
228
+ </BottomSheetComponent>
229
+ )}
230
+
231
+ </div>
232
+ );
233
+ };
234
+
235
+
236
+
237
+ interface CropperProps {
238
+ src : string;
239
+ aspect : number;
240
+ onDone ?: (file: File) => void;
241
+ }
242
+
243
+ export const CanvasCropper: React.FC<CropperProps> = ({
244
+ src,
245
+ aspect,
246
+ onDone,
247
+ }) => {
248
+ const canvasRef = useRef<HTMLCanvasElement>(null);
249
+ const previewRef = useRef<HTMLCanvasElement>(null);
250
+ const pinchDistRef = useRef(0);
251
+ const pinchStartZoomRef = useRef(1);
252
+
253
+ const [img, setImg] = useState<HTMLImageElement | null>(null);
254
+ const [zoom, setZoom] = useState(1);
255
+ const [minZoom, setMinZoom] = useState(1);
256
+ const [maxZoom, setMaxZoom] = useState(4);
257
+ const [pos, setPos] = useState({ x: 0, y: 0 });
258
+ const [dragging, setDragging] = useState(false);
259
+ const [start, setStart] = useState({ x: 0, y: 0 });
260
+
261
+ const CROP_SIZE = 280;
262
+ const CROP_W = CROP_SIZE * aspect;
263
+
264
+ const clamp = (v: number, min: number, max: number) => Math.max(min, Math.min(max, v));
265
+
266
+ useEffect(() => {
267
+ const i = new Image();
268
+ i.onload = () => setImg(i);
269
+ i.src = src;
270
+ }, [src]);
271
+
272
+ const renderCanvas = () => {
273
+ if (!img) return;
274
+
275
+ const canvas = canvasRef.current!;
276
+ const ctx = canvas.getContext("2d")!;
277
+
278
+ canvas.width = CROP_W;
279
+ canvas.height = CROP_SIZE;
280
+
281
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
282
+
283
+ const scaledW = img.width * zoom;
284
+ const scaledH = img.height * zoom;
285
+
286
+ const x = pos.x - scaledW / 2 + CROP_W / 2;
287
+ const y = pos.y - scaledH / 2 + CROP_SIZE / 2;
288
+
289
+ ctx.drawImage(img, x, y, scaledW, scaledH);
290
+ };
291
+
292
+ useEffect(() => {
293
+ renderCanvas();
294
+ }, [img, zoom, pos]);
295
+
296
+ useEffect(() => {
297
+ if (!img) return;
298
+
299
+ const imgW = img.naturalWidth;
300
+ const imgH = img.naturalHeight;
301
+
302
+ const scaleW = CROP_W / imgW;
303
+ const scaleH = CROP_SIZE / imgH;
304
+
305
+ const base = Math.max(scaleW, scaleH);
306
+
307
+ setMinZoom(base);
308
+ setZoom(base);
309
+ setMaxZoom(base * 4);
310
+
311
+ }, [img]);
312
+
313
+ const handleDown = (e: React.MouseEvent) => {
314
+ setDragging(true);
315
+ setStart({ x: e.clientX, y: e.clientY });
316
+ };
317
+
318
+ const handleUp = () => setDragging(false);
319
+
320
+ const handleMove = (e: React.MouseEvent) => {
321
+ if (!dragging) return;
322
+ const dx = e.clientX - start.x;
323
+ const dy = e.clientY - start.y;
324
+
325
+ setStart({ x: e.clientX, y: e.clientY });
326
+ setPos((p) => {
327
+ const scaledW = img!.naturalWidth * zoom;
328
+ const scaledH = img!.naturalHeight * zoom;
329
+
330
+ const minX = (scaledW - CROP_W) / 2;
331
+ const maxX = -(scaledW - CROP_W) / 2;
332
+ const minY = (scaledH - CROP_SIZE) / 2;
333
+ const maxY = -(scaledH - CROP_SIZE) / 2;
334
+
335
+ return {
336
+ x: clamp(p.x + dx, maxX, minX),
337
+ y: clamp(p.y + dy, maxY, minY),
338
+ };
339
+ });
340
+ };
341
+
342
+ const performCrop = () => {
343
+ if (!img) return;
344
+
345
+ const preview = previewRef.current!;
346
+ const pctx = preview.getContext("2d")!;
347
+
348
+ preview.width = CROP_W;
349
+ preview.height = CROP_SIZE;
350
+
351
+ const scaledW = img.width * zoom;
352
+ const scaledH = img.height * zoom;
353
+
354
+ const x = pos.x - scaledW / 2 + CROP_W / 2;
355
+ const y = pos.y - scaledH / 2 + CROP_SIZE / 2;
356
+
357
+ pctx.drawImage(img, x, y, scaledW, scaledH);
358
+
359
+ preview.toBlob((blob) => {
360
+ const file = new File([blob!], "cropped.png", { type: "image/png" });
361
+ onDone?.(file);
362
+ });
363
+ };
364
+
365
+
366
+ const handleWheel = (e: React.WheelEvent) => {
367
+ e.preventDefault();
368
+
369
+ setZoom((z) => {
370
+ let next = z - e.deltaY * 0.002;
371
+ next = Math.max(minZoom, Math.min(maxZoom, next));
372
+
373
+ const scaledW = img!.naturalWidth * next;
374
+ const scaledH = img!.naturalHeight * next;
375
+
376
+ const minX = (scaledW - CROP_W) / 2;
377
+ const maxX = -(scaledW - CROP_W) / 2;
378
+
379
+ const minY = (scaledH - CROP_SIZE) / 2;
380
+ const maxY = -(scaledH - CROP_SIZE) / 2;
381
+
382
+ setPos((p) => ({
383
+ x: clamp(p.x, maxX, minX),
384
+ y: clamp(p.y, maxY, minY),
385
+ }));
386
+
387
+ return next;
388
+ });
389
+ };
390
+
391
+
392
+ const handleTouchStart = (e: React.TouchEvent) => {
393
+ if (e.touches.length === 2) {
394
+ const t1 = e.touches[0];
395
+ const t2 = e.touches[1];
396
+
397
+ const dist = Math.hypot(
398
+ t2.clientX - t1.clientX,
399
+ t2.clientY - t1.clientY
400
+ );
401
+
402
+ pinchDistRef.current = dist;
403
+ pinchStartZoomRef.current = zoom;
404
+
405
+ e.preventDefault();
406
+ }
407
+
408
+ // Jika satu jari → drag
409
+ if (e.touches.length === 1) {
410
+ setDragging(true);
411
+ setStart({
412
+ x: e.touches[0].clientX,
413
+ y: e.touches[0].clientY,
414
+ });
415
+ }
416
+ };
417
+
418
+
419
+ const handleTouchMove = (e: React.TouchEvent) => {
420
+ if (e.touches.length === 2) {
421
+ const t1 = e.touches[0];
422
+ const t2 = e.touches[1];
423
+ const currDist = Math.hypot(t2.clientX - t1.clientX, t2.clientY - t1.clientY);
424
+
425
+ const diff = currDist - pinchDistRef.current;
426
+ let nextZoom = pinchStartZoomRef.current + diff * 0.005;
427
+
428
+ nextZoom = Math.max(minZoom, Math.min(maxZoom, nextZoom));
429
+
430
+ setZoom(nextZoom);
431
+
432
+ setPos((p) => {
433
+ const scaledW = img!.naturalWidth * nextZoom;
434
+ const scaledH = img!.naturalHeight * nextZoom;
435
+
436
+ const minX = (scaledW - CROP_W) / 2;
437
+ const maxX = -(scaledW - CROP_W) / 2;
438
+
439
+ const minY = (scaledH - CROP_SIZE) / 2;
440
+ const maxY = -(scaledH - CROP_SIZE) / 2;
441
+
442
+ return {
443
+ x: Math.max(maxX, Math.min(minX, p.x)),
444
+ y: Math.max(maxY, Math.min(minY, p.y)),
445
+ };
446
+ });
447
+
448
+ e.preventDefault();
449
+ return;
450
+ }
451
+
452
+ if (dragging && e.touches.length === 1) {
453
+ const { clientX, clientY } = e.touches[0];
454
+
455
+ const dx = clientX - start.x;
456
+ const dy = clientY - start.y;
457
+
458
+ setStart({ x: clientX, y: clientY });
459
+
460
+ setPos((p) => {
461
+ const scaledW = img!.naturalWidth * zoom;
462
+ const scaledH = img!.naturalHeight * zoom;
463
+
464
+ const minX = (scaledW - CROP_W) / 2;
465
+ const maxX = -(scaledW - CROP_W) / 2;
466
+
467
+ const minY = (scaledH - CROP_SIZE) / 2;
468
+ const maxY = -(scaledH - CROP_SIZE) / 2;
469
+
470
+ return {
471
+ x: Math.max(maxX, Math.min(minX, p.x + dx)),
472
+ y: Math.max(maxY, Math.min(minY, p.y + dy)),
473
+ };
474
+ });
475
+
476
+ e.preventDefault();
477
+ }
478
+ };
479
+
480
+
481
+ const handleTouchEnd = () => {
482
+ setDragging(false);
483
+ pinchDistRef.current = 0;
484
+ };
485
+
486
+ return (
487
+ <div style={{ touchAction: "none" }}>
488
+ <div
489
+ className="relative bg-background rounded-lg overflow-hidden mx-auto"
490
+ style={{ width: CROP_W, height: CROP_SIZE }}
491
+ onMouseMove={handleMove}
492
+ onMouseUp={handleUp}
493
+ onMouseLeave={handleUp}
494
+ onWheel={handleWheel}
495
+ onTouchStart={handleTouchStart}
496
+ onTouchMove={handleTouchMove}
497
+ onTouchEnd={handleTouchEnd}
498
+ >
499
+ <canvas
500
+ ref={canvasRef}
501
+ width={CROP_W}
502
+ height={CROP_SIZE}
503
+ onMouseDown={handleDown}
504
+ className="cursor-move"
505
+ />
506
+
507
+ <div className="absolute top-0 left-0 pointer-events-none" style={{ width: CROP_W, height: CROP_SIZE }}>
508
+ <div className="absolute inset-0 border-2 border-slate-200"></div>
509
+ <div className="absolute inset-0">
510
+ <div className="absolute top-1/3 left-0 w-full border-t border-slate-200"></div>
511
+ <div className="absolute top-2/3 left-0 w-full border-t border-slate-200"></div>
512
+ <div className="absolute left-1/3 top-0 h-full border-l border-slate-200"></div>
513
+ <div className="absolute left-2/3 top-0 h-full border-l border-slate-200"></div>
514
+ </div>
515
+ </div>
516
+ </div>
517
+
518
+ <div className="p-4 pb-2">
519
+ <ButtonComponent
520
+ label="Selesai"
521
+ variant="outline"
522
+ onClick={performCrop}
523
+ block
524
+ />
525
+ </div>
526
+
527
+ <canvas ref={previewRef} className="hidden" />
528
+ </div>
529
+ );
530
+ };
531
+
532
+
533
+