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

Fix FileInput helperText display when validation prop is set #8150

Merged
merged 5 commits into from
Sep 8, 2022
Merged
Changes from all 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
11 changes: 10 additions & 1 deletion packages/ra-ui-materialui/src/input/FileInput.stories.tsx
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ import { Create } from '../detail';
import { SimpleForm } from '../form';
import { FileInput } from './FileInput';
import { FileField } from '../field';
import { required } from 'ra-core';

export default { title: 'ra-ui-materialui/input/FileInput' };

@@ -26,6 +27,14 @@ export const LimitByFileType = () => (
</Wrapper>
);

export const Required = () => (
<Wrapper>
<FileInput source="attachment" isRequired validate={required()}>
<FileField source="src" title="title" />
</FileInput>
</Wrapper>
);

export const CustomPreview = () => (
<Wrapper>
<FileInput source="attachment" accept="image/*">
@@ -60,7 +69,7 @@ export const FullWidth = () => (

export const Disabled = () => (
<Wrapper>
<FileInput source="attachment" disabled>
<FileInput source="attachment" options={{ disabled: true }}>
<FileField source="src" title="title" />
</FileInput>
</Wrapper>
5 changes: 3 additions & 2 deletions packages/ra-ui-materialui/src/input/FileInput.tsx
Original file line number Diff line number Diff line change
@@ -91,7 +91,7 @@ export const FileInput = (props: FileInputProps) => {
validate,
...rest,
});
const { isTouched, error } = fieldState;
const { isTouched, error, invalid } = fieldState;
const files = value ? (Array.isArray(value) ? value : [value]) : [];

const onDrop = (newFiles, rejectedFiles, event) => {
@@ -152,6 +152,7 @@ export const FileInput = (props: FileInputProps) => {
source={source}
resource={resource}
isRequired={isRequired}
color={(isTouched || isSubmitted) && invalid && 'error'}
{...sanitizeInputRestProps(rest)}
>
<>
@@ -176,7 +177,7 @@ export const FileInput = (props: FileInputProps) => {
<p>{translate(labelSingle)}</p>
)}
</div>
<FormHelperText>
<FormHelperText error={(isTouched || isSubmitted) && invalid}>
<InputHelperText
touched={isTouched || isSubmitted}
error={error?.message}
17 changes: 13 additions & 4 deletions packages/ra-ui-materialui/src/input/ImageInput.stories.tsx
Original file line number Diff line number Diff line change
@@ -7,21 +7,22 @@ import { Create } from '../detail';
import { SimpleForm } from '../form';
import { ImageInput } from './ImageInput';
import { ImageField } from '../field';
import { required } from 'ra-core';

export default { title: 'ra-ui-materialui/input/ImageInput' };

export const Basic = () => (
<Wrapper>
<ImageInput source="image">
<ImageField source="attachment" title="title" />
<ImageField source="src" title="title" />
</ImageInput>
</Wrapper>
);

export const LimitByFileType = () => (
<Wrapper>
<ImageInput source="image" accept="image/png">
<ImageField source="attachment" title="title" />
<ImageField source="src" title="title" />
</ImageInput>
</Wrapper>
);
@@ -35,7 +36,7 @@ export const CustomPreview = () => (
borderColor: 'blue',
borderStyle: 'solid',
}}
source="attachment"
source="src"
title="title"
/>
</ImageInput>
@@ -60,7 +61,15 @@ export const FullWidth = () => (

export const Disabled = () => (
<Wrapper>
<ImageInput source="attachment" disabled>
<ImageInput source="attachment" options={{ disabled: true }}>
<ImageField source="src" title="title" />
</ImageInput>
</Wrapper>
);

export const Required = () => (
<Wrapper>
<ImageInput source="attachment" isRequired validate={required()}>
<ImageField source="src" title="title" />
</ImageInput>
</Wrapper>