From 087c82e55cc0fffc0362ca70b841b94bfc69cc27 Mon Sep 17 00:00:00 2001 From: delangle Date: Thu, 28 Mar 2024 10:52:04 +0100 Subject: [PATCH 1/2] [fields] Fix placeholder override --- .../hooks/useField/useField.types.ts | 1 + .../hooks/useField/useFieldV6TextField.ts | 36 +++++++++++++------ 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/packages/x-date-pickers/src/internals/hooks/useField/useField.types.ts b/packages/x-date-pickers/src/internals/hooks/useField/useField.types.ts index 4d82ca28b4ad..d60d89b6efc0 100644 --- a/packages/x-date-pickers/src/internals/hooks/useField/useField.types.ts +++ b/packages/x-date-pickers/src/internals/hooks/useField/useField.types.ts @@ -170,6 +170,7 @@ export interface UseFieldV6ForwardedProps { onClick?: React.MouseEventHandler; onFocus?: () => void; onPaste?: React.ClipboardEventHandler; + placeholder?: string; } interface UseFieldV6AdditionalProps diff --git a/packages/x-date-pickers/src/internals/hooks/useField/useFieldV6TextField.ts b/packages/x-date-pickers/src/internals/hooks/useField/useFieldV6TextField.ts index 6c666fa22ddd..4f8c89d11f41 100644 --- a/packages/x-date-pickers/src/internals/hooks/useField/useFieldV6TextField.ts +++ b/packages/x-date-pickers/src/internals/hooks/useField/useFieldV6TextField.ts @@ -80,7 +80,14 @@ export const useFieldV6TextField: UseFieldTextField = (params) => { const focusTimeoutRef = React.useRef>(); const { - forwardedProps: { onFocus, onClick, onPaste, onBlur, inputRef: inputRefProp }, + forwardedProps: { + onFocus, + onClick, + onPaste, + onBlur, + inputRef: inputRefProp, + placeholder: inPlaceholder, + }, internalProps: { readOnly = false }, parsedSelectedSections, activeSectionIndex, @@ -381,15 +388,24 @@ export const useFieldV6TextField: UseFieldTextField = (params) => { applyCharacterEditing({ keyPressed, sectionIndex: activeSectionIndex }); }); - const placeholder = React.useMemo( - () => - fieldValueManager.getV6InputValueFromSections( - getSectionsFromValue(valueManager.emptyValue), - localizedDigits, - isRTL, - ), - [fieldValueManager, getSectionsFromValue, valueManager.emptyValue, localizedDigits, isRTL], - ); + const placeholder = React.useMemo(() => { + if (inPlaceholder) { + return inPlaceholder; + } + + return fieldValueManager.getV6InputValueFromSections( + getSectionsFromValue(valueManager.emptyValue), + localizedDigits, + isRTL, + ); + }, [ + inPlaceholder, + fieldValueManager, + getSectionsFromValue, + valueManager.emptyValue, + localizedDigits, + isRTL, + ]); const valueStr = React.useMemo( () => From 3fec8b64788cb0eed002773f576c72eafd9a4ddf Mon Sep 17 00:00:00 2001 From: delangle Date: Thu, 28 Mar 2024 11:08:12 +0100 Subject: [PATCH 2/2] Add tests --- .../tests/field.DesktopDatePicker.test.tsx | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/packages/x-date-pickers/src/DesktopDatePicker/tests/field.DesktopDatePicker.test.tsx b/packages/x-date-pickers/src/DesktopDatePicker/tests/field.DesktopDatePicker.test.tsx index 44097f0832b1..e485c71da98f 100644 --- a/packages/x-date-pickers/src/DesktopDatePicker/tests/field.DesktopDatePicker.test.tsx +++ b/packages/x-date-pickers/src/DesktopDatePicker/tests/field.DesktopDatePicker.test.tsx @@ -100,6 +100,59 @@ describe(' - Field', () => { }); }); + describe('slots: field', () => { + const { render, clock } = createPickerRenderer({ + clock: 'fake', + clockConfig: new Date('2018-01-01T10:05:05.000'), + }); + const { renderWithProps } = buildFieldInteractions({ + clock, + render, + Component: DesktopDatePicker, + }); + + it('should allow to override the placeholder (v6 only)', () => { + renderWithProps({ + enableAccessibleFieldDOMStructure: false, + slotProps: { + field: { + // @ts-ignore + placeholder: 'Custom placeholder', + }, + }, + }); + + const input = getTextbox(); + expectFieldPlaceholderV6(input, 'Custom placeholder'); + }); + }); + + describe('slots: textField', () => { + const { render, clock } = createPickerRenderer({ + clock: 'fake', + clockConfig: new Date('2018-01-01T10:05:05.000'), + }); + const { renderWithProps } = buildFieldInteractions({ + clock, + render, + Component: DesktopDatePicker, + }); + + it('should allow to override the placeholder (v6 only)', () => { + renderWithProps({ + enableAccessibleFieldDOMStructure: false, + slotProps: { + textField: { + placeholder: 'Custom placeholder', + }, + }, + }); + + const input = getTextbox(); + expectFieldPlaceholderV6(input, 'Custom placeholder'); + }); + }); + describeAdapters('Timezone', DesktopDatePicker, ({ adapter, renderWithProps }) => { it('should clear the selected section when all sections are completed when using timezones', () => { const v7Response = renderWithProps(