Skip to content

useFileDialog | Make custom input less eager for late initilised template refs #4867

@rory-instil

Description

@rory-instil

Clear and concise description of the problem

Currently useFileDialog accepts HTMLInputElements for custom input elements.
However this unwraps the ref which when using it as a template ref will be undefined until after mounting.
This means useFileDialog is used in the setup code then the input will always be undefined and a new input created using the document.createElement fallback.

  let input: HTMLInputElement | undefined
  if (document) {
    input = unrefElement(options.input) || document.createElement('input') // 👈 this will unref the element which will be `undefined`
    input.type = 'file'

    input.onchange = (event: Event) => {
      const result = event.target as HTMLInputElement
      files.value = result.files
      changeTrigger(files.value)
    }

    input.oncancel = () => {
      cancelTrigger()
    }
  }

Suggested solution

Allow the custom input to be MaybeElementRef<HTMLInputElement | undefined> and watch for changes instead

Alternative

No response

Additional context

No response

Validations

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