Skip to content

Commit

Permalink
docs: add file upload live previews
Browse files Browse the repository at this point in the history
  • Loading branch information
@casey_baggz_omni committed Sep 11, 2024
1 parent 8577cea commit b017b4d
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 21 deletions.
53 changes: 53 additions & 0 deletions docs/app/react/file-uploader/components/live-preview.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use client'

import CodeBuilder from '@/app/components/code-builder/code-builder'
import { builder } from '@/app/components/code-builder/helpers'
import { useCodeBuilder } from '@/app/context/code-builder'
import { FileUploader } from '@cerberus-design/react'

const api = {
heading: builder.Text('heading', 'Upload your files'),
name: builder.Text('name', 'add_uuid'),
accept: builder.Text('accept', '.txt'),
}

export function LivePlayground() {
return (
<CodeBuilder api={api}>
<FileUploadPreview />
</CodeBuilder>
)
}

export function LivePlaygroundWithCode() {
return (
<CodeBuilder
api={api}
code={`import { FileUploader, type FileUploaderProps } from '@cerberus-design/react'
import { useCallback, type MouseEvent } from 'react'
export function MyFileUploader(props: FileUploaderProps) {
return (
<FileUploader
accept={{accept}}
heading={{heading}}
name={{name}}
/>
)
}`}
>
<FileUploadPreview />
</CodeBuilder>
)
}

export function FileUploadPreview() {
const { selectedProps } = useCodeBuilder()
return (
<FileUploader
accept={selectedProps.accept as string}
heading={selectedProps.heading as string}
name={selectedProps.file as string}
/>
)
}
127 changes: 127 additions & 0 deletions docs/app/react/file-uploader/components/status-live-preview.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
'use client'

import CodeBuilder from '@/app/components/code-builder/code-builder'
import { builder } from '@/app/components/code-builder/helpers'
import { useCodeBuilder } from '@/app/context/code-builder'
import { FileStatus, processStatus } from '@cerberus-design/react'

const file = 'file.txt'

const api = {
status: builder.Enum('status', [
processStatus.TODO,
processStatus.PROCESSING,
processStatus.DONE,
processStatus.ERROR,
]),
}

export function FileStatusLivePlayground() {
return (
<CodeBuilder api={api}>
<FileStatusPreview />
</CodeBuilder>
)
}

export function FileStatusLivePlaygroundWithCode() {
return (
<CodeBuilder
api={api}
code={`'use client'
import {
FileStatus,
processStatus,
type FileStatusProps,
} from '@cerberus-design/react'
import { useCallback, type MouseEvent } from 'react'
export function MyFileStatus(props: FileStatusProps) {
const handleClick = useCallback((
status: FileStatusActions,
e: MouseEvent<HTMLButtonElement>
) => {
switch (status) {
case processStatus.TODO:
console.log('TODO', e, 'call props.onClick')
break
case processStatus.PROCESSING:
console.log('PROCESSING', e, 'call props.onClick')
break
case processStatus.DONE:
console.log('DONE', e, 'call props.onClick')
break
case processStatus.ERROR:
console.log('ERROR', e, 'call props.onClick')
break
default:
throw new Error('Unknown status passed into handleClick')
}
}, [])
return (
<FileStatus
file={props.file}
now={props.now}
onClick={handleClick}
status={{status}}
/>
)
}`}
>
<FileStatusPreview />
</CodeBuilder>
)
}

export function FileStatusPreview() {
const { selectedProps } = useCodeBuilder()
switch (selectedProps.status) {
case processStatus.TODO:
return (
<FileStatus
file={file}
now={0}
onClick={() => {}}
status={processStatus.TODO}
/>
)
case processStatus.PROCESSING:
return (
<FileStatus
file={file}
now={50}
onClick={() => {}}
status={processStatus.PROCESSING}
/>
)
case processStatus.DONE:
return (
<FileStatus
file={file}
now={100}
onClick={() => {}}
status={processStatus.DONE}
/>
)
case processStatus.ERROR:
return (
<FileStatus
file={file}
now={0}
onClick={() => {}}
status={processStatus.ERROR}
/>
)
default:
return (
<FileStatus
file={file}
now={100}
onClick={() => {}}
status={processStatus.TODO}
/>
)
}
}
33 changes: 15 additions & 18 deletions docs/app/react/file-uploader/dev.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,27 @@ recipe: 'label.ts'

import CodePreview from '@/app/components/CodePreview'
import {
BasicFileUploader
} from '@/app/react/file-uploader/components/file-uploader-preview'
LivePlaygroundWithCode
} from '@/app/react/file-uploader/components/live-preview'
import {
FileStatusLivePlaygroundWithCode
} from '@/app/react/file-uploader/components/status-live-preview'

```ts
import { FileUploader } from '@cerberus-design/react'
import { FileUploader, FileStatus, processStatus } from '@cerberus-design/react'
```

## Usage
## Uploading Files

<CodePreview preview={<BasicFileUploader />}>
```tsx title="file-uploader.tsx" {5,6}
import { FileUploader } from '@cerberus-design/react'
To allow users to upload files, use the `FileUploader` component.

function BasicFileUploader() {
return (
<FileUploader
accept=".csv,.docx"
heading="Upload Files"
name="basic-example"
/>
)
}
```
</CodePreview>
<LivePlaygroundWithCode />

## File Status

If you choose to process the file, you can use the `FileStatus` component to display the status of the job.

<FileStatusLivePlaygroundWithCode />

## Customizing

Expand Down
8 changes: 5 additions & 3 deletions docs/app/react/file-uploader/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ a11y: 'forms'
import CodePreview from '@/app/components/CodePreview'
import OverviewList from '@/app/components/OverviewList'
import {
BasicFileUploader
} from '@/app/react/file-uploader/components/file-uploader-preview'
LivePlayground
} from '@/app/react/file-uploader/components/live-preview'
import {
UploadingCardsPreview
} from '@/app/react/file-uploader/components/file-status-preview'
Expand All @@ -21,10 +21,12 @@ import {

## Example

<CodePreview preview={<BasicFileUploader />} />
<LivePlayground />

## Processing Uploads

Cerberus provides some visual feedback when you would like to show the status of a file upload.

<CodePreview preview={<UploadingCardsPreview />} />


Expand Down
2 changes: 2 additions & 0 deletions packages/panda-preset/src/recipes/slots/fileUploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export const fileUploader: Partial<SlotRecipeConfig> = defineSlotRecipe({
},
label: {
// combine with vstack
color: 'page.text.200',
gap: '1',
justify: 'center',
position: 'relative',
Expand All @@ -59,6 +60,7 @@ export const fileUploader: Partial<SlotRecipeConfig> = defineSlotRecipe({
},
icon: modalIconBase,
heading: {
color: 'page.text.initial',
pt: '2',
textStyle: 'h6',
},
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/components/FileStatus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export function FileStatus(props: FileStatusProps) {
>
<small
className={css({
color: 'page.text.initial',
textStyle: 'label-sm',
})}
>
Expand Down

0 comments on commit b017b4d

Please sign in to comment.