-
-
Notifications
You must be signed in to change notification settings - Fork 52
devex: Create and document file selection components #2654
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
68beff9
document file list
SuaYoo 422a47e
create file input component
SuaYoo 0d08d3b
clean up file list component
SuaYoo 6d7d4e2
fix lint issue
SuaYoo fe7bd09
switch to form control
SuaYoo 930570c
update uploads
SuaYoo 29f3503
update file format story
SuaYoo 7f73cb7
fix selection in chrome and add label
SuaYoo 87c44d8
make entire dropzone clickable
SuaYoo 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 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 |
---|---|---|
@@ -0,0 +1,241 @@ | ||
import { localized } from "@lit/localize"; | ||
import clsx from "clsx"; | ||
import { html, nothing, type PropertyValues } from "lit"; | ||
import { customElement, property, query, state } from "lit/decorators.js"; | ||
import { ifDefined } from "lit/directives/if-defined.js"; | ||
import { repeat } from "lit/directives/repeat.js"; | ||
import { without } from "lodash/fp"; | ||
|
||
import type { | ||
BtrixFileChangeEvent, | ||
BtrixFileRemoveEvent, | ||
} from "./file-list/events"; | ||
|
||
import { TailwindElement } from "@/classes/TailwindElement"; | ||
import { FormControl } from "@/mixins/FormControl"; | ||
import { tw } from "@/utils/tailwind"; | ||
|
||
import "@/components/ui/file-list"; | ||
|
||
const droppingClass = tw`bg-slate-100`; | ||
|
||
/** | ||
* Allow attaching one or more files. | ||
* | ||
* @fires btrix-change | ||
* @fires btrix-remove | ||
*/ | ||
@customElement("btrix-file-input") | ||
@localized() | ||
export class FileInput extends FormControl(TailwindElement) { | ||
/** | ||
* Form control name, if used as a form control | ||
*/ | ||
@property({ type: String }) | ||
name?: string; | ||
|
||
/** | ||
* Form control label, if used as a form control | ||
*/ | ||
@property({ type: String }) | ||
label?: string; | ||
|
||
/** | ||
* Specify which file types are allowed | ||
*/ | ||
@property({ type: String }) | ||
accept?: HTMLInputElement["accept"]; | ||
|
||
/** | ||
* Enable selecting more than one file | ||
*/ | ||
@property({ type: Boolean }) | ||
multiple?: HTMLInputElement["multiple"]; | ||
|
||
/** | ||
* Enable dragging files into drop zone | ||
*/ | ||
@property({ type: Boolean }) | ||
drop = false; | ||
|
||
@state() | ||
private files: File[] = []; | ||
|
||
@query("#dropzone") | ||
private readonly dropzone?: HTMLElement | null; | ||
|
||
@query("input[type='file']") | ||
private readonly input?: HTMLInputElement | null; | ||
|
||
formResetCallback() { | ||
this.files = []; | ||
|
||
if (this.input) { | ||
this.input.value = ""; | ||
} | ||
} | ||
|
||
protected willUpdate(changedProperties: PropertyValues): void { | ||
if (changedProperties.has("files")) { | ||
this.syncFormValue(); | ||
} | ||
} | ||
|
||
private syncFormValue() { | ||
const formControlName = this.name; | ||
|
||
if (!formControlName) return; | ||
|
||
// `ElementInternals["setFormValue"]` doesn't support `FileList` yet, | ||
// construct `FormData` instead | ||
const formData = new FormData(); | ||
|
||
this.files.forEach((file) => { | ||
formData.append(formControlName, file); | ||
}); | ||
|
||
this.setFormValue(formData); | ||
} | ||
|
||
render() { | ||
return html` | ||
${this.label | ||
? html`<label for="fileInput" class="form-label">${this.label}</label>` | ||
: nothing} | ||
${this.files.length ? this.renderFiles() : this.renderInput()} | ||
`; | ||
} | ||
|
||
private readonly renderInput = () => { | ||
return html` | ||
<div | ||
id="dropzone" | ||
class=${clsx( | ||
this.drop | ||
? tw`flex size-full cursor-pointer items-center justify-center rounded p-6 text-center outline-dashed outline-1 -outline-offset-1 outline-neutral-400 transition-all hover:bg-slate-50 hover:outline-primary-400` | ||
: tw`size-max`, | ||
)} | ||
@drop=${this.drop ? this.onDrop : undefined} | ||
@dragover=${this.drop ? this.onDragover : undefined} | ||
@dragenter=${this.drop | ||
? () => this.dropzone?.classList.add(droppingClass) | ||
: undefined} | ||
@dragleave=${this.drop | ||
? () => this.dropzone?.classList.remove(droppingClass) | ||
: undefined} | ||
@click=${() => this.input?.click()} | ||
role="button" | ||
dropzone="copy" | ||
aria-dropeffect="copy" | ||
> | ||
<input | ||
id="fileInput" | ||
class="sr-only" | ||
type="file" | ||
accept=${ifDefined(this.accept)} | ||
?multiple=${this.multiple} | ||
@change=${() => { | ||
const files = this.input?.files; | ||
|
||
if (files) { | ||
void this.handleChange(files); | ||
} | ||
}} | ||
/> | ||
<div class="relative z-10"> | ||
<slot></slot> | ||
</div> | ||
</div> | ||
`; | ||
}; | ||
|
||
private readonly renderFiles = () => { | ||
return html` | ||
<btrix-file-list | ||
@btrix-remove=${(e: BtrixFileRemoveEvent) => { | ||
this.files = without([e.detail.item])(this.files); | ||
}} | ||
> | ||
${repeat( | ||
this.files, | ||
(file) => file.name, | ||
(file) => html` | ||
<btrix-file-list-item .file=${file}></btrix-file-list-item> | ||
`, | ||
)} | ||
</btrix-file-list> | ||
`; | ||
}; | ||
|
||
private readonly onDrop = (e: DragEvent) => { | ||
e.preventDefault(); | ||
|
||
this.dropzone?.classList.remove(droppingClass); | ||
|
||
const files = e.dataTransfer?.files; | ||
|
||
if (files) { | ||
const list = new DataTransfer(); | ||
|
||
if (this.multiple) { | ||
[...files].forEach((file) => { | ||
if (this.valid(file)) { | ||
list.items.add(file); | ||
} | ||
}); | ||
} else { | ||
const file = files[0]; | ||
|
||
if (this.valid(file)) { | ||
list.items.add(file); | ||
} | ||
} | ||
|
||
if (list.items.length) { | ||
void this.handleChange(list.files); | ||
} else { | ||
console.debug("none valid:", files); | ||
} | ||
} else { | ||
console.debug("no files dropped"); | ||
} | ||
}; | ||
|
||
private readonly onDragover = (e: DragEvent) => { | ||
e.preventDefault(); | ||
|
||
if (e.dataTransfer) { | ||
this.dropzone?.classList.add(droppingClass); | ||
e.dataTransfer.dropEffect = "copy"; | ||
} | ||
}; | ||
|
||
/** | ||
* @TODO More complex validation based on `accept` | ||
*/ | ||
private valid(file: File) { | ||
if (!this.accept) return true; | ||
|
||
return this.accept.split(",").some((accept) => { | ||
if (accept.startsWith(".")) { | ||
return file.name.endsWith(accept.trim()); | ||
} | ||
|
||
return new RegExp(accept.trim().replace("*", ".*")).test(file.type); | ||
}); | ||
} | ||
|
||
private async handleChange(fileList: FileList) { | ||
this.files = [...fileList]; | ||
|
||
await this.updateComplete; | ||
|
||
this.dispatchEvent( | ||
new CustomEvent<BtrixFileChangeEvent["detail"]>("btrix-change", { | ||
detail: { value: this.files }, | ||
composed: true, | ||
bubbles: true, | ||
}), | ||
); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import type { BtrixChangeEvent } from "@/events/btrix-change"; | ||
import type { BtrixRemoveEvent } from "@/events/btrix-remove"; | ||
|
||
export type BtrixFileRemoveEvent = BtrixRemoveEvent<File>; | ||
export type BtrixFileChangeEvent = BtrixChangeEvent<File[]>; |
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { css, html } from "lit"; | ||
import { customElement, queryAssignedElements } from "lit/decorators.js"; | ||
|
||
import { TailwindElement } from "@/classes/TailwindElement"; | ||
|
||
@customElement("btrix-file-list") | ||
export class FileList extends TailwindElement { | ||
emma-sg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
static styles = [ | ||
css` | ||
::slotted(btrix-file-list-item) { | ||
--border: 1px solid var(--sl-panel-border-color); | ||
--item-border-top: var(--border); | ||
--item-border-left: var(--border); | ||
--item-border-right: var(--border); | ||
--item-border-bottom: var(--border); | ||
--item-box-shadow: var(--sl-shadow-x-small); | ||
--item-border-radius: var(--sl-border-radius-medium); | ||
display: block; | ||
} | ||
|
||
::slotted(btrix-file-list-item:not(:last-of-type)) { | ||
margin-bottom: var(--sl-spacing-x-small); | ||
} | ||
`, | ||
]; | ||
|
||
@queryAssignedElements({ selector: "btrix-file-list-item" }) | ||
listItems!: HTMLElement[]; | ||
|
||
render() { | ||
return html`<div class="list" role="list"> | ||
<slot @slotchange=${this.handleSlotchange}></slot> | ||
</div>`; | ||
} | ||
|
||
private handleSlotchange() { | ||
this.listItems.map((el) => { | ||
if (!el.attributes.getNamedItem("role")) { | ||
el.setAttribute("role", "listitem"); | ||
} | ||
}); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import "./file-list"; | ||
import "./file-list-item"; | ||
|
||
export type { BtrixFileRemoveEvent as FileRemoveEvent } from "./events"; |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.