Skip to content

Commit

Permalink
Merge pull request #41 from CarlaPaiva/fea/add-cascader
Browse files Browse the repository at this point in the history
feat: add cascader instead of select
  • Loading branch information
CarlaPaiva committed Feb 3, 2024
2 parents e0f66fc + a30f681 commit 3154d25
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Alert, Button, Select, UploadFile } from 'antd'
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Alert, Button, Cascader, UploadFile } from 'antd'
import DraggerUpload from '../../../dragger-upload'
import './upload-step.css'
import { memo, useCallback, useMemo, useState } from 'react'
Expand All @@ -17,6 +18,12 @@ type ErrorMessage = {
message: string
}

type Option = {
value: string
label: string
children?: Option[]
}

const hideErrorMessage: ErrorMessage = {
isVisible: false,
message: '',
Expand Down Expand Up @@ -65,9 +72,16 @@ const UploadStep = memo(function UploadStepComponent(
}, [props.allowedUploadingFormats])

const onSelectedFormatChange = useCallback(
(value: string) => {
setFormat(value)
props.updateSelectedExtension(value)
(value: (string | number)[]) => {
const result = value[1] as string

if (!result) {
console.warn('Could not read selected format.')
return
}

setFormat(result)
props.updateSelectedExtension(result)
},
[props],
)
Expand All @@ -87,22 +101,39 @@ const UploadStep = memo(function UploadStepComponent(
}, [files.length, format, props])

const options = useMemo(() => {
return props.extensionOptions.map((item) => {
return { value: item.extension, label: item.name }
const opts: Option[] = []

props.extensionOptions.forEach((item) => {
if (opts.some((x) => x.label === item.category)) {
return
}

const children = props.extensionOptions.filter(
(value) => value.category === item.category,
)

opts.push({
label: item.category,
value: item.category,
children: children.map((cItem) => {
return { value: cItem.extension, label: cItem.name, children: [] }
}),
})
})

return opts
}, [props.extensionOptions])

return (
<div className="container">
<DraggerUpload accept={acceptedTypes} onUploadDone={onUploadDone} />
<Select
value={format}
placeholder="Convert all to..."
size="large"
<Cascader
className="container__items"
onChange={onSelectedFormatChange}
size="large"
placeholder="Convert all to..."
options={options}
></Select>
onChange={onSelectedFormatChange}
/>
<Button
size="large"
type="primary"
Expand Down
5 changes: 5 additions & 0 deletions src/components/video-converter/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,27 @@ const optionsToConvertTo: Format[] = [
{
name: 'GIF',
extension: '.gif',
category: 'Image',
},
{
name: 'MP4',
extension: '.mp4',
category: 'Video',
},
{
name: 'AVI',
extension: '.avi',
category: 'Video',
},
{
name: 'WMV',
extension: '.wmv',
category: 'Video',
},
{
name: 'MP3',
extension: '.mp3',
category: 'Audio',
},
]

Expand Down
5 changes: 5 additions & 0 deletions src/model/format.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
/** Category of a Format */
type FormatCategory = 'Image' | 'Audio' | 'Video' | 'File'

/** Defines a format */
export type Format = {
/** Name o the format */
name: string
/** Extension with a dot */
extension: string
/** Category of the format */
category: FormatCategory
}

0 comments on commit 3154d25

Please sign in to comment.