-
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
1 parent
7d3716a
commit 7eed696
Showing
7 changed files
with
135 additions
and
127 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,25 @@ | ||
import { action, Story } from "@ladle/react"; | ||
import { Story, action } from "@ladle/react"; | ||
import { HiddenFileInput } from "./HiddenFileInput"; | ||
|
||
const file = new File(["Hello, world!"], "hello-world.txt", { | ||
type: "text/plain", | ||
type: "text/plain", | ||
}); | ||
|
||
export const Default: Story = () => ( | ||
<form onSubmit={(e) => { | ||
e.preventDefault(); | ||
const form = e.target as HTMLFormElement; | ||
const formData = new FormData(form); | ||
action("submit")(formData) | ||
}}> | ||
<HiddenFileInput name="myfile" file={file} /> | ||
<button className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded" type="submit">Submit</button> | ||
</form> | ||
<form | ||
onSubmit={(e) => { | ||
e.preventDefault(); | ||
const form = e.target as HTMLFormElement; | ||
const formData = new FormData(form); | ||
action("submit")(formData); | ||
}} | ||
> | ||
<HiddenFileInput name="myfile" file={file} /> | ||
<button | ||
className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded" | ||
type="submit" | ||
> | ||
Submit | ||
</button> | ||
</form> | ||
); |
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,15 +1,15 @@ | ||
import { useEffect, useRef } from "react"; | ||
|
||
export function HiddenFileInput({ name, file }: { name: string; file: File }) { | ||
const ref = useRef<HTMLInputElement>(null); | ||
const ref = useRef<HTMLInputElement>(null); | ||
|
||
useEffect(() => { | ||
if (ref.current) { | ||
const dataTransfer = new DataTransfer(); | ||
dataTransfer.items.add(file); | ||
ref.current.files = dataTransfer.files; | ||
} | ||
}, [file]); | ||
useEffect(() => { | ||
if (ref.current) { | ||
const dataTransfer = new DataTransfer(); | ||
dataTransfer.items.add(file); | ||
ref.current.files = dataTransfer.files; | ||
} | ||
}, [file]); | ||
|
||
return <input ref={ref} type="file" name={name} className="hidden" />; | ||
return <input ref={ref} type="file" name={name} className="hidden" />; | ||
} |
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
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,33 +1,33 @@ | ||
import { useState, useEffect, ReactNode } from "react"; | ||
import { ReactNode, useEffect, useState } from "react"; | ||
import { cn } from "./cn"; | ||
|
||
// TODO allow other a tag attributes like title or target to be passed in | ||
|
||
export function LinkToFile({ | ||
file, | ||
children, | ||
className = undefined, | ||
file, | ||
children, | ||
className = undefined, | ||
}: { | ||
file: File; | ||
children: ReactNode; | ||
className?: string; | ||
file: File; | ||
children: ReactNode; | ||
className?: string; | ||
}) { | ||
const [url, setUrl] = useState("#"); | ||
const [url, setUrl] = useState("#"); | ||
|
||
useEffect(() => { | ||
const url = URL.createObjectURL(file); | ||
setUrl(url); | ||
return () => URL.revokeObjectURL(url); | ||
}, [file]); | ||
useEffect(() => { | ||
const url = URL.createObjectURL(file); | ||
setUrl(url); | ||
return () => URL.revokeObjectURL(url); | ||
}, [file]); | ||
|
||
return ( | ||
<a | ||
className={cn("underline", className)} | ||
download={file.name} | ||
href={url} | ||
type={file.type} | ||
> | ||
{children} | ||
</a> | ||
); | ||
return ( | ||
<a | ||
className={cn("underline", className)} | ||
download={file.name} | ||
href={url} | ||
type={file.type} | ||
> | ||
{children} | ||
</a> | ||
); | ||
} |
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
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
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