Skip to content

Commit

Permalink
[pickers] Use the new ownerState in useClearableField (#15776)
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviendelangle authored Dec 6, 2024
1 parent 64ba0c3 commit 6c78434
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
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

0 comments on commit 6c78434

Please sign in to comment.