-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
140 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
2 changes: 1 addition & 1 deletion
2
...ontent/docs/reference/forms/components.md → .../reference/forms/basic-form-components.md
This file contains 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,5 +1,5 @@ | ||
--- | ||
title: Components | ||
title: Basic Form Components | ||
description: HTML like form for Astro | ||
--- | ||
|
||
|
This file contains 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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
--- | ||
title: Client Side Events & Triggers | ||
description: Forms client side events & submit triggers | ||
--- | ||
|
||
## Client Side Events | ||
|
||
You can listen to submit events to handle the loading while the form is being submitted. | ||
|
||
```ts | ||
type SubmitInfo = { | ||
button: HTMLButtonElement, | ||
uploads: {count: number, totalSize: number} // only for big files | ||
}; | ||
|
||
document.addEventListener('WFSubmitting', ({detail}: {details: SubmitInfo}) => { | ||
const {button, uploads} = details; | ||
|
||
button.classList.add('loading'); | ||
console.log('Form is being submitted'); | ||
}); | ||
``` | ||
|
||
## Submit Triggers | ||
|
||
If you want custom event to submit the form, you can use the `submitForm` global function. | ||
|
||
```astro | ||
--- | ||
import { Bind, BButton, BindForm } from '@astro-utils/forms/forms.js'; | ||
const form = Bind(); | ||
function onSubmit(){ | ||
console.log(form.select); | ||
} | ||
--- | ||
<BindForm bind={form}> | ||
<BSelect name="select" required onchange="submitForm(this)"> | ||
<BOption value="1">Option 1</BOption> | ||
<BOption value="2">Option 2</BOption> | ||
</BSelect> | ||
<BButton onClick={onSubmit} class="hidden">This button is hidden</BButton> | ||
</BindForm> | ||
``` | ||
|
||
|
||
### Select button | ||
It will try to select the best BButton in this BindForm. | ||
|
||
The order of selection is: | ||
1. The last BButton with the `whenFormOK` attribute. | ||
2. Last BButton in the form. | ||
|
||
You can also manually configure that by adding the `data-submit` attribute to the select. | ||
|
||
```astro | ||
<BSelect name="select" required onchange="submitForm(this)" data-submit="consoleItButton"> | ||
<BOption value="1">Option 1</BOption> | ||
<BOption value="2">Option 2</BOption> | ||
</BSelect> | ||
<BButton id="consoleItButton" onClick={onSubmit} class="hidden">This button is hidden</BButton> | ||
``` |
This file contains 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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
--- | ||
title: Upload Big File Component | ||
description: Uploading Big Files with Astro Utils | ||
--- | ||
|
||
## Big File Upload | ||
|
||
The big file upload component is a simple way to upload large files to the server. It uses the `xhr` API to upload the file in chunks (and not all at once). This way, the server can handle large files without running out of memory. | ||
|
||
It is also able to show the progress of the upload and handle errors (in the server side). | ||
|
||
## Components | ||
|
||
### UploadBigFile | ||
|
||
Same as the `BInput` component, but for big files. | ||
|
||
```astro | ||
--- | ||
import { BigFile, Bind, FormErrors, BButton } from '@astro-utils/forms/forms.js'; | ||
type Form = { | ||
file: BigFile | ||
showSubmitText: string; | ||
} | ||
const bind = Bind<Form>(); | ||
function formSubmit(){ | ||
showSubmitText = `You uploaded ${form.file.name} (${form.file.size} bytes)`; | ||
} | ||
--- | ||
<BindForm bind={bind}> | ||
<FormErrors /> | ||
<p>{showSubmitText}</p> | ||
<UploadBigFile name="file" required/> | ||
<BButton onClick={formSubmit} whenFormOK>Submit</BButton> | ||
</BindForm> | ||
``` | ||
|
||
### UploadBigFileProgress | ||
|
||
A progress bar connected to a `UploadBigFile` component. | ||
|
||
```astro | ||
<UploadBigFile name="file" required/> | ||
<UploadBigFileProgress for="file" /> | ||
``` | ||
|
||
## Configuration | ||
|
||
All configuration is done through the middleware. The following options are available: | ||
|
||
```ts | ||
type MiddlewareOptions = { | ||
forms?: { | ||
bigFilesUpload?: { | ||
bigFileClientOptions?: { | ||
retryChunks?: number; // default: 5 | ||
chunkSize?: number; // default: 5MB | ||
parallelChunks?: number; // default: 3 | ||
parallelUploads?: number; // default: 3 | ||
}, | ||
bigFileServerOptions?: { | ||
maxUploadTime?: number; // default: 1.5 hours | ||
maxUploadSize?: number; // default: 1GB | ||
maxDirectorySize?: number; // default: 50GB | ||
tempDirectory?: string; // default: path.join(os.tmpdir(), "astro_forms_big_files_uploads") | ||
}; | ||
} | ||
} | ||
} | ||
``` |