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
Add directory support for FileTrigger #5444
Changes from 2 commits
34e499f
e8d8f1f
10b8984
544d804
e50fe97
ec611bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -120,6 +120,20 @@ A file trigger can accept multiple files by passsing the `allowsMultiple` proper | |
</FileTrigger> | ||
``` | ||
|
||
## Directory Selection | ||
|
||
To enable selecting directories instead of files, use the `UNSAFE_directory` property. | ||
|
||
This reflects the webkitdirectory HTML attribute and allows users to select directories and their contents. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Feel free to make There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the comment. Link added. |
||
|
||
The `UNSAFE_` prefix is used to signify that this feature's behavior may vary across different browsers and versions. | ||
|
||
```tsx example | ||
<FileTrigger UNSAFE_directory> | ||
<Button>Select a directory</Button> | ||
</FileTrigger> | ||
``` | ||
|
||
## Media capture | ||
|
||
To specify the media capture mechanism to capture media on the spot, pass `user` for the user-facing camera or `environment` for the outward-facing camera via the `defaultCamera` prop. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,11 +35,16 @@ export interface FileTriggerProps { | |
/** | ||
* The children of the component. | ||
*/ | ||
children?: ReactNode | ||
children?: ReactNode, | ||
/** | ||
* Enables the selection of directories instead of individual files. | ||
* Note: This feature's behavior may vary across different browsers and versions. | ||
*/ | ||
UNSAFE_directory?: boolean | ||
} | ||
|
||
function FileTrigger(props: FileTriggerProps, ref: ForwardedRef<HTMLInputElement>) { | ||
let {onSelect, acceptedFileTypes, allowsMultiple, defaultCamera, children, ...rest} = props; | ||
let {onSelect, acceptedFileTypes, allowsMultiple, defaultCamera, children, UNSAFE_directory, ...rest} = props; | ||
let inputRef = useObjectRef(ref); | ||
let domProps = filterDOMProps(rest); | ||
|
||
|
@@ -62,7 +67,9 @@ function FileTrigger(props: FileTriggerProps, ref: ForwardedRef<HTMLInputElement | |
accept={acceptedFileTypes?.toString()} | ||
onChange={(e) => onSelect?.(e.target.files)} | ||
capture={defaultCamera} | ||
multiple={allowsMultiple} /> | ||
multiple={allowsMultiple} | ||
// @ts-expect-error | ||
webkitdirectory={UNSAFE_directory ? '' : undefined} /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this implementation uses an empty string for the webkitdirectory attribute instead of a boolean value because it does not function correctly with boolean. |
||
</> | ||
); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since it looks like this is well-supported with browsers we support, I think we can just use
directory
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed by the following commit.
refac: Remove unsafe prefix