Skip to content

Commit 954360a

Browse files
fix(form): Do not crash when cleaning file fields (#75929)
1 parent 5c3e33a commit 954360a

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

static/app/components/forms/fields/fileField.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,22 @@ export interface FileFieldProps extends Omit<InputFieldProps, 'type' | 'accept'>
2727

2828
export default function FileField({accept, hideControlState, ...props}: FileFieldProps) {
2929
const [fileName, setFileName] = useState('');
30-
const handleFile = (model, name, onChange, e) => {
31-
const file = e.target.files[0];
30+
const handleFile = (
31+
model: FormModel,
32+
name: string,
33+
onChange: (value: any, e: React.ChangeEvent<HTMLInputElement>) => void,
34+
e: React.ChangeEvent<HTMLInputElement>
35+
) => {
36+
const file = e.target.files?.[0];
37+
38+
// No file selected
39+
if (!file) {
40+
onChange([], e);
41+
return;
42+
}
3243

33-
const reader = new FileReader();
3444
model.setSaving(name, true);
45+
const reader = new FileReader();
3546
reader.addEventListener(
3647
'load',
3748
() => {
@@ -56,7 +67,7 @@ export default function FileField({accept, hideControlState, ...props}: FileFiel
5667
children: React.ReactNode;
5768
model: FormModel;
5869
name: string;
59-
onChange: (value, event?: React.FormEvent<HTMLInputElement>) => void;
70+
onChange: (value: any, event?: React.FormEvent<HTMLInputElement>) => void;
6071
}) => {
6172
return (
6273
<InputGroup>

0 commit comments

Comments
 (0)