@skalfa/skalfa-component 1.0.7 → 1.0.8

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