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 <PickersCalendarHeader />, <PickersArrowSwitcher /> and <DayCalendarSkeleton /> #15499

Merged
merged 5 commits into from
Nov 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 @@ -20,8 +20,7 @@ export interface PickersFadeTransitionGroupProps {
classes?: Partial<PickersFadeTransitionGroupClasses>;
}

const useUtilityClasses = (ownerState: PickersFadeTransitionGroupProps) => {
const { classes } = ownerState;
const useUtilityClasses = (classes: Partial<PickersFadeTransitionGroupClasses> | undefined) => {
const slots = {
root: ['root'],
};
Expand All @@ -43,9 +42,10 @@ const PickersFadeTransitionGroupRoot = styled(TransitionGroup, {
*/
export function PickersFadeTransitionGroup(inProps: PickersFadeTransitionGroupProps) {
const props = useThemeProps({ props: inProps, name: 'MuiPickersFadeTransitionGroup' });
const { children, className, reduceAnimations, transKey } = props;
const classes = useUtilityClasses(props);
const { children, className, reduceAnimations, transKey, classes: classesProp } = props;
const classes = useUtilityClasses(classesProp);
const theme = useTheme();

if (reduceAnimations) {
return children;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@ import {
pickersSlideTransitionClasses,
PickersSlideTransitionClasses,
} from './pickersSlideTransitionClasses';
import { PickerOwnerState } from '../models/pickers';
import { usePickerPrivateContext } from '../internals/hooks/usePickerPrivateContext';

export type SlideDirection = 'right' | 'left';

interface PickerSlideTransitionOwnerState extends PickerOwnerState {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not used in any styleOverrides, only in useUtilityClasses but I'm also unifying the parameters of this hook

slideDirection: SlideDirection;
}

export interface ExportedSlideTransitionProps {
/**
* Override or extend the styles applied to the component.
Expand All @@ -28,8 +35,12 @@ export interface SlideTransitionProps
transKey: React.Key;
}

const useUtilityClasses = (ownerState: SlideTransitionProps) => {
const { classes, slideDirection } = ownerState;
const useUtilityClasses = (
classes: Partial<PickersSlideTransitionClasses> | undefined,
ownerState: PickerSlideTransitionOwnerState,
) => {
const { slideDirection } = ownerState;

const slots = {
root: ['root'],
exit: ['slideExit'],
Expand Down Expand Up @@ -117,11 +128,13 @@ export function PickersSlideTransition(inProps: SlideTransitionProps) {
reduceAnimations,
slideDirection,
transKey,
// extracting `classes` from `other`
classes: providedClasses,
classes: classesProp,
...other
} = props;
const classes = useUtilityClasses(props);

const { ownerState: pickerOwnerState } = usePickerPrivateContext();
const ownerState = { ...pickerOwnerState, slideDirection };
const classes = useUtilityClasses(classesProp, ownerState);
Comment on lines +135 to +137
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very small performance thing: would it make sense here to prevent creating 2 additional objects?

Suggested change
const { ownerState: pickerOwnerState } = usePickerPrivateContext();
const ownerState = { ...pickerOwnerState, slideDirection };
const classes = useUtilityClasses(classesProp, ownerState);
const classes = useUtilityClasses(classesProp, {
...usePickerPrivateContext().ownerState,
slideDirection,
});

Copy link
Member Author

@flaviendelangle flaviendelangle Nov 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me we have the exact same amount of objects created with both approaches.
We only skip the creation of the pickerOwnerState and ownerState` variables (which are reference based since it's an object so for me it's negligeable)

const theme = useTheme();
if (reduceAnimations) {
return <div className={clsx(classes.root, className)}>{children}</div>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ export interface DayCalendarSkeletonProps extends HTMLDivProps {
ref?: React.Ref<HTMLDivElement>;
}

const useUtilityClasses = (ownerState: DayCalendarSkeletonProps) => {
const { classes } = ownerState;
const useUtilityClasses = (classes: Partial<DayCalendarSkeletonClasses> | undefined) => {
const slots = {
root: ['root'],
week: ['week'],
Expand Down Expand Up @@ -59,14 +58,11 @@ const DayCalendarSkeletonDay = styled(Skeleton, {
name: 'MuiDayCalendarSkeleton',
slot: 'DaySkeleton',
overridesResolver: (props, styles) => styles.daySkeleton,
})<{ ownerState: { day: number } }>({
})({
margin: `0 ${DAY_MARGIN}px`,
variants: [
{
props: { day: 0 },
style: { visibility: 'hidden' },
},
],
'&[data-day-in-month="0"]': {
visibility: 'hidden',
},
});

const monthMap = [
Expand All @@ -92,22 +88,22 @@ function DayCalendarSkeleton(inProps: DayCalendarSkeletonProps) {
name: 'MuiDayCalendarSkeleton',
});

const { className, ...other } = props;
const { className, classes: classesProp, ...other } = props;

const classes = useUtilityClasses(other);
const classes = useUtilityClasses(classesProp);

return (
<DayCalendarSkeletonRoot className={clsx(classes.root, className)} {...other}>
{monthMap.map((week, index) => (
<DayCalendarSkeletonWeek key={index} className={classes.week}>
{week.map((day, index2) => (
{week.map((dayInMonth, index2) => (
<DayCalendarSkeletonDay
key={index2}
variant="circular"
width={DAY_SIZE}
height={DAY_SIZE}
className={classes.daySkeleton}
ownerState={{ day }}
data-day-in-month={dayInMonth}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can create a DayCalendarSkeletonOwnerState but the component is probably not used a lot and even less overriden with styleOverrides so I went with simplicity here

/>
))}
</DayCalendarSkeletonWeek>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ import {
} from '../internals/hooks/date-helpers-hooks';
import {
getPickersCalendarHeaderUtilityClass,
PickersCalendarHeaderClasses,
pickersCalendarHeaderClasses,
} from './pickersCalendarHeaderClasses';
import {
PickersCalendarHeaderOwnerState,
PickersCalendarHeaderProps,
} from './PickersCalendarHeader.types';
import { PickersCalendarHeaderProps } from './PickersCalendarHeader.types';
import { PickerOwnerState } from '../models/pickers';
import { usePickerPrivateContext } from '../internals/hooks/usePickerPrivateContext';

const useUtilityClasses = (ownerState: PickersCalendarHeaderOwnerState) => {
const { classes } = ownerState;
const useUtilityClasses = (classes: Partial<PickersCalendarHeaderClasses> | undefined) => {
const slots = {
root: ['root'],
labelContainer: ['labelContainer'],
Expand All @@ -43,7 +42,7 @@ const PickersCalendarHeaderRoot = styled('div', {
slot: 'Root',
overridesResolver: (_, styles) => styles.root,
})<{
ownerState: PickersCalendarHeaderOwnerState;
ownerState: PickerOwnerState;
}>({
display: 'flex',
alignItems: 'center',
Expand All @@ -61,7 +60,7 @@ const PickersCalendarHeaderLabelContainer = styled('div', {
slot: 'LabelContainer',
overridesResolver: (_, styles) => styles.labelContainer,
})<{
ownerState: PickersCalendarHeaderOwnerState;
ownerState: PickerOwnerState;
}>(({ theme }) => ({
display: 'flex',
overflow: 'hidden',
Expand All @@ -77,7 +76,7 @@ const PickersCalendarHeaderLabel = styled('div', {
slot: 'Label',
overridesResolver: (_, styles) => styles.label,
})<{
ownerState: PickersCalendarHeaderOwnerState;
ownerState: PickerOwnerState;
}>({
marginRight: 6,
});
Expand All @@ -87,7 +86,7 @@ const PickersCalendarHeaderSwitchViewButton = styled(IconButton, {
slot: 'SwitchViewButton',
overridesResolver: (_, styles) => styles.switchViewButton,
})<{
ownerState: PickersCalendarHeaderOwnerState;
ownerState: PickerOwnerState;
}>({
marginRight: 'auto',
variants: [
Expand All @@ -107,7 +106,7 @@ const PickersCalendarHeaderSwitchViewIcon = styled(ArrowDropDownIcon, {
slot: 'SwitchViewIcon',
overridesResolver: (_, styles) => styles.switchViewIcon,
})<{
ownerState: PickersCalendarHeaderOwnerState;
ownerState: PickerOwnerState;
}>(({ theme }) => ({
willChange: 'transform',
transition: theme.transitions.create('transform'),
Expand Down Expand Up @@ -154,14 +153,14 @@ const PickersCalendarHeader = React.forwardRef(function PickersCalendarHeader(
views,
labelId,
className,
classes: classesProp,
timezone,
format = `${utils.formats.month} ${utils.formats.year}`,
...other
} = props;

const ownerState = props;

const classes = useUtilityClasses(props);
const { ownerState } = usePickerPrivateContext();
const classes = useUtilityClasses(classesProp);

const SwitchViewButton = slots?.switchViewButton ?? PickersCalendarHeaderSwitchViewButton;
const switchViewButtonProps = useSlotProps({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
PickersArrowSwitcherSlotProps,
} from '../internals/components/PickersArrowSwitcher';
import { MonthValidationOptions } from '../internals/hooks/date-helpers-hooks';
import { PickerValidDate, DateView } from '../models';
import { PickerValidDate, DateView, PickerOwnerState } from '../models';
import { SlideDirection } from '../DateCalendar/PickersSlideTransition';
import { PickersCalendarHeaderClasses } from './pickersCalendarHeaderClasses';

Expand All @@ -29,19 +29,17 @@ export interface PickersCalendarHeaderSlots extends PickersArrowSwitcherSlots {
// We keep the interface to allow module augmentation
export interface PickersCalendarHeaderSlotPropsOverrides {}

export type PickersCalendarHeaderOwnerState = PickersCalendarHeaderProps;

export interface PickersCalendarHeaderSlotProps extends PickersArrowSwitcherSlotProps {
switchViewButton?: SlotComponentProps<
typeof IconButton,
PickersCalendarHeaderSlotPropsOverrides,
PickersCalendarHeaderOwnerState
PickerOwnerState
>;

switchViewIcon?: SlotComponentProps<
typeof SvgIcon,
PickersCalendarHeaderSlotPropsOverrides,
PickersCalendarHeaderOwnerState
PickerOwnerState
>;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ describe('<PickersCalendarHeader /> - Describes', () => {
render,
muiName: 'MuiPickersCalendarHeader',
refInstanceof: window.HTMLDivElement,
skip: ['componentProp', 'componentsProp', 'themeVariants'],
skip: [
'componentProp',
'componentsProp',
'themeVariants',
'slotPropsCallbackWithPropsAsOwnerState',
],
slots: {
switchViewButton: {
expectedClassName: pickersCalendarHeaderClasses.switchViewButton,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ import composeClasses from '@mui/utils/composeClasses';
import useSlotProps from '@mui/utils/useSlotProps';
import IconButton from '@mui/material/IconButton';
import { ArrowLeftIcon, ArrowRightIcon } from '../../../icons';
import { PickersArrowSwitcherProps } from './PickersArrowSwitcher.types';
import {
PickersArrowSwitcherOwnerState,
PickersArrowSwitcherProps,
} from './PickersArrowSwitcher.types';
import { getPickersArrowSwitcherUtilityClass } from './pickersArrowSwitcherClasses';
getPickersArrowSwitcherUtilityClass,
PickersArrowSwitcherClasses,
} from './pickersArrowSwitcherClasses';
import { usePickerPrivateContext } from '../../hooks/usePickerPrivateContext';
import { PickerOwnerState } from '../../../models';

const PickersArrowSwitcherRoot = styled('div', {
name: 'MuiPickersArrowSwitcher',
slot: 'Root',
overridesResolver: (props, styles) => styles.root,
})<{
ownerState: PickersArrowSwitcherProps;
ownerState: PickerOwnerState;
}>({
display: 'flex',
});
Expand All @@ -28,7 +30,7 @@ const PickersArrowSwitcherSpacer = styled('div', {
slot: 'Spacer',
overridesResolver: (props, styles) => styles.spacer,
})<{
ownerState: PickersArrowSwitcherProps;
ownerState: PickerOwnerState;
}>(({ theme }) => ({
width: theme.spacing(3),
}));
Expand All @@ -38,7 +40,7 @@ const PickersArrowSwitcherButton = styled(IconButton, {
slot: 'Button',
overridesResolver: (props, styles) => styles.button,
})<{
ownerState: PickersArrowSwitcherProps;
ownerState: PickerOwnerState;
}>({
variants: [
{
Expand All @@ -48,8 +50,7 @@ const PickersArrowSwitcherButton = styled(IconButton, {
],
});

const useUtilityClasses = (ownerState: PickersArrowSwitcherOwnerState) => {
const { classes } = ownerState;
const useUtilityClasses = (classes: Partial<PickersArrowSwitcherClasses> | undefined) => {
const slots = {
root: ['root'],
spacer: ['spacer'],
Expand Down Expand Up @@ -84,12 +85,13 @@ export const PickersArrowSwitcher = React.forwardRef(function PickersArrowSwitch
onGoToPrevious,
previousLabel,
labelId,
classes: classesProp,
...other
} = props;

const ownerState = props;
const { ownerState } = usePickerPrivateContext();

const classes = useUtilityClasses(ownerState);
const classes = useUtilityClasses(classesProp);

const nextProps = {
isDisabled: isNextDisabled,
Expand Down Expand Up @@ -117,7 +119,7 @@ export const PickersArrowSwitcher = React.forwardRef(function PickersArrowSwitch
edge: 'end',
onClick: previousProps.goTo,
},
ownerState: { ...ownerState, hidden: previousProps.isHidden },
ownerState: { ...ownerState, hidden: previousProps.isHidden ?? false },
className: clsx(classes.button, classes.previousIconButton),
});

Expand All @@ -133,7 +135,7 @@ export const PickersArrowSwitcher = React.forwardRef(function PickersArrowSwitch
edge: 'start',
onClick: nextProps.goTo,
},
ownerState: { ...ownerState, hidden: nextProps.isHidden },
ownerState: { ...ownerState, hidden: nextProps.isHidden ?? false },
className: clsx(classes.button, classes.nextIconButton),
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { SlotComponentProps } from '@mui/utils';
import IconButton from '@mui/material/IconButton';
import SvgIcon from '@mui/material/SvgIcon';
import { PickersArrowSwitcherClasses } from './pickersArrowSwitcherClasses';
import { PickerOwnerState } from '../../../models';

export interface ExportedPickersArrowSwitcherProps {
/**
Expand Down Expand Up @@ -41,7 +42,12 @@ export interface PickersArrowSwitcherProps
labelId?: string;
}

export type PickersArrowSwitcherOwnerState = PickersArrowSwitcherProps;
export interface PickersArrowSwitcherOwnerState extends PickerOwnerState {
/**
* If `true`, this button should be hidden.
*/
hidden: boolean;
}

export interface PickersArrowSwitcherSlotPropsOverrides {}

Expand All @@ -68,29 +74,25 @@ export interface PickersArrowSwitcherSlots {
rightArrowIcon?: React.ElementType;
}

export interface PickersArrowSwitcherButtonSlotOwnerState extends PickersArrowSwitcherOwnerState {
hidden?: boolean;
}

export interface PickersArrowSwitcherSlotProps {
previousIconButton?: SlotComponentProps<
typeof IconButton,
PickersArrowSwitcherSlotPropsOverrides,
PickersArrowSwitcherButtonSlotOwnerState
PickersArrowSwitcherOwnerState
>;
nextIconButton?: SlotComponentProps<
typeof IconButton,
PickersArrowSwitcherSlotPropsOverrides,
PickersArrowSwitcherButtonSlotOwnerState
PickersArrowSwitcherOwnerState
>;
leftArrowIcon?: SlotComponentProps<
typeof SvgIcon,
PickersArrowSwitcherSlotPropsOverrides,
PickersArrowSwitcherButtonSlotOwnerState
PickerOwnerState
>;
rightArrowIcon?: SlotComponentProps<
typeof SvgIcon,
PickersArrowSwitcherSlotPropsOverrides,
PickersArrowSwitcherButtonSlotOwnerState
PickerOwnerState
>;
}
Loading