-
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
92181c0
commit 80fbb15
Showing
9 changed files
with
154 additions
and
13 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
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,26 @@ | ||
import React from "react"; | ||
import { FileInput } from "./file-input"; | ||
|
||
export default { | ||
title: "File Input", | ||
argTypes: { | ||
error: { | ||
defaultValue: "", | ||
control: { | ||
type: "text", | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
export const Primary = ({ error }) => { | ||
const [selectedFile, setSelectedFile] = React.useState<File | null>(null); | ||
return ( | ||
<FileInput | ||
onChange={(file) => setSelectedFile(file)} | ||
placeholder="Upload your image" | ||
errors={[error]} | ||
value={selectedFile} | ||
/> | ||
); | ||
}; |
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,62 @@ | ||
import React from "react"; | ||
import { FileUploadIcon } from "../icons"; | ||
import { Spacer } from "../spacer"; | ||
import { Text } from "../text"; | ||
|
||
type Props = { | ||
placeholder?: string; | ||
accept?: string; | ||
name?: string; | ||
errors?: string[]; | ||
onChange?: (file: File | null) => void; | ||
value?: File | null; | ||
}; | ||
|
||
export const FileInput = React.forwardRef<HTMLInputElement, Props>( | ||
({ placeholder, accept, name, errors, onChange, value }: Props, ref) => { | ||
const allErrors = errors ?? []; | ||
const hasErrors = allErrors.length > 0; | ||
|
||
const inputChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
if (!e.target.files) { | ||
onChange?.(null); | ||
return; | ||
} | ||
|
||
onChange?.(e.target.files[0]); | ||
}; | ||
|
||
const visibleName = value?.name ?? placeholder; | ||
|
||
return ( | ||
<div> | ||
<label> | ||
<div className="border-b border-black pb-2 flex justify-between"> | ||
<Text color={value?.name ? "black" : "grey-250"} size={2}> | ||
{visibleName} | ||
</Text> | ||
<FileUploadIcon /> | ||
</div> | ||
|
||
{hasErrors && ( | ||
<> | ||
<Spacer size="thin" /> | ||
<Text as="p" size="label4" color="error" uppercase> | ||
{allErrors.join(", ")} | ||
</Text> | ||
</> | ||
)} | ||
|
||
<input | ||
ref={ref} | ||
type="file" | ||
className="hidden" | ||
accept={accept} | ||
name={name} | ||
onChange={inputChange} | ||
/> | ||
</label> | ||
</div> | ||
); | ||
}, | ||
); |
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 @@ | ||
export { FileInput } from "./file-input"; |
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,43 @@ | ||
import React from "react"; | ||
|
||
export const FileUploadIcon = (props) => ( | ||
<svg | ||
width="32" | ||
height="32" | ||
viewBox="0 0 32 32" | ||
fill="none" | ||
role="presentation" | ||
{...props} | ||
> | ||
<line | ||
x1="4" | ||
y1="29.3018" | ||
x2="27.6165" | ||
y2="29.3018" | ||
stroke="#0E1116" | ||
strokeWidth="2" | ||
/> | ||
<line | ||
x1="5" | ||
y1="30.3018" | ||
x2="5" | ||
y2="21.3018" | ||
stroke="#0E1116" | ||
strokeWidth="2" | ||
/> | ||
<line | ||
x1="26.6172" | ||
y1="30.3018" | ||
x2="26.6172" | ||
y2="21.305" | ||
stroke="#0E1116" | ||
strokeWidth="2" | ||
/> | ||
<path | ||
fillRule="evenodd" | ||
clipRule="evenodd" | ||
d="M14.7476 6.13585L10.0964 11.3685L8.60156 10.0398L15.0002 2.84129L15.7476 2.00046L16.4951 2.84129L22.8937 10.0398L21.3989 11.3685L16.7476 6.13585L16.7476 22.3018L14.7476 22.3018L14.7476 6.13585Z" | ||
fill="#0E1116" | ||
/> | ||
</svg> | ||
); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,4 +25,5 @@ export type Icon = | |
| "web" | ||
| "drink" | ||
| "forks" | ||
| "live-circle"; | ||
| "live-circle" | ||
| "file-upload"; |
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