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
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -138,18 +138,57 @@ describe('<DesktopDatePicker /> - Field', () => {
Component: DesktopDatePicker,
});

it('should allow to override the placeholder (v6 only)', () => {
renderWithProps({
enableAccessibleFieldDOMStructure: false,
slotProps: {
textField: {
placeholder: 'Custom placeholder',
describe('Placeholder override (v6 only)', () => {
arthurbalduini marked this conversation as resolved.
Show resolved Hide resolved
it('should allow to override the placeholder', () => {
renderWithProps({
enableAccessibleFieldDOMStructure: false,
slotProps: {
textField: {
placeholder: 'Custom placeholder',
},
},
},
});

const input = getTextbox();
expectFieldPlaceholderV6(input, 'Custom placeholder');
});

const input = getTextbox();
expectFieldPlaceholderV6(input, 'Custom placeholder');
it('should show blank placeholder when prop is an empty string', () => {
renderWithProps({
enableAccessibleFieldDOMStructure: false,
slotProps: {
textField: {
placeholder: '',
},
},
});

const input = getTextbox();
expectFieldPlaceholderV6(input, '');
});

it('should show blank placeholder when prop is undefined', () => {
renderWithProps({
enableAccessibleFieldDOMStructure: false,
slotProps: {
textField: {
placeholder: undefined,
},
},
});

const input = getTextbox();
expectFieldPlaceholderV6(input, '');
});

it('should show default placeholder when no prop is received', () => {
renderWithProps({
enableAccessibleFieldDOMStructure: false,
});

const input = getTextbox();
expectFieldPlaceholderV6(input, 'MM/DD/YYYY');
});
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

@arthurbalduini arthurbalduini May 16, 2024

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

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

}

return fieldValueManager.getV6InputValueFromSections(
Expand All @@ -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. 🙈

inPlaceholder,
fieldValueManager,
getSectionsFromValue,
Expand Down