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

[TimePicker] Add Mui-selected class to TimeClock meridiem buttons #13848

Merged
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
49 changes: 20 additions & 29 deletions packages/x-date-pickers/src/TimeClock/Clock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import clsx from 'clsx';
import IconButton from '@mui/material/IconButton';
import Typography from '@mui/material/Typography';
import { styled, useThemeProps } from '@mui/material/styles';
import { styled, useThemeProps, Theme } from '@mui/material/styles';
import {
unstable_useEnhancedEffect as useEnhancedEffect,
unstable_composeClasses as composeClasses,
Expand All @@ -16,6 +16,7 @@ import { CLOCK_HOUR_WIDTH, getHours, getMinutes } from './shared';
import { PickerValidDate, TimeView } from '../models';
import { ClockClasses, getClockUtilityClass } from './clockClasses';
import { formatMeridiem } from '../internals/utils/date-utils';
import { Meridiem } from '../internals/utils/time-utils';

export interface ClockProps<TDate extends PickerValidDate>
extends ReturnType<typeof useMeridiemMode> {
Expand Down Expand Up @@ -47,15 +48,15 @@ export interface ClockProps<TDate extends PickerValidDate>
}

const useUtilityClasses = (ownerState: ClockProps<any>) => {
const { classes } = ownerState;
const { classes, meridiemMode } = ownerState;
const slots = {
root: ['root'],
clock: ['clock'],
wrapper: ['wrapper'],
squareMask: ['squareMask'],
pin: ['pin'],
amButton: ['amButton'],
pmButton: ['pmButton'],
amButton: ['amButton', meridiemMode === 'am' && 'selected'],
pmButton: ['pmButton', meridiemMode === 'pm' && 'selected'],
meridiemText: ['meridiemText'],
};

Expand Down Expand Up @@ -145,21 +146,15 @@ const ClockPin = styled('div', {
transform: 'translate(-50%, -50%)',
}));

const ClockAmButton = styled(IconButton, {
name: 'MuiClock',
slot: 'AmButton',
overridesResolver: (_, styles) => styles.amButton,
})<{ ownerState: ClockProps<any> }>(({ theme }) => ({
const meridiemButtonCommonStyles = (theme: Theme, meridiemMode: Meridiem) => ({
zIndex: 1,
position: 'absolute',
bottom: 8,
left: 8,
paddingLeft: 4,
paddingRight: 4,
width: CLOCK_HOUR_WIDTH,
variants: [
{
props: { meridiemMode: 'am' },
props: { meridiemMode },
style: {
backgroundColor: (theme.vars || theme).palette.primary.main,
color: (theme.vars || theme).palette.primary.contrastText,
Expand All @@ -169,32 +164,28 @@ const ClockAmButton = styled(IconButton, {
},
},
],
});

const ClockAmButton = styled(IconButton, {
name: 'MuiClock',
slot: 'AmButton',
overridesResolver: (_, styles) => styles.amButton,
})<{ ownerState: ClockProps<any> }>(({ theme }) => ({
...meridiemButtonCommonStyles(theme, 'am'),
// keeping it here to make TS happy
position: 'absolute',
left: 8,
}));

const ClockPmButton = styled(IconButton, {
name: 'MuiClock',
slot: 'PmButton',
overridesResolver: (_, styles) => styles.pmButton,
})<{ ownerState: ClockProps<any> }>(({ theme }) => ({
zIndex: 1,
...meridiemButtonCommonStyles(theme, 'pm'),
// keeping it here to make TS happy
position: 'absolute',
bottom: 8,
right: 8,
paddingLeft: 4,
paddingRight: 4,
width: CLOCK_HOUR_WIDTH,
variants: [
{
props: { meridiemMode: 'pm' },
style: {
backgroundColor: (theme.vars || theme).palette.primary.main,
color: (theme.vars || theme).palette.primary.contrastText,
'&:hover': {
backgroundColor: (theme.vars || theme).palette.primary.light,
},
},
},
],
}));

const ClockMeridiemText = styled(Typography, {
Expand Down
3 changes: 3 additions & 0 deletions packages/x-date-pickers/src/TimeClock/clockClasses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export interface ClockClasses {
pmButton: string;
/** Styles applied to the meridiem typography element. */
meridiemText: string;
/** Styles applied to the selected meridiem button element */
selected: string;
}

export type ClockClassKey = keyof ClockClasses;
Expand All @@ -37,4 +39,5 @@ export const clockClasses: ClockClasses = generateUtilityClasses('MuiClock', [
'amButton',
'pmButton',
'meridiemText',
'selected',
]);