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] Fix placeholder override #12589

Merged
merged 2 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -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
Loading