Skip to content

fix(form): Do not crash when cleaning file fields #75929

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions static/app/components/forms/fields/fileField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,22 @@ export interface FileFieldProps extends Omit<InputFieldProps, 'type' | 'accept'>

export default function FileField({accept, hideControlState, ...props}: FileFieldProps) {
const [fileName, setFileName] = useState('');
const handleFile = (model, name, onChange, e) => {
const file = e.target.files[0];
const handleFile = (
model: FormModel,
name: string,
onChange: (value: any, e: React.ChangeEvent<HTMLInputElement>) => void,
e: React.ChangeEvent<HTMLInputElement>
) => {
const file = e.target.files?.[0];

// No file selected
if (!file) {
onChange([], e);
return;
}

const reader = new FileReader();
model.setSaving(name, true);
const reader = new FileReader();
reader.addEventListener(
'load',
() => {
Expand All @@ -56,7 +67,7 @@ export default function FileField({accept, hideControlState, ...props}: FileFiel
children: React.ReactNode;
model: FormModel;
name: string;
onChange: (value, event?: React.FormEvent<HTMLInputElement>) => void;
onChange: (value: any, event?: React.FormEvent<HTMLInputElement>) => void;
}) => {
return (
<InputGroup>
Expand Down
Loading