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] Clean the new ownerState object #15056

Merged
merged 4 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -9,7 +9,7 @@ export default function CustomSlotPropsCallback() {
<DatePicker
slotProps={{
openPickerIcon: (ownerState) => ({
color: ownerState.open ? 'secondary' : 'primary',
color: ownerState.isPickerOpen ? 'secondary' : 'primary',
}),
}}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default function CustomSlotPropsCallback() {
<DatePicker
slotProps={{
openPickerIcon: (ownerState) => ({
color: ownerState.open ? 'secondary' : 'primary',
color: ownerState.isPickerOpen ? 'secondary' : 'primary',
}),
}}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<DatePicker
slotProps={{
openPickerIcon: (ownerState) => ({
color: ownerState.open ? 'secondary' : 'primary',
color: ownerState.isPickerOpen ? 'secondary' : 'primary',
}),
}}
/>
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,7 @@ export interface ExportedUseDesktopPickerSlotProps<
{},
UseDesktopPickerProps<TDate, any, TEnableAccessibleFieldDOMStructure, any, any>
>;
openPickerIcon?: SlotComponentPropsFromProps<
Record<string, any>,
{},
PickerOwnerState<TDate | null>
>;
openPickerIcon?: SlotComponentPropsFromProps<Record<string, any>, {}, PickerOwnerState>;
}

export interface DesktopOnlyPickerProps
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const usePicker = <
propsFromPickerViews: pickerViewsResponse.layoutProps,
});

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

return {
// Picker value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,5 @@ export interface UsePickerResponse<
> extends Omit<UsePickerValueResponse<TValue, TSection, TError>, 'viewProps' | 'layoutProps'>,
Omit<UsePickerViewsResponse<TView>, 'layoutProps'>,
UsePickerLayoutPropsResponse<TValue, TView> {
ownerState: PickerOwnerState<TValue>;
ownerState: PickerOwnerState;
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,40 @@
import * as React from 'react';
import { FieldSection, PickerOwnerState } from '../../../models';
import type { UsePickerProps } from './usePicker.types';
import { UsePickerValueResponse } from './usePickerValue.types';
import { PickerValueManager, UsePickerValueResponse } from './usePickerValue.types';
import { useUtils } from '../useUtils';

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

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

const utils = useUtils();

return React.useMemo(
() => ({
value: pickerValueResponse.viewProps.value,
open: pickerValueResponse.open,
disabled: props.disabled ?? false,
readOnly: props.readOnly ?? false,
isPickerValueEmpty: valueManager.areValuesEqual(
utils,
pickerValueResponse.viewProps.value,
valueManager.emptyValue,
),
isPickerOpen: pickerValueResponse.open,
isPickerDisabled: props.disabled ?? false,
isPickerReadOnly: props.readOnly ?? false,
}),
[pickerValueResponse.viewProps.value, pickerValueResponse.open, props.disabled, props.readOnly],
[
utils,
valueManager,
pickerValueResponse.viewProps.value,
pickerValueResponse.open,
props.disabled,
props.readOnly,
],
);
}
12 changes: 6 additions & 6 deletions packages/x-date-pickers/src/models/pickers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ export type PickerValidDate = keyof PickerValidDateLookup extends never
? any
: PickerValidDateLookup[keyof PickerValidDateLookup];

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