@mbao01/common 0.0.36 → 0.0.37

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.
@@ -5,7 +5,7 @@ export declare const FileUploader: {
5
5
  displayName: string;
6
6
  Content: import("react").ForwardRefExoticComponent<import("react").HTMLAttributes<HTMLDivElement> & import("react").RefAttributes<HTMLDivElement>>;
7
7
  Input: {
8
- ({ className, children, ...props }: FileUploaderInputProps): import("react/jsx-runtime").JSX.Element;
8
+ ({ classes, children, ...props }: FileUploaderInputProps): import("react/jsx-runtime").JSX.Element;
9
9
  displayName: string;
10
10
  };
11
11
  Item: import("react").ForwardRefExoticComponent<{
@@ -7,7 +7,14 @@ export type FileUploaderProps = {
7
7
  dropzoneOptions: DropzoneOptions;
8
8
  orientation?: "horizontal" | "vertical";
9
9
  } & React.HTMLAttributes<HTMLDivElement>;
10
- export type FileUploaderInputProps = React.InputHTMLAttributes<HTMLInputElement>;
10
+ export type FileUploaderInputProps = Omit<React.InputHTMLAttributes<HTMLInputElement>, "className"> & {
11
+ classes?: Partial<{
12
+ all: string;
13
+ accepted: string;
14
+ rejected: string;
15
+ default: string;
16
+ }>;
17
+ };
11
18
  export type DirectionOptions = "rtl" | "ltr" | undefined;
12
19
  export type FileUploaderContextType = {
13
20
  dropzoneState: DropzoneState;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mbao01/common",
3
3
  "private": false,
4
- "version": "0.0.36",
4
+ "version": "0.0.37",
5
5
  "type": "module",
6
6
  "author": "Ayomide Bakare",
7
7
  "license": "MIT",
@@ -145,5 +145,5 @@
145
145
  "react-dom": "^18.2.0",
146
146
  "typescript": "^5.2.2"
147
147
  },
148
- "gitHead": "56575cccb960383ac1b9397068161bbd1b25a477"
148
+ "gitHead": "dc6e10ed230647d7e4626895c8d9b6a6808daab5"
149
149
  }
@@ -55,54 +55,67 @@ export const FileUploader = ({
55
55
 
56
56
  const handleKeyDown = useCallback(
57
57
  (e: React.KeyboardEvent<HTMLDivElement>) => {
58
- e.preventDefault();
59
- e.stopPropagation();
60
-
61
- if (!value) return;
62
-
63
- const moveNext = () => {
64
- const nextIndex = activeIndex + 1;
65
- setActiveIndex(nextIndex > value.length - 1 ? 0 : nextIndex);
66
- };
67
-
68
- const movePrev = () => {
69
- const nextIndex = activeIndex - 1;
70
- setActiveIndex(nextIndex < 0 ? value.length - 1 : nextIndex);
71
- };
72
-
73
- const prevKey =
74
- orientation === "horizontal"
75
- ? direction === "ltr"
76
- ? "ArrowLeft"
77
- : "ArrowRight"
78
- : "ArrowUp";
79
-
80
- const nextKey =
81
- orientation === "horizontal"
82
- ? direction === "ltr"
83
- ? "ArrowRight"
84
- : "ArrowLeft"
85
- : "ArrowDown";
86
-
87
- if (e.key === nextKey) {
88
- moveNext();
89
- } else if (e.key === prevKey) {
90
- movePrev();
91
- } else if (e.key === "Enter" || e.key === "Space") {
92
- if (activeIndex === -1) {
93
- dropzoneState.inputRef.current?.click();
94
- }
95
- } else if (e.key === "Delete" || e.key === "Backspace") {
96
- if (activeIndex !== -1) {
97
- removeFileFromSet(activeIndex);
98
- if (value.length - 1 === 0) {
99
- setActiveIndex(-1);
100
- return;
101
- }
58
+ const files = value ?? [];
59
+ const KEY_LIST = [
60
+ "ArrowLeft",
61
+ "ArrowRight",
62
+ "ArrowUp",
63
+ "ArrowDown",
64
+ "Enter",
65
+ "Space",
66
+ "Delete",
67
+ "Backspace",
68
+ "Escape",
69
+ ];
70
+
71
+ if (KEY_LIST.includes(e.key)) {
72
+ e.preventDefault();
73
+ e.stopPropagation();
74
+
75
+ const moveNext = () => {
76
+ const nextIndex = activeIndex + 1;
77
+ setActiveIndex(nextIndex > files.length - 1 ? 0 : nextIndex);
78
+ };
79
+
80
+ const movePrev = () => {
81
+ const nextIndex = activeIndex - 1;
82
+ setActiveIndex(nextIndex < 0 ? files.length - 1 : nextIndex);
83
+ };
84
+
85
+ const prevKey =
86
+ orientation === "horizontal"
87
+ ? direction === "ltr"
88
+ ? "ArrowLeft"
89
+ : "ArrowRight"
90
+ : "ArrowUp";
91
+
92
+ const nextKey =
93
+ orientation === "horizontal"
94
+ ? direction === "ltr"
95
+ ? "ArrowRight"
96
+ : "ArrowLeft"
97
+ : "ArrowDown";
98
+
99
+ if (e.key === nextKey) {
100
+ moveNext();
101
+ } else if (e.key === prevKey) {
102
102
  movePrev();
103
+ } else if (e.key === "Enter" || e.key === "Space") {
104
+ if (activeIndex === -1) {
105
+ dropzoneState.inputRef.current?.click();
106
+ }
107
+ } else if (e.key === "Delete" || e.key === "Backspace") {
108
+ if (activeIndex !== -1) {
109
+ removeFileFromSet(activeIndex);
110
+ if (files.length - 1 === 0) {
111
+ setActiveIndex(-1);
112
+ return;
113
+ }
114
+ movePrev();
115
+ }
116
+ } else if (e.key === "Escape") {
117
+ setActiveIndex(-1);
103
118
  }
104
- } else if (e.key === "Escape") {
105
- setActiveIndex(-1);
106
119
  }
107
120
  },
108
121
  // eslint-disable-next-line react-hooks/exhaustive-deps
@@ -197,17 +210,13 @@ export const FileUploader = ({
197
210
  }}
198
211
  >
199
212
  <div
200
- tabIndex={0}
201
213
  onKeyDownCapture={handleKeyDown}
202
- className={cn(
203
- "grid w-full focus:outline-none overflow-hidden ",
204
- className,
205
- {
206
- "gap-2": value && value.length > 0,
207
- }
208
- )}
214
+ className={cn("grid w-full overflow-hidden", className, {
215
+ "gap-2": value && value.length > 0,
216
+ })}
209
217
  dir={dir}
210
218
  {...props}
219
+ tabIndex={-1}
211
220
  >
212
221
  {children}
213
222
  </div>
@@ -257,9 +266,9 @@ const FileUploaderItem = forwardRef<
257
266
  <div
258
267
  ref={ref}
259
268
  className={cn(
260
- "h-6 p-1 justify-between cursor-pointer relative",
261
- className,
262
- isSelected ? "bg-muted" : ""
269
+ "h-6 p-1 justify-between cursor-pointer relative rounded",
270
+ { "bg-base-300": isSelected },
271
+ className
263
272
  )}
264
273
  {...props}
265
274
  >
@@ -275,7 +284,12 @@ const FileUploaderItem = forwardRef<
275
284
  onClick={() => removeFileFromSet(index)}
276
285
  >
277
286
  <span className="sr-only">remove item {index}</span>
278
- <TrashIcon className="w-4 h-4 hover:stroke-destructive duration-200 ease-in-out" />
287
+ <TrashIcon
288
+ className={cn(
289
+ "w-4 h-4 shrink-0 hover:stroke-destructive duration-200 ease-in-out",
290
+ { "text-error": isSelected }
291
+ )}
292
+ />
279
293
  </button>
280
294
  </div>
281
295
  );
@@ -284,7 +298,7 @@ const FileUploaderItem = forwardRef<
284
298
  FileUploaderItem.displayName = "FileUploaderItem";
285
299
 
286
300
  const FileUploaderInput = ({
287
- className,
301
+ classes,
288
302
  children,
289
303
  ...props
290
304
  }: FileUploaderInputProps) => {
@@ -292,39 +306,35 @@ const FileUploaderInput = ({
292
306
  useFileUpload();
293
307
  const rootProps = isLOF ? {} : dropzoneState.getRootProps();
294
308
  return (
295
- <div
296
- {...props}
297
- className={`relative w-full ${
298
- isLOF ? "opacity-50 cursor-not-allowed " : "cursor-pointer "
299
- }`}
300
- >
309
+ <div {...props} className="relative w-full">
301
310
  <div
302
311
  className={cn(
303
- `w-full rounded-lg duration-300 ease-in-out
304
- ${
305
- dropzoneState.isDragAccept
306
- ? "border-green-500"
307
- : dropzoneState.isDragReject || isFileTooBig
308
- ? "border-red-500"
309
- : "border-gray-300"
310
- }`,
311
- className
312
+ classes?.all,
313
+ "w-full rounded-lg duration-300 ease-in-out",
314
+ isLOF ? "opacity-50 cursor-not-allowed" : "cursor-pointer",
315
+ dropzoneState.isDragAccept
316
+ ? classes?.accepted
317
+ : dropzoneState.isDragReject || isFileTooBig
318
+ ? classes?.rejected
319
+ : classes?.default
312
320
  )}
313
321
  {...rootProps}
314
322
  >
315
323
  {children}
316
324
  </div>
317
325
  <input
318
- {...props}
319
326
  ref={hiddenInputRef}
327
+ {...props}
320
328
  type="file"
321
- style={{ opacity: 0, display: "none" }}
329
+ tabIndex={-1}
330
+ className="hidden opacity-0"
322
331
  />
323
332
  <input
324
- disabled={isLOF}
325
333
  ref={dropzoneState.inputRef}
334
+ disabled={isLOF}
326
335
  {...dropzoneState.getInputProps()}
327
- className={`${isLOF ? "cursor-not-allowed" : ""}`}
336
+ tabIndex={-1}
337
+ className={cn({ "cursor-not-allowed": isLOF })}
328
338
  />
329
339
  </div>
330
340
  );
@@ -9,8 +9,17 @@ export type FileUploaderProps = {
9
9
  orientation?: "horizontal" | "vertical";
10
10
  } & React.HTMLAttributes<HTMLDivElement>;
11
11
 
12
- export type FileUploaderInputProps =
13
- React.InputHTMLAttributes<HTMLInputElement>;
12
+ export type FileUploaderInputProps = Omit<
13
+ React.InputHTMLAttributes<HTMLInputElement>,
14
+ "className"
15
+ > & {
16
+ classes?: Partial<{
17
+ all: string;
18
+ accepted: string;
19
+ rejected: string;
20
+ default: string;
21
+ }>;
22
+ };
14
23
 
15
24
  export type DirectionOptions = "rtl" | "ltr" | undefined;
16
25