Skip to content

Cannot reset file input #287

@katywings

Description

@katywings

IS

If you reset a file input by setting its value to "" and dispatching an input event, modular-forms keeps the previously selected file in the form state.

SHOULD

Resetting the file input value with the mentioned logic, should result in the value being cleared in the form state.

Extra information

The reason for this happening is in the following lines:

  1. files[0] is undefined for an empty file input:

  2. since the value from getElementInput is undefined, this is gonna fallback to prevValue:

Workaround

Add the following transform to your file fields.

export const toOptionalFile = (value: any, evt: FieldEvent) => {
  const input = evt.target as HTMLInputElement;
  if (!input.files?.length) return;
  return value
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions