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 <TextInput> missing placeholder with MUI v5.4 #8439

Merged
merged 7 commits into from
Dec 8, 2022

Conversation

kosmotema
Copy link
Contributor

@kosmotema kosmotema commented Nov 26, 2022

Starting with MUI 5.4.0, they changed the way of displaying the label for TextField. Now the label will not be rendered only if null, undefined or "" value is provided, but react-admin passes false as the label (so it will be rendered). This often affects the SearchInput component, which doesn't (and shouldn't) have label: placeholder is shown only when SearchInput is active (because the label replaces the placeholder when the input is not active).

Also, I fixed a bug, where when providing an explicit undefined value to the SearchInput's label property, the default label (based on source) was shown:

<SearchInput source="q" label={undefined} />

@WiXSL
Copy link
Contributor

WiXSL commented Nov 26, 2022

Thank you!

Copy link
Member

@fzaninotto fzaninotto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you show before / after screenshots?

Also, I understand that MUI changed, but I want to be sure that react-admin works with both the old and the new MUI versions. Are you sure this change is backwards compatible?

@@ -8,17 +8,18 @@ import { CommonInputProps } from './CommonInputProps';
import { TextInput, TextInputProps } from './TextInput';

export const SearchInput = (props: SearchInputProps) => {
const { label, ...rest } = props;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why you made this change

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To handle the case when specifying undefined for a SearchInput's label explicitly (like <SearchInput source="q" alwaysOn label={undefined} />) will result in the default label is being displayed.
Reproduction on codesandbox

Copy link
Contributor Author

@kosmotema kosmotema Nov 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also noticed, that it's possible to use SearchInput with 0 label like (added this case to repro on CodeSandbox):

<SearchInput source="q" alwaysOn label={0} />

I think, it would be better to revert all changes in this PR, that related to the SearchInput's label overwriting logic and open a new issue to discuss, what and how to do about this case (IMHO it's better to prevent users from providing a label at all instead of throwing an error).

throw new Error(
"<SearchInput> isn't designed to be used with a label prop. Use <TextInput> if you need a label."
);
}

return (
<StyledTextInput
hiddenLabel
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you remove that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, added it back

@@ -166,4 +166,36 @@ describe('<TextInput />', () => {
expect(input.value).toEqual('bar');
});
});

describe('label', () => {
it('should not render label when `label` prop is `false`', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're missing a test showing that a proper label prop renders a label.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added two more tests: when label not specified and when label is a non-empty string

import { FilterForm } from '../list';

describe('<SearchInput />', () => {
it('should not render label if passed explicit `undefined` value', async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added to cover the case of passing explicit undefined for the SearchInput's label property (more info here)

@kosmotema
Copy link
Contributor Author

kosmotema commented Nov 28, 2022

Could you show before / after screenshots?

I made a reproduction of the bug:

  1. With MUI 5.0
  2. With MUI 5.4
  3. With MUI 5.0 and patched react-admin
  4. With MUI 5.4 and patched react-admin

And with screenshots:
The placeholder is not displayed when using MUI > 5.4 and latest react-admin (because MUI [due to changed logic of displaying a label here] renders the label element with empty text):

mui > 5.4, stable react-admin, placeholder is hidden

But the placeholder is visible after clicking on the SearchInput:

placeholder is visible

With the proposed changes, the placeholder now displayed correctly (both until the SearchInput is focused, and after that):

placholder is visible, SearchInput not focused

placeholder is also visiblde, SearchInput as focused


Are you sure this change is backwards compatible?

Yes, there should be (since MUI made the label's check only a little stricter)

@slax57
Copy link
Contributor

slax57 commented Dec 8, 2022

Tested the fix in TextInput successfully, and the change in SearchInput is harmless, so I'll merge this PR as is.

Thanks for this contribution and well done for the analysis!

@slax57 slax57 added this to the 4.6.1 milestone Dec 8, 2022
@slax57 slax57 merged commit 833f833 into marmelab:master Dec 8, 2022
@slax57 slax57 changed the title Fix bugs in SearchInput and TextField related to the label property Fix <TextInput> missing placeholder with MUI v5.4 Dec 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants