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

[pickers] Use the new ownerState in useClearableField #15776

Merged
merged 2 commits into from
Dec 6, 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
16 changes: 10 additions & 6 deletions packages/x-date-pickers/src/hooks/useClearableField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import InputAdornment from '@mui/material/InputAdornment';
import { SxProps } from '@mui/system';
import { ClearIcon } from '../icons';
import { usePickerTranslations } from './usePickerTranslations';
import { FieldOwnerState } from '../models/fields';
import { useFieldOwnerState } from '../internals/hooks/useFieldOwnerState';
import { FormProps } from '../internals/models';

export interface ExportedUseClearableFieldProps {
/**
Expand Down Expand Up @@ -34,11 +37,11 @@ export interface UseClearableFieldSlots {
}

export interface UseClearableFieldSlotProps {
clearIcon?: SlotComponentProps<typeof ClearIcon, {}, {}>;
clearButton?: SlotComponentProps<typeof MuiIconButton, {}, {}>;
clearIcon?: SlotComponentProps<typeof ClearIcon, {}, FieldOwnerState>;
clearButton?: SlotComponentProps<typeof MuiIconButton, {}, FieldOwnerState>;
}

interface UseClearableFieldProps extends ExportedUseClearableFieldProps {
interface UseClearableFieldProps extends ExportedUseClearableFieldProps, FormProps {
InputProps?: { endAdornment?: React.ReactNode };
sx?: SxProps<any>;
slots?: UseClearableFieldSlots;
Expand All @@ -54,15 +57,16 @@ export const useClearableField = <TFieldProps extends UseClearableFieldProps>(
props: TFieldProps,
): UseClearableFieldResponse<TFieldProps> => {
const translations = usePickerTranslations();
const ownerState = useFieldOwnerState(props);

const { clearable, onClear, InputProps, sx, slots, slotProps, ...other } = props;

const IconButton = slots?.clearButton ?? MuiIconButton;
// The spread is here to avoid this bug mui/material-ui#34056
const { ownerState, ...iconButtonProps } = useSlotProps({
const { ownerState: iconButtonOwnerState, ...iconButtonProps } = useSlotProps({
elementType: IconButton,
externalSlotProps: slotProps?.clearButton,
ownerState: {},
ownerState,
className: 'clearButton',
additionalProps: {
title: translations.fieldClearLabel,
Expand All @@ -73,7 +77,7 @@ export const useClearableField = <TFieldProps extends UseClearableFieldProps>(
const endClearIconProps = useSlotProps({
elementType: EndClearIcon,
externalSlotProps: slotProps?.clearIcon,
ownerState: {},
ownerState,
});

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { PickerOwnerState } from '../../models';
import { PickersInputLocaleText } from '../../locales';
import { LocalizationProvider } from '../../LocalizationProvider';
import { PickerOrientation, PickerVariant } from '../models';
import { UsePickerValueContextValue } from '../hooks/usePicker/usePickerValue.types';
import type { UsePickerValueContextValue } from '../hooks/usePicker/usePickerValue.types';

export const PickerContext = React.createContext<PickerContextValue | null>(null);

Expand Down
Loading