Skip to content

Commit

Permalink
[fields] Fix placeholder override (#12589)
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviendelangle committed Mar 28, 2024
1 parent 8644e2a commit 3341a1a
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,59 @@ describe('<DesktopDatePicker /> - 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ export interface UseFieldV6ForwardedProps {
onClick?: React.MouseEventHandler;
onFocus?: () => void;
onPaste?: React.ClipboardEventHandler<HTMLDivElement>;
placeholder?: string;
}

interface UseFieldV6AdditionalProps
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,14 @@ export const useFieldV6TextField: UseFieldTextField<false> = (params) => {
const focusTimeoutRef = React.useRef<ReturnType<typeof setTimeout>>();

const {
forwardedProps: { onFocus, onClick, onPaste, onBlur, inputRef: inputRefProp },
forwardedProps: {
onFocus,
onClick,
onPaste,
onBlur,
inputRef: inputRefProp,
placeholder: inPlaceholder,
},
internalProps: { readOnly = false },
parsedSelectedSections,
activeSectionIndex,
Expand Down Expand Up @@ -381,15 +388,24 @@ export const useFieldV6TextField: UseFieldTextField<false> = (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(
() =>
Expand Down

0 comments on commit 3341a1a

Please sign in to comment.