/
FileInput.tsx
174 lines (160 loc) · 4.49 KB
/
FileInput.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import type { InputHTMLAttributes, ReactNode } from "react";
import { forwardRef } from "react";
import cn from "classnames";
import type { ButtonThemeProps } from "@react-md/button";
import { buttonThemeClassNames } from "@react-md/button";
import { TextIconSpacing, useIcon } from "@react-md/icon";
import { useInteractionStates } from "@react-md/states";
import { SrOnly } from "@react-md/typography";
import { bem } from "@react-md/utils";
type InputAttributes = Omit<
InputHTMLAttributes<HTMLInputElement>,
"type" | "defaultValue" | "value"
>;
export interface FileInputProps extends ButtonThemeProps, InputAttributes {
/**
* An id for the input. This is required for a11y since it also is applied as
* the `htmlFor` prop for the label.
*/
id: string;
/**
* The change event handler to attach to this input. This is required since
* there's really no use in this component otherwise.
*/
onChange: React.ChangeEventHandler<HTMLInputElement>;
/**
* An optional icon to display for the file input.
*/
icon?: ReactNode;
/**
* Boolean if the icon should appear after the children in the label.
*/
iconAfter?: boolean;
/**
* Boolean if the children should not have some spacing between the icon and
* itself. The default behavior is to use the `<TextIconSpacing>` component
* for text styled input buttons, but this can be disabled if you want to use
* a screen-reader only accessible label.
*
* Note: This will default to `false` if {@link children} are provided.
*
* @defaultValue `true`
*/
disableIconSpacing?: boolean;
/**
* Boolean if the file input should no longer allow the same file to be
* selected multiple times and trigger the `onChange` each time it is
* selected.
*/
disableRepeatableFiles?: boolean;
/**
* Children should generally be provided when the {@link buttonType} is
* set to `"text"`. This defaults to a screen-reader only accessible text.
*
* @defaultValue `<SrOnly>Upload</SrOnly>`
*/
children?: ReactNode;
}
const block = bem("rmd-file-input");
/**
* This component is a wrapper for the `<input type="file" />` that can be themed
* like a button.
*/
export const FileInput = forwardRef<HTMLInputElement, FileInputProps>(
function FileInput(
{
style,
className: propClassName,
icon: propIcon,
iconAfter = false,
children: propChildren,
theme = "primary",
themeType = "contained",
buttonType = propChildren ? "text" : "icon",
multiple = false,
disableIconSpacing: propDisableIconSpacing,
disableRepeatableFiles = false,
onKeyDown,
onKeyUp,
onMouseDown,
onMouseUp,
onMouseLeave,
onClick,
onTouchStart,
onTouchMove,
onTouchEnd,
onChange,
...props
},
ref
) {
const { id, disabled } = props;
const icon = useIcon("upload", propIcon);
const disableIconSpacing =
propDisableIconSpacing ?? typeof propChildren === "undefined";
let children = propChildren;
if (typeof propChildren === "undefined") {
children = <SrOnly>Upload</SrOnly>;
}
const { ripples, className, handlers } = useInteractionStates({
handlers: {
onKeyDown,
onKeyUp,
onMouseDown,
onMouseUp,
onMouseLeave,
onClick,
onTouchStart,
onTouchMove,
onTouchEnd,
},
className: buttonThemeClassNames({
theme,
themeType,
buttonType,
disabled,
className: propClassName,
}),
// pressing enter or space would trigger two click events otherwise.
disableEnterClick: true,
});
let content: ReactNode = icon;
if (disableIconSpacing || (children && !icon)) {
content = (
<>
{!iconAfter && icon}
{children}
{iconAfter && icon}
</>
);
} else if (children) {
content = (
<TextIconSpacing icon={icon} iconAfter={iconAfter}>
{children}
</TextIconSpacing>
);
}
return (
<>
<input
{...props}
{...handlers}
ref={ref}
onChange={onChange}
value={disableRepeatableFiles ? undefined : ""}
type="file"
className={block()}
multiple={multiple}
/>
<label
htmlFor={id}
style={style}
className={cn("rmd-file-input-label", className)}
>
{content}
{ripples}
</label>
</>
);
}
);