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 Input components add a bottom margin even when helperText is false #9037

Merged
merged 17 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ export const ArrayInput = (props: ArrayInputProps) => {
</Labeled>
);
}
const renderHelperText =
helperText !== false || ((isDirty || isSubmitted) && !!error);

return (
<Root
Expand Down Expand Up @@ -186,7 +188,7 @@ export const ArrayInput = (props: ArrayInputProps) => {
disabled,
})}
</ArrayInputContext.Provider>
{!!((isDirty || isSubmitted) && !!error) || helperText ? (
{renderHelperText ? (
<FormHelperText error={(isDirty || isSubmitted) && !!error}>
<InputHelperText
touched={isDirty || isSubmitted}
Expand Down
20 changes: 15 additions & 5 deletions packages/ra-ui-materialui/src/input/AutocompleteInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,10 @@ If you provided a React element for the optionText prop, you must also provide t
const isOptionEqualToValue = (option, value) => {
return String(getChoiceValue(option)) === String(getChoiceValue(value));
};
const renderHelperText =
!!fetchError ||
helperText !== false ||
((isTouched || isSubmitted) && invalid);

return (
<>
Expand Down Expand Up @@ -565,11 +569,17 @@ If you provided a React element for the optionText prop, you must also provide t
((isTouched || isSubmitted) && invalid)
}
helperText={
<InputHelperText
touched={isTouched || isSubmitted || fetchError}
error={error?.message || fetchError?.message}
helperText={helperText}
/>
renderHelperText ? (
<InputHelperText
touched={
isTouched || isSubmitted || fetchError
}
error={
error?.message || fetchError?.message
}
helperText={helperText}
/>
) : null
}
margin={margin}
variant={variant}
Expand Down
19 changes: 12 additions & 7 deletions packages/ra-ui-materialui/src/input/BooleanInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ export const BooleanInput = (props: BooleanInputProps) => {
[field]
);

const renderHelperText =
helperText !== false || ((isTouched || isSubmitted) && invalid);

return (
<FormGroup
className={clsx('ra-input', `ra-input-${source}`, className)}
Expand Down Expand Up @@ -91,13 +94,15 @@ export const BooleanInput = (props: BooleanInputProps) => {
/>
}
/>
<FormHelperText error={(isTouched || isSubmitted) && invalid}>
<InputHelperText
touched={isTouched || isSubmitted}
error={error?.message}
helperText={helperText}
/>
</FormHelperText>
{renderHelperText ? (
<FormHelperText error={(isTouched || isSubmitted) && invalid}>
<InputHelperText
touched={isTouched || isSubmitted}
error={error?.message}
helperText={helperText}
/>
</FormHelperText>
) : null}
</FormGroup>
);
};
Expand Down
29 changes: 19 additions & 10 deletions packages/ra-ui-materialui/src/input/CheckboxGroupInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ export const CheckboxGroupInput: FunctionComponent<CheckboxGroupInputProps> = pr
);
}

const renderHelperText =
!!fetchError ||
helperText !== false ||
((isTouched || isSubmitted) && invalid);

return (
<StyledFormControl
component="fieldset"
Expand Down Expand Up @@ -235,16 +240,20 @@ export const CheckboxGroupInput: FunctionComponent<CheckboxGroupInputProps> = pr
/>
))}
</FormGroup>
<FormHelperText
error={fetchError || ((isTouched || isSubmitted) && !!error)}
className={CheckboxGroupInputClasses.helperText}
>
<InputHelperText
touched={isTouched || isSubmitted || fetchError}
error={error?.message || fetchError?.message}
helperText={helperText}
/>
</FormHelperText>
{renderHelperText ? (
<FormHelperText
error={
fetchError || ((isTouched || isSubmitted) && !!error)
}
className={CheckboxGroupInputClasses.helperText}
>
<InputHelperText
touched={isTouched || isSubmitted || fetchError}
error={error?.message || fetchError?.message}
helperText={helperText}
/>
</FormHelperText>
) : null}
</StyledFormControl>
);
};
Expand Down
14 changes: 9 additions & 5 deletions packages/ra-ui-materialui/src/input/DateInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ export const DateInput = ({

const { error, invalid, isTouched } = fieldState;
const { isSubmitted } = formState;
const renderHelperText =
helperText !== false || ((isTouched || isSubmitted) && invalid);

return (
<TextField
Expand All @@ -74,11 +76,13 @@ export const DateInput = ({
margin={margin}
error={(isTouched || isSubmitted) && invalid}
helperText={
<InputHelperText
touched={isTouched || isSubmitted}
error={error?.message}
helperText={helperText}
/>
renderHelperText ? (
<InputHelperText
touched={isTouched || isSubmitted}
error={error?.message}
helperText={helperText}
/>
) : null
}
label={
<FieldTitle
Expand Down
15 changes: 9 additions & 6 deletions packages/ra-ui-materialui/src/input/DateTimeInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ export const DateTimeInput = ({

const { error, invalid, isTouched } = fieldState;
const { isSubmitted } = formState;

const renderHelperText =
helperText !== false || ((isTouched || isSubmitted) && invalid);
return (
<TextField
id={id}
Expand All @@ -63,11 +64,13 @@ export const DateTimeInput = ({
margin={margin}
error={(isTouched || isSubmitted) && invalid}
helperText={
<InputHelperText
touched={isTouched || isSubmitted}
error={error?.message}
helperText={helperText}
/>
renderHelperText ? (
<InputHelperText
touched={isTouched || isSubmitted}
error={error?.message}
helperText={helperText}
/>
) : null
}
label={
<FieldTitle
Expand Down
22 changes: 15 additions & 7 deletions packages/ra-ui-materialui/src/input/FileInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ export const FileInput = (props: FileInputProps) => {
onDrop,
});

const renderHelperText =
helperText !== false || ((isTouched || isSubmitted) && invalid);

return (
<StyledLabeled
htmlFor={id}
Expand Down Expand Up @@ -180,13 +183,18 @@ export const FileInput = (props: FileInputProps) => {
<p>{translate(labelSingle)}</p>
)}
</div>
<FormHelperText error={(isTouched || isSubmitted) && invalid}>
<InputHelperText
touched={isTouched || isSubmitted}
error={error?.message}
helperText={helperText}
/>
</FormHelperText>
{renderHelperText ? (
<FormHelperText
error={(isTouched || isSubmitted) && invalid}
>
<InputHelperText
touched={isTouched || isSubmitted}
error={error?.message}
helperText={helperText}
/>
</FormHelperText>
) : null}

{children && (
<div className="previews">
{files.map((file, index) => (
Expand Down
15 changes: 9 additions & 6 deletions packages/ra-ui-materialui/src/input/NullableBooleanInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ export const NullableBooleanInput = (props: NullableBooleanInputProps) => {
validate,
...rest,
});

const renderHelperText =
helperText !== false || ((isTouched || isSubmitted) && invalid);
return (
<StyledTextField
id={id}
Expand All @@ -72,11 +73,13 @@ export const NullableBooleanInput = (props: NullableBooleanInputProps) => {
}
error={(isTouched || isSubmitted) && invalid}
helperText={
<InputHelperText
touched={isTouched || isSubmitted}
error={error?.message}
helperText={helperText}
/>
renderHelperText ? (
<InputHelperText
touched={isTouched || isSubmitted}
error={error?.message}
helperText={helperText}
/>
) : null
}
variant={variant}
{...sanitizeInputRestProps(rest)}
Expand Down
15 changes: 10 additions & 5 deletions packages/ra-ui-materialui/src/input/NumberInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ export const NumberInput = ({
setValue(value => (value !== stringValue ? stringValue : value));
};

const renderHelperText =
helperText !== false || ((isTouched || isSubmitted) && invalid);

return (
<TextField
id={id}
Expand All @@ -133,11 +136,13 @@ export const NumberInput = ({
variant={variant}
error={(isTouched || isSubmitted) && invalid}
helperText={
<InputHelperText
touched={isTouched || isSubmitted}
error={error?.message}
helperText={helperText}
/>
renderHelperText ? (
<InputHelperText
touched={isTouched || isSubmitted}
error={error?.message}
helperText={helperText}
/>
) : null
}
label={
<FieldTitle
Expand Down
22 changes: 15 additions & 7 deletions packages/ra-ui-materialui/src/input/RadioButtonGroupInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ export const RadioButtonGroupInput = (props: RadioButtonGroupInputProps) => {
</Labeled>
);
}

const renderHelperText =
!!fetchError ||
helperText !== false ||
((isTouched || isSubmitted) && invalid);

return (
<StyledFormControl
component="fieldset"
Expand Down Expand Up @@ -196,13 +202,15 @@ export const RadioButtonGroupInput = (props: RadioButtonGroupInputProps) => {
/>
))}
</RadioGroup>
<FormHelperText>
<InputHelperText
touched={isTouched || isSubmitted || fetchError}
error={error?.message || fetchError?.message}
helperText={helperText}
/>
</FormHelperText>
{renderHelperText ? (
<FormHelperText>
<InputHelperText
touched={isTouched || isSubmitted || fetchError}
error={error?.message || fetchError?.message}
helperText={helperText}
/>
</FormHelperText>
) : null}
</StyledFormControl>
);
};
Expand Down
22 changes: 15 additions & 7 deletions packages/ra-ui-materialui/src/input/SelectArrayInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ export const SelectArrayInput = (props: SelectArrayInputProps) => {
),
}
: {};
const renderHelperText =
!!fetchError ||
helperText !== false ||
((isTouched || isSubmitted) && invalid);

return (
<>
Expand Down Expand Up @@ -337,13 +341,17 @@ export const SelectArrayInput = (props: SelectArrayInputProps) => {
>
{finalChoices.map(renderMenuItem)}
</Select>
<FormHelperText error={fetchError || (isTouched && !!error)}>
<InputHelperText
touched={isTouched || isSubmitted || fetchError}
error={error?.message || fetchError?.message}
helperText={helperText}
/>
</FormHelperText>
{renderHelperText ? (
<FormHelperText
error={fetchError || (isTouched && !!error)}
>
<InputHelperText
touched={isTouched || isSubmitted || fetchError}
error={error?.message || fetchError?.message}
helperText={helperText}
/>
</FormHelperText>
) : null}
</StyledFormControl>
{createElement}
</>
Expand Down
17 changes: 12 additions & 5 deletions packages/ra-ui-materialui/src/input/SelectInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,11 @@ export const SelectInput = (props: SelectInputProps) => {
);
}

const renderHelperText =
!!fetchError ||
helperText !== false ||
((isTouched || isSubmitted) && invalid);

return (
<>
<StyledResettableTextField
Expand All @@ -318,11 +323,13 @@ export const SelectInput = (props: SelectInputProps) => {
clearAlwaysVisible
error={!!fetchError || ((isTouched || isSubmitted) && invalid)}
helperText={
<InputHelperText
touched={isTouched || isSubmitted || fetchError}
error={error?.message || fetchError?.message}
helperText={helperText}
/>
renderHelperText ? (
<InputHelperText
touched={isTouched || isSubmitted || fetchError}
error={error?.message || fetchError?.message}
helperText={helperText}
/>
) : null
}
margin={margin}
{...sanitizeRestProps(rest)}
Expand Down
Loading