Skip to content

Commit

Permalink
[pickers] Prepare new picker ownerState object
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviendelangle committed Oct 9, 2024
1 parent b4b1794 commit baa73e9
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import useForkRef from '@mui/utils/useForkRef';
import useId from '@mui/utils/useId';
import { PickersPopper } from '../../components/PickersPopper';
import {
UseDesktopPickerOwnerState,
UseDesktopPickerParams,
UseDesktopPickerProps,
UseDesktopPickerSlotProps,
Expand Down Expand Up @@ -81,6 +80,7 @@ export const useDesktopPicker = <
shouldRestoreFocus,
fieldProps: pickerFieldProps,
contextValue,
ownerState,
} = usePicker<TDate | null, TDate, TView, FieldSection, TExternalProps, {}>({
...pickerParams,
props,
Expand All @@ -90,11 +90,6 @@ export const useDesktopPicker = <
wrapperVariant: 'desktop',
});

// TODO v8: Apply this ownerState to all the slots in this hook.
const ownerStateV8: UseDesktopPickerOwnerState = {
open,
};

const InputAdornment = slots.inputAdornment ?? MuiInputAdornment;
const { ownerState: inputAdornmentOwnerState, ...inputAdornmentProps } = useSlotProps({
elementType: InputAdornment,
Expand Down Expand Up @@ -122,7 +117,7 @@ export const useDesktopPicker = <
const openPickerIconProps = useSlotProps({
elementType: OpenPickerIcon,
externalSlotProps: innerSlotProps?.openPickerIcon,
ownerState: ownerStateV8,
ownerState,
});

const Field = slots.field;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
UseClearableFieldSlotProps,
} from '../../../hooks/useClearableField';
import { SlotComponentPropsFromProps } from '../../models/helpers';
import { PickerOwnerState } from '../usePicker/usePickerOwnerState';

export interface UseDesktopPickerSlots<
TDate extends PickerValidDate,
Expand Down Expand Up @@ -66,10 +67,6 @@ export interface UseDesktopPickerSlotProps<
> extends ExportedUseDesktopPickerSlotProps<TDate, TView, TEnableAccessibleFieldDOMStructure>,
Pick<PickersLayoutSlotProps<TDate | null, TDate, TView>, 'toolbar'> {}

export interface UseDesktopPickerOwnerState {
open: boolean;
}

export interface ExportedUseDesktopPickerSlotProps<
TDate extends PickerValidDate,
TView extends DateOrTimeViewWithMeridiem,
Expand All @@ -95,7 +92,11 @@ export interface ExportedUseDesktopPickerSlotProps<
{},
UseDesktopPickerProps<TDate, any, TEnableAccessibleFieldDOMStructure, any, any>
>;
openPickerIcon?: SlotComponentPropsFromProps<Record<string, any>, {}, UseDesktopPickerOwnerState>;
openPickerIcon?: SlotComponentPropsFromProps<
Record<string, any>,
{},
PickerOwnerState<TDate | null>
>;
}

export interface DesktopOnlyPickerProps
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { usePickerViews } from './usePickerViews';
import { usePickerLayoutProps } from './usePickerLayoutProps';
import { FieldSection, PickerValidDate, InferError } from '../../../models';
import { DateOrTimeViewWithMeridiem } from '../../models';
import { usePickerOwnerState } from './usePickerOwnerState';

export const usePicker = <
TValue,
Expand Down Expand Up @@ -71,6 +72,8 @@ export const usePicker = <
propsFromPickerViews: pickerViewsResponse.layoutProps,
});

const pickerOwnerState = usePickerOwnerState({ props, pickerValueResponse });

return {
// Picker value
open: pickerValueResponse.open,
Expand All @@ -87,5 +90,8 @@ export const usePicker = <

// Picker context
contextValue: pickerValueResponse.contextValue,

// Picker owner state
ownerState: pickerOwnerState,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import { UsePickerLayoutProps, UsePickerLayoutPropsResponse } from './usePickerLayoutProps';
import { FieldSection, PickerValidDate } from '../../../models';
import { DateOrTimeViewWithMeridiem } from '../../models';
import { PickerOwnerState } from './usePickerOwnerState';

/**
* Props common to all picker headless implementations.
Expand Down Expand Up @@ -64,4 +65,6 @@ export interface UsePickerResponse<
TError,
> extends Omit<UsePickerValueResponse<TValue, TSection, TError>, 'viewProps' | 'layoutProps'>,
Omit<UsePickerViewsResponse<TView>, 'layoutProps'>,
UsePickerLayoutPropsResponse<TValue, TView> {}
UsePickerLayoutPropsResponse<TValue, TView> {
ownerState: PickerOwnerState<TValue>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { FieldSection } from '../../../models';
import type { UsePickerProps } from './usePicker.types';
import { UsePickerValueResponse } from './usePickerValue.types';

export interface PickerOwnerState<TValue> {
/**
* The value currently displayed in the field and in the view.
*/
value: TValue;
/**
* `true` if the picker is open, `false` otherwise.
*/
open: boolean;
/**
* `true` if the picker is disabled, `false` otherwise.
*/
disabled: boolean;
/**
* `true` if the picker is read-only, `false` otherwise.
*/
readOnly: boolean;
}

interface UsePickerOwnerStateParameters<TValue> {
props: UsePickerProps<TValue, any, any, any, any, any>;
pickerValueResponse: UsePickerValueResponse<TValue, FieldSection, any>;
}

export function usePickerOwnerState<TValue>(
parameters: UsePickerOwnerStateParameters<TValue>,
): PickerOwnerState<TValue> {
const { props, pickerValueResponse } = parameters;

return {
value: pickerValueResponse.viewProps.value,
open: pickerValueResponse.open,
disabled: props.disabled ?? false,
readOnly: props.readOnly ?? false,
};
}

0 comments on commit baa73e9

Please sign in to comment.