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

ImageInput clones its child #7877

Closed
fzaninotto opened this issue Jun 22, 2022 · 1 comment · Fixed by #8052
Closed

ImageInput clones its child #7877

fzaninotto opened this issue Jun 22, 2022 · 1 comment · Fixed by #8052
Assignees
Labels

Comments

@fzaninotto
Copy link
Member

What you were expecting:

Using ImageInput with a custom preview component works:

const PreviewField = () => <ImageField source="src" title="title" />;

<ImageInput source="pictures" label="Related pictures" accept="image/*">
    <PreviewField />
</ImageInput>

What happened instead:

The preview doesn't work. To make it work, one has to pass the props explicitly in the preview component:

const PreviewField = (props) => <ImageField source="src" title="title" {...props} />;

This may be natural in 3.0, but in 4.0 this is unexpected.

Related code:

The culprit is <FileInput>, which still clones its child instead of wrapping it in a RecordContext:

{children && (
<div className="previews">
{files.map((file, index) => (
<FileInputPreview
key={index}
file={file}
onRemove={onRemove(file)}
className={FileInputClasses.removeButton}
>
{cloneElement(childrenElement as ReactElement, {
record: file,
className: FileInputClasses.preview,
})}
</FileInputPreview>
))}
</div>
)}

Environment

  • React-admin version: 4
  • Last version that did not exhibit the issue (if applicable): 3
  • React version:18
  • Browser: Chrome
@fzaninotto fzaninotto added the bug label Jun 22, 2022
@thibault-barrat
Copy link
Contributor

I experienced this problem and I can add that <FileInput> uses the source props of its children to create the file which is passed as a record to its children :

const { source, title } = (Children.only(children) as ReactElement<
any
>).props;
const preview = URL.createObjectURL(file);
const transformedFile = {
rawFile: file,
[source]: preview,
};

So, the custom preview component must have a source props to make it work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants