Skip to content

Commit

Permalink
feat: add readonly mode for FilesList
Browse files Browse the repository at this point in the history
  • Loading branch information
akhmyrov committed Nov 21, 2022
1 parent 82c3ec0 commit 6141dd4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bestdoctor/ke-beta",
"version": "13.2.7",
"version": "13.2.8",
"description": "BestDoctor back-office UI constructor",
"author": "pro100filipp",
"main": "dist/index.js",
Expand Down
58 changes: 38 additions & 20 deletions src/FormControls/FilesUpload/FilesList.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Это легаси
/* eslint-disable react/jsx-props-no-spreading */
import React, { ReactElement } from 'react'
import React, { ReactElement, useCallback } from 'react'
import { List, ListIcon, ListItem, IconButton, Link, LinkProps } from '@chakra-ui/react'
import { Paperclip, X } from 'react-feather'

Expand All @@ -13,16 +13,39 @@ const listItemCss = css`
}
`

type FilesListProps = {
value: readonly FileDescriptor[]
listItemIcon?: React.ComponentType
linkProps?: Omit<LinkProps, 'href'>
} & (
| {
isReadOnly: true
onChange: undefined
}
| {
isReadOnly?: false
onChange: (value: FileDescriptor[]) => void
}
)

export function FilesList({
value,
onChange,
listItemIcon = Paperclip,
linkProps,
isReadOnly = false,
}: FilesListProps): ReactElement<FilesListProps> {
const deleteFile = (file: FileDescriptor): void => {
const restFiles = value.filter((f) => f.uuid !== file.uuid)
onChange(restFiles)
}
const deleteFile = useCallback(
(file: FileDescriptor): void => {
if (!onChange) {
return
}

const restFiles = value.filter((f) => f.uuid !== file.uuid)
onChange(restFiles)
},
[onChange, value]
)

return (
<List>
Expand All @@ -36,23 +59,18 @@ export function FilesList({
) : (
file.name
)}
<IconButton
aria-label="Удалить"
variant="unstyled"
size="xs"
icon={<X color="red" size={16} />}
ml={2}
onClick={() => deleteFile(file)}
/>
{!isReadOnly && (
<IconButton
aria-label="Удалить"
variant="unstyled"
size="xs"
icon={<X color="red" size={16} />}
ml={2}
onClick={() => deleteFile(file)}
/>
)}
</ListItem>
))}
</List>
)
}

interface FilesListProps {
value: readonly FileDescriptor[]
onChange: (value: FileDescriptor[]) => void
listItemIcon?: React.ComponentType
linkProps?: Omit<LinkProps, 'href'>
}

0 comments on commit 6141dd4

Please sign in to comment.