deepsea-components 5.9.7 → 5.10.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.
@@ -0,0 +1,104 @@
1
+ "use client"
2
+
3
+ import { ComponentProps, ComponentRef, createElement, DragEvent, Fragment, JSX, MouseEvent as ReactMouseEvent, useRef, useState } from "react"
4
+ import { FileType, getFileData, InputFile, InputFileBaseProps, InputFileDataType, InputFileDataTypeMap, InputFileExtraProps, ValueType } from "./InputFile"
5
+
6
+ export type InputFileButtonProps<
7
+ Multiple extends boolean = false,
8
+ Type extends InputFileDataType = "file",
9
+ AS extends keyof JSX.IntrinsicElements = "button",
10
+ > = Omit<ComponentProps<AS>, "type" | "disabled"> &
11
+ InputFileExtraProps<Multiple, Type> & {
12
+ disabled?: boolean
13
+ inputProps?: Omit<InputFileBaseProps, "accept">
14
+ accept?: string
15
+ dragFile?: boolean
16
+ as?: AS
17
+ }
18
+
19
+ /** 专用于读取文件的 button 组件 */
20
+ export function InputFileButton<Multiple extends boolean = false, Type extends InputFileDataType = "file", AS extends keyof JSX.IntrinsicElements = "button">(
21
+ props: InputFileButtonProps<Multiple, Type, AS>,
22
+ ) {
23
+ const {
24
+ as = "button",
25
+ onClick: _onClick,
26
+ inputProps = {},
27
+ accept,
28
+ onDrop: _onDrop,
29
+ onDragOver: _onDragOver,
30
+ dragFile,
31
+ disabled: _disabled,
32
+ type = "file",
33
+ multiple,
34
+ onValueChange,
35
+ onFileChange,
36
+ clearAfterChange,
37
+ ...rest
38
+ } = props
39
+
40
+ const { style, disabled: __disabled, ...restInputProps } = inputProps
41
+ const [disabled, setDisabled] = useState(false)
42
+ const input = useRef<HTMLInputElement>(null)
43
+
44
+ function onClick(e: ReactMouseEvent<ComponentRef<AS>, MouseEvent>) {
45
+ input.current?.click()
46
+ _onClick?.(e as any)
47
+ }
48
+
49
+ async function onDrop(e: DragEvent<ComponentRef<AS>>) {
50
+ _onDrop?.(e as any)
51
+ if (disabled || !dragFile) return
52
+ e.preventDefault()
53
+ const { files } = e.dataTransfer
54
+ if (!files || files.length === 0) return
55
+ setDisabled(true)
56
+ try {
57
+ if (multiple) {
58
+ const files2: File[] = Array.from(files)
59
+ const values: InputFileDataTypeMap[Type][] = []
60
+ for (const file of files2) {
61
+ const value = (await getFileData(file, type)) as InputFileDataTypeMap[Type]
62
+ values.push(value)
63
+ }
64
+ onFileChange?.(files2 as FileType<Multiple>)
65
+ onValueChange?.(values as ValueType<Multiple, Type>)
66
+ } else {
67
+ const file = files[0]
68
+ onFileChange?.(file as FileType<Multiple>)
69
+ onValueChange?.((await getFileData(file, type)) as ValueType<Multiple, Type>)
70
+ }
71
+ } finally {
72
+ setDisabled(false)
73
+ }
74
+ }
75
+
76
+ function onDragOver(e: DragEvent<ComponentRef<AS>>) {
77
+ _onDragOver?.(e as any)
78
+ if (disabled || !dragFile) return
79
+ e.preventDefault()
80
+ }
81
+
82
+ return (
83
+ <Fragment>
84
+ <InputFile<Multiple, Type>
85
+ disabled={disabled || _disabled || __disabled}
86
+ style={{ display: "none", ...style }}
87
+ multiple={multiple}
88
+ accept={accept}
89
+ type={type as Type}
90
+ onValueChange={onValueChange}
91
+ onFileChange={onFileChange}
92
+ clearAfterChange={clearAfterChange}
93
+ {...restInputProps}
94
+ />
95
+ {createElement(as, {
96
+ disabled: disabled || _disabled,
97
+ onClick,
98
+ onDrop,
99
+ onDragOver,
100
+ ...rest,
101
+ })}
102
+ </Fragment>
103
+ )
104
+ }
package/src/index.ts CHANGED
@@ -11,6 +11,7 @@ export * from "@/components/HlsPlayer"
11
11
  export * from "@/components/ImportExcel"
12
12
  export * from "@/components/InfiniteScroll"
13
13
  export * from "@/components/InputFile"
14
+ export * from "@/components/InputFileButton"
14
15
  export * from "@/components/LoopSwiper"
15
16
  export * from "@/components/Ring"
16
17
  export * from "@/components/Scroll"