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

[fields] Allow empty textField slot placeholder value #13148

Merged
merged 8 commits into from
May 16, 2024

Conversation

arthurbalduini
Copy link
Member

@arthurbalduini arthurbalduini commented May 16, 2024

Fixes #12573.

Add some tests to prevent it in the future.

It should now be possible to pass the placeholder prop as either an empty string or undefined in order to achieve an empty placeholder in textField component. Sending no placeholder prop must fall into the default placeholder, generated specifically for each kind of picker.

@arthurbalduini arthurbalduini added bug 🐛 Something doesn't work component: pickers This is the name of the generic UI component, not the React module! labels May 16, 2024
@arthurbalduini arthurbalduini requested a review from a team May 16, 2024 07:18
@mui-bot
Copy link

mui-bot commented May 16, 2024

Deploy preview: https://deploy-preview-13148--material-ui-x.netlify.app/

Generated by 🚫 dangerJS against fd2ea8c

@arthurbalduini arthurbalduini requested review from LukasTy and removed request for a team May 16, 2024 07:28
@arthurbalduini arthurbalduini changed the title Empty placeholder [Pickers] Fix override textField placeholder with empty value May 16, 2024
@arthurbalduini arthurbalduini changed the title [Pickers] Fix override textField placeholder with empty value [Fields] Fix override textField placeholder with empty value May 16, 2024
@@ -390,8 +390,8 @@ export const useFieldV6TextField: UseFieldTextField<false> = (params) => {
});

const placeholder = React.useMemo(() => {
if (inPlaceholder) {
return inPlaceholder;
if (params.forwardedProps.hasOwnProperty('placeholder')) {
Copy link
Member

Choose a reason for hiding this comment

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

How about simply doing:

Suggested change
if (params.forwardedProps.hasOwnProperty('placeholder')) {
if (inPlaceholder !== undefined) {

I think that it would be more consistent with the other props in our codebase.
Usually when you pass undefined, we fallback to the internal value rather than applying undefined (for example in props.value or props.views)

Copy link
Member Author

Choose a reason for hiding this comment

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

My thought to decide to use this approach was to be able to differentiate the scenarios:

  • user explicitly passes the placeholder value as undefined
  • user doesn't pass any placeholder prop

In both cases inPlaceholder will be undefined, but on the second scenario, we don't pass forward the prop, so the params.forwardedProps.hasOwnProperty('placeholder') will be false and it will fall on the default placeholder, on the other hand if users explicitly passes the prop as undefined, I assume they wish to have an empty placeholder. WDYT ?

Copy link
Member

Choose a reason for hiding this comment

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

I totally understand your approach
IMHO, we should render the default placeholder when the user renders <DateField placeholder={undefined} />, because it's closer from the other props / components behavior (<DateField minDate={undefined} /> will apply the 01-01-1900 as the min date rather than removing the boundary, <DateField value={undefined} /> will fallback to the internal state rather than enforcing an empty value, etc...).

Copy link
Member

Choose a reason for hiding this comment

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

I agree with @flaviendelangle on this one, let's go for simplicity. 👍
In "my book", undefined == no prop.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, folks. Done in 658c29c

@@ -400,6 +400,7 @@ export const useFieldV6TextField: UseFieldTextField<false> = (params) => {
isRTL,
);
}, [
params.forwardedProps,
Copy link
Member

Choose a reason for hiding this comment

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

This could force the placeholder to be recalculated more often than we'd want. 🙈

if (inPlaceholder) {
return inPlaceholder;
if (params.forwardedProps.hasOwnProperty('placeholder')) {
return inPlaceholder ?? '';
Copy link
Member

Choose a reason for hiding this comment

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

Have you tried testing Flavien's suggestion?
I think that with it we could remove the ?? '' here. 🤔

Copy link
Member

Choose a reason for hiding this comment

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

And remove params.forwardedProps from the deps

Copy link
Member Author

Choose a reason for hiding this comment

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

Oops 🙈
Done in d9d4964 and 78007e0

@LukasTy LukasTy changed the title [Fields] Fix override textField placeholder with empty value [fields] Allow empty textField placeholder value May 16, 2024
@LukasTy LukasTy changed the title [fields] Allow empty textField placeholder value [fields] Allow empty textFieldslot placeholder value May 16, 2024
@LukasTy LukasTy changed the title [fields] Allow empty textFieldslot placeholder value [fields] Allow empty textField slot placeholder value May 16, 2024
Copy link
Member

@LukasTy LukasTy left a comment

Choose a reason for hiding this comment

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

LGTM! 👏
Thank you, nice work! 💯 👍

Co-authored-by: Lukas <llukas.tyla@gmail.com>
Signed-off-by: Arthur Suh Balduini <34691066+arthurbalduini@users.noreply.github.com>
Copy link
Member

@flaviendelangle flaviendelangle left a comment

Choose a reason for hiding this comment

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

Nice work 👌

The test is super clean

Co-authored-by: Flavien DELANGLE <flaviendelangle@gmail.com>
Signed-off-by: Arthur Suh Balduini <34691066+arthurbalduini@users.noreply.github.com>
@arthurbalduini arthurbalduini merged commit c4cc7a3 into mui:master May 16, 2024
17 checks passed
arthurbalduini added a commit to arthurbalduini/mui-x that referenced this pull request May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work component: pickers This is the name of the generic UI component, not the React module!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[pickers] It is no longer possible to override the placeholder value
4 participants