Skip to content
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

feat!: Expose FileInput component ref with a clearFiles method #1165

Merged
merged 7 commits into from
Jun 10, 2021

Conversation

suzubara
Copy link
Contributor

@suzubara suzubara commented Apr 22, 2021

Summary

This PR modifies the FileInput component to use React.forwardRef and the useImperativeHandle hook to expose the underlying <input type="file" /> element, a method to clear the files state, and the value of the files state.

Related Issues or PRs

Fixes #958

How To Test

In an application, try passing a ref into the FileInput component, and then use ref.current.files and ref.current.clearFiles() to interact with the file input.

Screenshots (optional)

Apr-23-2021 11-53-41

@suzubara suzubara added type: feature New feature or request type: breaking This introduces breaking changes and will require a major version increase labels Apr 22, 2021
@trussworks-infra-zz trussworks-infra-zz temporarily deployed to storybook April 22, 2021 19:04 Inactive
@trussworks-infra-zz trussworks-infra-zz temporarily deployed to storybook April 23, 2021 15:58 Inactive
@trussworks-infra-zz trussworks-infra-zz temporarily deployed to storybook April 23, 2021 16:53 Inactive
@suzubara suzubara changed the title Use forwardRef on FileInput, add clearFiles method feat: Expose FileInput component ref with a clearFiles method Apr 23, 2021
@suzubara suzubara requested a review from haworku April 23, 2021 16:58
Copy link
Contributor

@haworku haworku left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took a look - lets screenshare if it seems useful

src/components/forms/FileInput/FileInput.tsx Show resolved Hide resolved
src/components/forms/FileInput/FileInput.tsx Outdated Show resolved Hide resolved
disabled={disabled}
onChange={handleChange}
multiple={multiple}
accept={accept}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting a type error here for enterKeyHint being a missing prop as soon I added the new FileInput to the application.

Adding that prop with the value of enter (default) resolved the issue. It sounded similar to this issue. 🤔 Not sure what to make of this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops this was an issue in our code. SMH excuse me.

@suzubara suzubara marked this pull request as ready for review May 6, 2021 18:53
@trussworks-infra-zz trussworks-infra-zz temporarily deployed to storybook May 6, 2021 18:56 Inactive
Copy link
Contributor

@haworku haworku left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works in mc-review. ⭐ The key thing missing in our code was changing everything to use ref.current?.input?.files instead of ref.current.files.

@trussworks-infra-zz trussworks-infra-zz temporarily deployed to storybook May 6, 2021 21:11 Inactive
@haworku haworku changed the base branch from main to V2-cherry-picked June 2, 2021 16:50
@brandonlenz brandonlenz changed the title feat: Expose FileInput component ref with a clearFiles method feat!: Expose FileInput component ref with a clearFiles method Jun 2, 2021
@brandonlenz
Copy link
Contributor

I updated the title to include a "!" for the breaking change so that the squash will automatically preserve that. Feel free also when merging to add a helpful blurb about how to incorporate this change in the body of the squash commit message.

@brandonlenz
Copy link
Contributor

brandonlenz commented Jun 2, 2021

This works in mc-review. ⭐ The key thing missing in our code was changing everything to use ref.current?.input?.files instead of ref.current.files.

@haworku Maybe a question more for my understanding, but the FileInputRef currently looks like this:

export type FileInputRef = {
  clearFiles: () => void
  input: HTMLInputElement | null
  files: File[]
}

shouldn't files be accessible from the ref directly as you were attempting to do?

@trussworks-infra-zz trussworks-infra-zz temporarily deployed to storybook June 7, 2021 19:49 Inactive
@haworku haworku force-pushed the sr-file-input-forward-ref-958 branch from 005f324 to 4f902e5 Compare June 7, 2021 20:02
@trussworks-infra-zz trussworks-infra-zz temporarily deployed to storybook June 7, 2021 20:28 Inactive
@brandonlenz
Copy link
Contributor

We may want to adjust the storybook story to make sure that the ref's files prop we are exposing is what is being used. This can be a future item since the functionality of the Component will not need to change.

@brandonlenz brandonlenz merged commit 0bc0b54 into V2-cherry-picked Jun 10, 2021
@brandonlenz brandonlenz deleted the sr-file-input-forward-ref-958 branch June 10, 2021 17:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: breaking This introduces breaking changes and will require a major version increase type: feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[feat] FileInput ref should expose a reset prop to clear its value
4 participants