-
Notifications
You must be signed in to change notification settings - Fork 155
Inputs pages #540
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
Merged
Inputs pages #540
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,101 @@ | ||
# File input | ||
|
||
<!-- [TODO] check okay, updated to point to /javascript/files rather than FileAttachment info in docs --> | ||
[API Reference ›](https://github.com/observablehq/inputs/blob/main/README.md#file) | ||
|
||
The File input specifies a local file. The exposed value provides the same interface as an Observable [file attachment](../javascript/files) for convenient parsing in various formats such as text, image, JSON, CSV, ZIP, and XLSX; however, the file is not uploaded and is only available temporarily in memory. | ||
The file input specifies a local file. The exposed value provides the same interface as an Observable [file attachment](../javascript/files) for convenient parsing in various formats such as text, image, JSON, CSV, ZIP, and XLSX; however, the file is not uploaded and is only available temporarily in memory. | ||
|
||
By default, any file is allowed, and the value of the input resolves to null. | ||
|
||
<!-- [TODO] check error, return to File input after hearing back (Fil has PR submitted) --> | ||
```js echo | ||
const file = view(Inputs.file()); | ||
``` | ||
|
||
```js echo | ||
file | ||
``` | ||
|
||
We recommend providing a *label* to improve usability. | ||
|
||
Specify the *accept* option to limit the allowed file extensions. This is useful when you intend to parse the file as a specific file format, such as CSV. By setting the *required* option to true, the value of the input won’t resolve until the user choses a file. | ||
|
||
```js echo | ||
const file = view(Inputs.file()) | ||
``` | ||
const csvfile = view(Inputs.file({label: "CSV file", accept: ".csv", required: true})); | ||
``` | ||
|
||
Once a file has been selected, you can read its contents like so: | ||
|
||
|
||
```js echo | ||
const data = display(await csvfile.csv({typed: true})); | ||
``` | ||
|
||
Here are examples of other supported file types. | ||
|
||
```js echo | ||
const jsonfile = view(Inputs.file({label: "JSON file", accept: ".json", required: true})); | ||
``` | ||
|
||
```js echo | ||
const data = display(await jsonfile.json()); | ||
``` | ||
|
||
```js echo | ||
const textfile = view(Inputs.file({label: "Text file", accept: ".txt", required: true})); | ||
``` | ||
|
||
```js echo | ||
const data = display(await textfile.text()); | ||
``` | ||
|
||
```js echo | ||
const imgfile = view(Inputs.file({label: "Image file", accept: ".png,.jpg", required: true})); | ||
``` | ||
|
||
```js echo | ||
const image = display(await imgfile.image()); | ||
``` | ||
|
||
```js echo | ||
const xlsxfile = view(Inputs.file({label: "Excel file", accept: ".xlsx", required: true})); | ||
``` | ||
|
||
```js echo | ||
const workbook = display(await xlsxfile.xlsx()); | ||
``` | ||
|
||
```js echo | ||
const zipfile = view(Inputs.file({label: "ZIP archive", accept: ".zip", required: true})); | ||
``` | ||
|
||
```js echo | ||
const archive = display(await zipfile.zip()) | ||
``` | ||
|
||
The *multiple* option allows the user to pick multiple files. In this mode, the exposed value is an array of files instead of a single file. | ||
|
||
```js echo | ||
const files = view(Inputs.file({label: "Files", multiple: true})); | ||
``` | ||
|
||
```js echo | ||
files | ||
``` | ||
|
||
## Options | ||
|
||
**Inputs.file(*options*)** | ||
|
||
The available file input options are: | ||
|
||
* *label* - a label; either a string or an HTML element. | ||
* *required* - if true, a valid file must be selected. | ||
* *validate* - a function to check whether the file input is valid. | ||
* *accept* - the [acceptable file types](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#accept). | ||
* *capture* - for [capturing image or video data](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#capture). | ||
* *multiple* - whether to allow multiple files to be selected; defaults to false. | ||
* *width* - the width of the input (not including the label). | ||
* *disabled* - whether input is disabled; defaults to false. | ||
|
||
Note that the value of file input cannot be set programmatically; it can only be changed by the user. | ||
|
||
<!-- TODO check: Delete? (In vanilla JavaScript, the Inputs.file method is not exposed directly. Instead, an Inputs.fileOf method is exposed which takes an AbstractFile implementation and returns the Inputs.file method. This avoids a circular dependency between Observable Inputs and the Observable standard library.)--> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
this would be lovely, but depends on d3/d3-scale-chromatic#51