Skip to content
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

Add upload component #316

Merged
merged 16 commits into from
Oct 9, 2024
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@
"typescript": "^5.4.3",
"vite": "^4.4.3",
"vitest": "^0.33.0",
"zod": "^3.22.4"
"zod": "^3.22.4",
"axios-mock-adapter": "^1.22.0"
},
"peerDependencies": {
"@aboutbits/react-pagination": "^3.0.3",
Expand Down
52 changes: 52 additions & 0 deletions src/components/button/ResponsiveButtonIcon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import classNames from 'classnames'
import { ComponentType, ReactNode } from 'react'
import { useTheme } from '../../framework'
import { IconProps } from '../types'
import { Button, ButtonProps } from './Button'
import { ButtonIcon, ButtonIconProps } from './ButtonIcon'

export type ResponsiveButtonIconProps = Omit<
ButtonProps & ButtonIconProps,
'ref' | 'children' | 'icon' | 'iconStart' | 'iconEnd' | 'label'
> & {
label: ReactNode
mollpo marked this conversation as resolved.
Show resolved Hide resolved
} & (
| {
icon: ComponentType<IconProps>
iconEnd?: never
}
| {
icon?: never
iconEnd: ComponentType<IconProps>
}
)

export function ResponsiveButtonIcon({
mollpo marked this conversation as resolved.
Show resolved Hide resolved
label,
className,
icon,
iconEnd,
...props
}: ResponsiveButtonIconProps) {
const { button } = useTheme()
return (
<>
<Button
{...props}
iconStart={icon}
iconEnd={iconEnd}
className={classNames(button.buttonIconResponsive.button, className)}
>
{label}
</Button>
<ButtonIcon
{...props}
icon={icon ?? iconEnd}
className={classNames(
button.buttonIconResponsive.buttonIcon,
className,
)}
/>
</>
)
}
4 changes: 4 additions & 0 deletions src/components/button/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ export default {
},
},
},
buttonIconResponsive: {
button: 'hidden md:flex',
mollpo marked this conversation as resolved.
Show resolved Hide resolved
buttonIcon: 'md:hidden',
},
modeVariantTone: {
[Mode.Light]: {
[ButtonVariant.Solid]: {
Expand Down
44 changes: 44 additions & 0 deletions src/components/files/DeleteFileAction.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import IconDelete from '@aboutbits/react-material-icons/dist/IconDelete'
import { useState } from 'react'
import { useInternationalization } from '../../framework'
import { ButtonVariant } from '../button'
import { ResponsiveButtonIcon } from '../button/ResponsiveButtonIcon'
import { IconSpinner } from '../loading/IconSpinner'
import { Tone } from '../types'
import { FileUploadObject } from './FileUploadState'

type DeleteFileActionProps<TRemoteFile> = {
fileUploadObject: FileUploadObject<TRemoteFile>
onDelete: (
fileUploadObject: FileUploadObject<TRemoteFile>,
) => void | Promise<void>
}

export function DeleteFileAction<TRemoteFile>({
fileUploadObject,
onDelete,
}: DeleteFileActionProps<TRemoteFile>) {
const [isDeleting, setIsDeleting] = useState(false)
const { messages } = useInternationalization()
return (
<>
mollpo marked this conversation as resolved.
Show resolved Hide resolved
<ResponsiveButtonIcon
variant={ButtonVariant.Transparent}
tone={Tone.Neutral}
disabled={isDeleting}
onClick={() => {
setIsDeleting(true)
Promise.resolve(onDelete(fileUploadObject))
.then(() => {
setIsDeleting(false)
})
.catch(() => {
setIsDeleting(false)
})
}}
icon={isDeleting ? IconSpinner : IconDelete}
label={messages['files.action.delete']}
/>
</>
)
}
23 changes: 23 additions & 0 deletions src/components/files/DownloadFileAction.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import IconDownload from '@aboutbits/react-material-icons/dist/IconDownload'
import { useInternationalization } from '../../framework'
import { ButtonVariant } from '../button'
import {
ResponsiveButtonIcon,
ResponsiveButtonIconProps,
} from '../button/ResponsiveButtonIcon'
import { Tone } from '../types'

export function DownloadFileAction({
onClick,
}: Pick<ResponsiveButtonIconProps, 'onClick'>) {
const { messages } = useInternationalization()
return (
<ResponsiveButtonIcon
variant={ButtonVariant.Transparent}
tone={Tone.Neutral}
icon={IconDownload}
onClick={onClick}
mollpo marked this conversation as resolved.
Show resolved Hide resolved
label={messages['files.action.download']}
/>
)
}
44 changes: 44 additions & 0 deletions src/components/files/FileDropZone.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {
Controls,
Description,
Primary,
Subheading,
Title,
} from '@storybook/blocks'
import { Meta, StoryObj } from '@storybook/react'
import { FileDropZone } from './FileDropZone'

const meta = {
component: FileDropZone,
argTypes: {
fileTypes: {
options: ['None', 'pdf', 'jpg, png, gif'],
control: {
type: 'select',
},
mapping: {
None: undefined,
pdf: ['pdf'],
'jpg, png, gif': ['jpg', 'png', 'gif'],
},
},
},
parameters: {
docs: {
page: () => (
<>
<Title />
<Description />
<Primary />
<Subheading>Props</Subheading>
<Controls />
</>
),
},
},
} satisfies Meta<typeof FileDropZone>

export default meta
type Story = StoryObj<typeof FileDropZone>

export const Default: Story = {}
115 changes: 115 additions & 0 deletions src/components/files/FileDropZone.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import IconUploadFile from '@aboutbits/react-material-icons/dist/IconUploadFile'
import classNames from 'classnames'
import { ChangeEventHandler, useRef } from 'react'
import { useInternationalization, useTheme } from '../../framework'
import { ClassNameProps } from '../types'
import { useFileDropZone } from './useFileDropZone'
import { useHumanReadableFileSize } from './utils'

export type FileDropZoneProps = {
fileTypes?: string[]
disabled?: boolean
maxFileSize?: number
className?: string
} & (
| { multipleFiles?: false; onSelect?: (file: File) => void }
| { multipleFiles: true; onSelect?: (files: File[]) => void }
) &
ClassNameProps

export function FileDropZone({
fileTypes,
multipleFiles,
disabled = false,
onSelect,
maxFileSize,
className,
}: FileDropZoneProps) {
const inputRef = useRef<HTMLInputElement>(null)

const handleFilesChange: ChangeEventHandler<HTMLInputElement> = ({
target,
}) => {
if (!inputRef.current) {
throw new Error('FileUpload: No input element specified!')
}

const files = Array.from(target.files ?? [])

if (multipleFiles) {
onSelect?.(files)
} else if (files[0]) {
onSelect?.(files[0])
}

target.value = ''
}

const { files } = useTheme()

const { messages } = useInternationalization()

const formatFileSize = useHumanReadableFileSize()

const { dropZoneRef, isFileHovering } = useFileDropZone<HTMLButtonElement>({
disabled,
inputRef,
})

return (
<div className={files.fileDropzone.container.base}>
<input
type="file"
ref={inputRef}
multiple={multipleFiles}
accept={fileTypes?.map((fileType) => `.${fileType}`).join(',')}
onChange={handleFilesChange}
disabled={disabled}
hidden
/>
<button
ref={dropZoneRef}
type="button"
onClick={() => inputRef.current?.click()}
className={classNames(
files.fileDropzone.uploadButton.base,
isFileHovering && files.fileDropzone.uploadButton.fileHovering,
className,
)}
>
<div
className={classNames(
files.icon.container.base,
files.icon.container.default,
)}
>
<IconUploadFile
className={classNames(files.icon.size, files.icon.default)}
/>
</div>
<div className={files.text.base}>
<div>
<span className={files.text.underline}>
{messages['files.dropzone.description.part1.text']}
</span>
{messages['files.dropzone.description.part2.text']}
</div>
<div className={files.text.info}>
{fileTypes && fileTypes.length > 0 && (
<>
{messages['files.dropzone.allowedFileTypes.text']}{' '}
{fileTypes.join(', ')}{' '}
</>
)}
{maxFileSize && (
<>
({messages['files.dropzone.maxFileSize.text']}{' '}
{formatFileSize(maxFileSize)})
</>
)}
</div>
</div>
</button>
</div>
)
}
9 changes: 9 additions & 0 deletions src/components/files/FileList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { PropsWithChildren } from 'react'
import { useTheme } from '../../framework'

export type FileListProps = PropsWithChildren

export function FileList({ children }: FileListProps) {
const { files } = useTheme()
return <div className={files.fileList.container}>{children}</div>
}
Loading