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

make the remove icon of the file preview configurable and default to RemoveCircle #8756

Merged
merged 5 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/FileInput.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Files are accepted or rejected based on the `accept`, `multiple`, `minSize` and
| `multiple` | Optional | `boolean` | `false` | Whether the inputs can accept multiple files. |
| `options` | Optional | `Object` | `{}` | Additional options passed to react-dropzone's `useDropzone()` hook. |
| `placeholder` | Optional | `ReactNode` | - | Invite displayed in the drop zone |
| `removeIcon` | Optional | `ReactNode` | - | The clickable icon for removing images |
slax57 marked this conversation as resolved.
Show resolved Hide resolved
| `validateFile Removal` | Optional | `function` | - | Allows to cancel the removal of files |

`<FileInput>` also accepts the [common input props](./Inputs.md#common-input-props).
Expand Down Expand Up @@ -152,6 +153,10 @@ If that's not enough, you can pass a `placeholder` prop to overwrite it. The val
</FileInput>
```

## `removeIcon`

Optionally overwrite the [default icon](https://mui.com/material-ui/material-icons/?query=removeCir&selected=RemoveCircle) for removing files.
slax57 marked this conversation as resolved.
Show resolved Hide resolved

## `sx`: CSS API

The `<FileInput>` component accepts the usual `className` prop. You can also override many styles of the inner components thanks to the `sx` property (as most MUI components, see their [documentation about it](https://mui.com/customization/how-to-customize/#overriding-nested-component-styles)). This property accepts the following subclasses:
Expand Down
7 changes: 6 additions & 1 deletion docs/ImageInput.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ Files are accepted or rejected based on the `accept`, `multiple`, `minSize` and

| Prop | Required | Type | Default | Description |
|------------------------|----------|---------------------|------------|---------------------------------------------------------------------|
| `accept` | Optional | `string | string[]` | - | Accepted file type(s). When empty, all file types are accepted. |
| `accept` | Optional | `string | string[]` | - | Accepted file type(s). When empty, all file types are accepted. |
| `children` | Optional | `ReactNode` | - | Element used to preview file(s) |
| `minSize` | Optional | `number` | 0 | Minimum file size (in bytes), e.g. 5000 for 5KB |
| `maxSize` | Optional | `number` | `Infinity` | Maximum file size (in bytes), e.g. 5000000 for 5MB |
| `multiple` | Optional | `boolean` | `false` | Whether the inputs can accept multiple files. |
| `options` | Optional | `Object` | `{}` | Additional options passed to react-dropzone's `useDropzone()` hook. |
| `placeholder` | Optional | `ReactNode` | - | Invite displayed in the drop zone |
| `removeIcon` | Optional | `ReactNode` | - | The clickable icon for removing images |
slax57 marked this conversation as resolved.
Show resolved Hide resolved
| `validateFile Removal` | Optional | `function` | - | Allows to cancel the removal of files |

`<ImageInput>` also accepts the [common input props](./Inputs.md#common-input-props).
Expand Down Expand Up @@ -149,6 +150,10 @@ If that's not enough, you can pass a `placeholder` prop to overwrite it. The val
</ImageInput>
```

## `removeIcon`

Optionally overwrite the [default icon](https://mui.com/material-ui/material-icons/?query=removeCir&selected=RemoveCircle) for removing images.
slax57 marked this conversation as resolved.
Show resolved Hide resolved

## `sx`: CSS API

The `<ImageInput>` component accepts the usual `className` prop. You can also override many styles of the inner components thanks to the `sx` property (as most MUI components, see their [documentation about it](https://mui.com/customization/how-to-customize/#overriding-nested-component-styles)). This property accepts the following subclasses:
Expand Down
5 changes: 5 additions & 0 deletions packages/ra-ui-materialui/src/input/FileInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { FileInputPreview } from './FileInputPreview';
import { sanitizeInputRestProps } from './sanitizeInputRestProps';
import { InputHelperText } from './InputHelperText';
import { SxProps } from '@mui/system';
import { SvgIconProps } from '@mui/material';

export const FileInput = (props: FileInputProps) => {
const {
Expand All @@ -41,6 +42,7 @@ export const FileInput = (props: FileInputProps) => {
onRemove: onRemoveProp,
parse,
placeholder,
removeIcon,
resource,
source,
validate,
Expand Down Expand Up @@ -192,6 +194,7 @@ export const FileInput = (props: FileInputProps) => {
file={file}
onRemove={onRemove(file)}
className={FileInputClasses.removeButton}
removeIcon={removeIcon}
>
<RecordContextProvider value={file}>
{childrenElement}
Expand Down Expand Up @@ -223,6 +226,7 @@ FileInput.propTypes = {
multiple: PropTypes.bool,
validateFileRemoval: PropTypes.func,
options: PropTypes.object,
removeIcon: PropTypes.element,
resource: PropTypes.string,
source: PropTypes.string,
placeholder: PropTypes.node,
Expand Down Expand Up @@ -264,6 +268,7 @@ export type FileInputProps = CommonInputProps & {
options?: DropzoneOptions;
onRemove?: Function;
placeholder?: ReactNode;
removeIcon?: React.ReactElement<SvgIconProps>;
inputProps?: any;
validateFileRemoval?(file): boolean | Promise<boolean>;
sx?: SxProps;
Expand Down
16 changes: 13 additions & 3 deletions packages/ra-ui-materialui/src/input/FileInputPreview.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import * as React from 'react';
import { ReactNode, useEffect } from 'react';
import { styled } from '@mui/material/styles';
import { useEffect, ReactNode } from 'react';
import PropTypes from 'prop-types';
import RemoveCircle from '@mui/icons-material/RemoveCircle';
import IconButton from '@mui/material/IconButton';
import { useTranslate } from 'ra-core';
import { SvgIconProps } from '@mui/material';

export const FileInputPreview = (props: FileInputPreviewProps) => {
const { children, className, onRemove, file, ...rest } = props;
const {
children,
className,
onRemove,
file,
removeIcon = RemoveCircle,
...rest
} = props;

const translate = useTranslate();

Expand All @@ -30,7 +38,7 @@ export const FileInputPreview = (props: FileInputPreviewProps) => {
title={translate('ra.action.delete')}
size="small"
>
<RemoveCircle className={FileInputPreviewClasses.removeIcon} />
{removeIcon}
slax57 marked this conversation as resolved.
Show resolved Hide resolved
</IconButton>
{children}
</Root>
Expand All @@ -42,6 +50,7 @@ FileInputPreview.propTypes = {
className: PropTypes.string,
file: PropTypes.object,
onRemove: PropTypes.func.isRequired,
removeIcon: PropTypes.element,
slax57 marked this conversation as resolved.
Show resolved Hide resolved
};

FileInputPreview.defaultProps = {
Expand Down Expand Up @@ -71,4 +80,5 @@ export interface FileInputPreviewProps {
className?: string;
onRemove: () => void;
file: any;
removeIcon?: React.ReactElement<SvgIconProps>;
}