-
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
bd2be94
commit fcea336
Showing
4 changed files
with
84 additions
and
8 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,7 +1,28 @@ | ||
import { ComponentProps } from "react"; | ||
"use client"; | ||
|
||
import { ChangeEvent, ComponentProps } from "react"; | ||
import { useFileInput } from "./Root"; | ||
|
||
export type ControlProps = ComponentProps<"input">; | ||
|
||
export function Control(props: ControlProps) { | ||
return <input type="file" className="sr-only" id="photo" {...props} />; | ||
const { id, onFileSelected } = useFileInput(); | ||
|
||
function handleFilesSelected(event: ChangeEvent<HTMLInputElement>) { | ||
if (!event.target.files?.length) return; | ||
|
||
const files = Array.from(event.target.files); | ||
|
||
onFileSelected(files); | ||
} | ||
|
||
return ( | ||
<input | ||
type="file" | ||
className="sr-only" | ||
onChange={handleFilesSelected} | ||
id={id} | ||
{...props} | ||
/> | ||
); | ||
} |
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,9 +1,34 @@ | ||
"use client"; | ||
|
||
import { User } from "lucide-react"; | ||
import { useFileInput } from "./Root"; | ||
import { useMemo } from "react"; | ||
|
||
export function ImagePreview() { | ||
const { files } = useFileInput(); | ||
|
||
const previewURL = useMemo(() => { | ||
if (files.length === 0) { | ||
return null; | ||
} | ||
|
||
return URL.createObjectURL(files[0]); | ||
}, [files]); | ||
|
||
if (previewURL === null) { | ||
return ( | ||
<div className="flex h-16 w-16 items-center justify-center rounded-full bg-violet-50"> | ||
<User className="h-8 w-8 text-violet-500" /> | ||
</div> | ||
); | ||
} | ||
|
||
return ( | ||
<div className="flex h-16 w-16 items-center justify-center rounded-full bg-violet-50"> | ||
<User className="h-8 w-8 text-violet-500" /> | ||
</div> | ||
// eslint-disable-next-line @next/next/no-img-element | ||
<img | ||
src={previewURL} | ||
alt="" | ||
className="h-16 w-16 rounded-full object-cover" | ||
/> | ||
); | ||
} |
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,7 +1,32 @@ | ||
import { ComponentProps } from "react"; | ||
"use client"; | ||
|
||
import { | ||
ComponentProps, | ||
createContext, | ||
useContext, | ||
useId, | ||
useState, | ||
} from "react"; | ||
|
||
export type RootProps = ComponentProps<"div">; | ||
|
||
type FileInputContextType = { | ||
id: string; | ||
files: File[]; | ||
onFileSelected: (files: File[]) => void; | ||
}; | ||
|
||
const FileInputContext = createContext({} as FileInputContextType); | ||
|
||
export function Root(props: RootProps) { | ||
return <div {...props} />; | ||
const id = useId(); | ||
const [files, setFiles] = useState<File[]>([]); | ||
|
||
return ( | ||
<FileInputContext.Provider value={{ id, files, onFileSelected: setFiles }}> | ||
<div {...props} />; | ||
</FileInputContext.Provider> | ||
); | ||
} | ||
|
||
export const useFileInput = () => useContext(FileInputContext); |
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