deepsea-components 5.9.7 → 5.10.0

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