-
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.
ADCIO-3115) feat: implement file input (#37)
* feat: implement file input * refactor: refactor file handler * feat: modify to accept more mime types * docs: modify storybook * feat: add file name to input * feat: export file input * refactor: rollback input container style --------- Co-authored-by: dev-redo <mis05041@naver.com>
- Loading branch information
Showing
4 changed files
with
224 additions
and
14 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 |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { ChangeEvent, useId } from 'react'; | ||
|
||
import styled from '@emotion/styled'; | ||
|
||
import Icon from '../Icon'; | ||
import { B3 } from '../Text'; | ||
import { | ||
BaseInputType, | ||
type InputBaseProps, | ||
InputContainer, | ||
baseInputStyles, | ||
} from './InputContainer'; | ||
|
||
export type FileInputProps = Omit<InputBaseProps, 'children' | 'value'> & { | ||
uploadType: 'audio' | 'image' | 'video' | 'csv' | 'txt' | 'pdf' | 'default'; | ||
file: File | null; | ||
onUpload: (file: File) => void; | ||
}; | ||
|
||
const getMimeType = (uploadType?: FileInputProps['uploadType']) => { | ||
switch (uploadType) { | ||
case 'audio': | ||
case 'image': | ||
case 'video': | ||
return `${uploadType}/*`; | ||
case 'csv': | ||
return 'text/csv'; | ||
case 'txt': | ||
return 'text/plain'; | ||
case 'pdf': | ||
return '.pdf'; | ||
default: | ||
return '*/*'; | ||
} | ||
}; | ||
|
||
export function FileInput({ | ||
label, | ||
placeholder, | ||
description, | ||
name, | ||
file, | ||
error, | ||
disabled, | ||
onUpload, | ||
defaultValue, | ||
required = false, | ||
width, | ||
tooltip, | ||
onClick, | ||
height, | ||
uploadType = 'default', | ||
...props | ||
}: FileInputProps) { | ||
const id = useId(); | ||
return ( | ||
<InputContainer | ||
cursorPointer | ||
onClick={onClick} | ||
label={label} | ||
description={description} | ||
required={required} | ||
error={error} | ||
width={width} | ||
tooltip={tooltip} | ||
{...props} | ||
leftSection={<Icon.Upload />} | ||
> | ||
<> | ||
<FileUploadInput | ||
onChange={(event: ChangeEvent<HTMLInputElement>) => { | ||
if (event.target.files) { | ||
onUpload(event.target.files[0]); | ||
} | ||
}} | ||
type="file" | ||
accept={getMimeType(uploadType)} | ||
id={`file-input-${id}`} | ||
/> | ||
<UploadButton | ||
isLeftSection | ||
error={error} | ||
height={height} | ||
cursorPointer | ||
htmlFor={`file-input-${id}`} | ||
> | ||
{file ? <B3>{file.name}</B3> : <B3 c="grey-50">{placeholder}</B3>} | ||
</UploadButton> | ||
</> | ||
</InputContainer> | ||
); | ||
} | ||
|
||
const FileUploadInput = styled.input` | ||
display: none; | ||
`; | ||
|
||
export const UploadButton = styled.label<BaseInputType>` | ||
${props => baseInputStyles(props)} | ||
display: flex; | ||
align-items: center; | ||
`; |
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