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

Feature theme augmentation #2092

Closed
wants to merge 4 commits into from
Closed
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
21 changes: 21 additions & 0 deletions docs/pages/guides/typescript.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,24 @@ In this case it is required to manually set proper [generic type manually](#gene
onChange={newValue => newValue?.getDate()}
/>
```

#### Theme augmentation

In order to benefit from the [CSS overrides](https://material-ui.com/customization/globals/#css) and
[default prop customization](https://material-ui.com/customization/globals/#default-props) with the theme,
TypeScript users need to import the following types. Internally, it uses [module augmentation](https://material-ui.com/guides/typescript/#customization-of-theme)
to extend the default theme structure with the extension components available in the pickers.

```tsx
import '@material-ui/pickers/themeAugmentation';

const theme = createMuiTheme({
overrides: {
MuiPickersDatePicker: {
root: {
backgroundColor: 'red',
},
},
},
});
```
8 changes: 4 additions & 4 deletions lib/src/DatePicker/DatePicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type DatePickerComponent<TWrapper extends SomeWrapper> = <TDate>(
export const DatePicker = makePickerWithStateAndWrapper<BaseDatePickerProps<unknown>>(
ResponsiveWrapper,
{
name: 'MuiDatePicker',
name: 'MuiPickersDatePicker',
...datePickerConfig,
}
) as DatePickerComponent<typeof ResponsiveWrapper>;
Expand All @@ -67,7 +67,7 @@ export type DatePickerProps = React.ComponentProps<typeof DatePicker>;
export const MobileDatePicker = makePickerWithStateAndWrapper<BaseDatePickerProps<unknown>>(
MobileWrapper,
{
name: 'MuiMobileDatePicker',
name: 'MuiPickersMobileDatePicker',
...datePickerConfig,
}
) as DatePickerComponent<typeof MobileWrapper>;
Expand All @@ -77,7 +77,7 @@ export type MobileDatePickerProps = React.ComponentProps<typeof MobileDatePicker
export const DesktopDatePicker = makePickerWithStateAndWrapper<BaseDatePickerProps<unknown>>(
DesktopWrapper,
{
name: 'MuiDesktopDatePicker',
name: 'MuiPickersDesktopDatePicker',
...datePickerConfig,
}
);
Expand All @@ -87,7 +87,7 @@ export type DesktopDatePickerProps = React.ComponentProps<typeof DesktopDatePick
export const StaticDatePicker = makePickerWithStateAndWrapper<BaseDatePickerProps<unknown>>(
StaticWrapper,
{
name: 'MuiStaticDatePicker',
name: 'MuiPickersStaticDatePicker',
...datePickerConfig,
}
) as DatePickerComponent<typeof StaticWrapper>;
Expand Down
12 changes: 9 additions & 3 deletions lib/src/DateRangePicker/DateRangePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,17 +170,23 @@ export const DateRangePicker = makeRangePicker(
export type DateRangePickerProps = React.ComponentProps<typeof DateRangePicker>;

export const DesktopDateRangePicker = makeRangePicker(
'MuiDesktopDateRangePicker',
'MuiPickersDesktopDateRangePicker',
DesktopTooltipWrapper
);

export type DesktopDateRangePickerProps = React.ComponentProps<typeof DesktopDateRangePicker>;

export const MobileDateRangePicker = makeRangePicker('MuiMobileDateRangePicker', MobileWrapper);
export const MobileDateRangePicker = makeRangePicker(
'MuiPickersMobileDateRangePicker',
MobileWrapper
);

export type MobileDateRangePickerProps = React.ComponentProps<typeof MobileDateRangePicker>;

export const StaticDateRangePicker = makeRangePicker('MuiStaticDateRangePicker', StaticWrapper);
export const StaticDateRangePicker = makeRangePicker(
'MuiPickersStaticDateRangePicker',
StaticWrapper
);

export type StaticDateRangePickerProps = React.ComponentProps<typeof StaticDateRangePicker>;

Expand Down
8 changes: 4 additions & 4 deletions lib/src/DateTimePicker/DateTimePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ type DateTimePickerComponent<TWrapper extends SomeWrapper> = <TDate>(
export const DateTimePicker = makePickerWithStateAndWrapper<BaseDateTimePickerProps<unknown>>(
ResponsiveWrapper,
{
name: 'MuiDateTimePicker',
name: 'MuiPickersDateTimePicker',
...dateTimePickerConfig,
}
) as DateTimePickerComponent<typeof ResponsiveWrapper>;
Expand All @@ -132,7 +132,7 @@ export type DateTimePickerProps = React.ComponentProps<typeof DateTimePicker>;
export const DesktopDateTimePicker = makePickerWithStateAndWrapper<
BaseDateTimePickerProps<unknown>
>(DesktopWrapper, {
name: 'MuiDesktopDateTimePicker',
name: 'MuiPickersDesktopDateTimePicker',
...dateTimePickerConfig,
}) as DateTimePickerComponent<typeof DesktopWrapper>;

Expand All @@ -141,7 +141,7 @@ export type DesktopDateTimePickerProps = React.ComponentProps<typeof DesktopDate
export const MobileDateTimePicker = makePickerWithStateAndWrapper<BaseDateTimePickerProps<unknown>>(
MobileWrapper,
{
name: 'MuiMobileDateTimePicker',
name: 'MuiPickersMobileDateTimePicker',
...dateTimePickerConfig,
}
) as DateTimePickerComponent<typeof MobileWrapper>;
Expand All @@ -151,7 +151,7 @@ export type MobileDateTimePickerProps = React.ComponentProps<typeof MobileDateTi
export const StaticDateTimePicker = makePickerWithStateAndWrapper<BaseDateTimePickerProps<unknown>>(
StaticWrapper,
{
name: 'MuiStaticDateTimePicker',
name: 'MuiPickersStaticDateTimePicker',
...dateTimePickerConfig,
}
) as DateTimePickerComponent<typeof StaticWrapper>;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/DateTimePicker/DateTimePickerTabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const useStyles = makeStyles(
},
};
},
{ name: 'MuiDateTimePickerTabs' }
{ name: 'MuiPickersDateTimePickerTabs' }
);

const DateTimePickerTabs: React.FC<DateTimePickerTabsProps> = (props) => {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/DateTimePicker/DateTimePickerToolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { withDefaultProps } from '../_shared/withDefaultProps';
import { ToolbarComponentProps } from '../Picker/SharedPickerProps';
import { WrapperVariantContext } from '../wrappers/WrapperVariantContext';

const muiComponentConfig = { name: 'MuiDateTimePickerToolbar' };
const muiComponentConfig = { name: 'MuiPickersDateTimePickerToolbar' };

export const useStyles = makeStyles(
{
Expand Down
8 changes: 4 additions & 4 deletions lib/src/TimePicker/TimePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ type TimePickerComponent<TWrapper extends SomeWrapper> = <TDate>(
) => JSX.Element;

export const TimePicker = makePickerWithStateAndWrapper<BaseTimePickerProps>(ResponsiveWrapper, {
name: 'MuiTimePicker',
name: 'MuiPickersTimePicker',
...timePickerConfig,
}) as TimePickerComponent<typeof ResponsiveWrapper>;

Expand All @@ -83,22 +83,22 @@ export type TimePickerProps = React.ComponentProps<typeof TimePicker>;
export const DesktopTimePicker = makePickerWithStateAndWrapper<BaseTimePickerProps>(
DesktopWrapper,
{
name: 'MuiDesktopTimePicker',
name: 'MuiPickersDesktopTimePicker',
...timePickerConfig,
}
) as TimePickerComponent<typeof DesktopWrapper>;

export type DesktopTimePickerProps = React.ComponentProps<typeof DesktopTimePicker>;

export const MobileTimePicker = makePickerWithStateAndWrapper<BaseTimePickerProps>(MobileWrapper, {
name: 'MuiMobileTimePicker',
name: 'MuiPickersMobileTimePicker',
...timePickerConfig,
}) as TimePickerComponent<typeof MobileWrapper>;

export type MobileTimePickerProps = React.ComponentProps<typeof MobileTimePicker>;

export const StaticTimePicker = makePickerWithStateAndWrapper<BaseTimePickerProps>(StaticWrapper, {
name: 'MuiStaticTimePicker',
name: 'MuiPickersStaticTimePicker',
...timePickerConfig,
});

Expand Down
10 changes: 1 addition & 9 deletions lib/src/__tests__/typings.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1 @@
import { MuiPickersComponentsToClassName, MuiPickersComponentsPropsList } from '../typings';

declare module '@material-ui/core/styles/overrides' {
export interface ComponentNameToClassKey extends MuiPickersComponentsToClassName {}
}

declare module '@material-ui/core/styles/props' {
export interface ComponentsPropsList extends MuiPickersComponentsPropsList {}
}
import '../themeAugmentation';
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ export interface MuiPickersComponentsToClassName {
MuiPickersDatePickerRoot: Classes<typeof DatePickerRootStyles>;
MuiPickersBasePicker: Classes<typeof MuiBasePickerStyles>;
MuiPickersModalDialog: Classes<typeof import('../_shared/PickersModalDialog').useStyles>;
MuiDateTimePickerTabs: Classes<typeof import('../DateTimePicker/DateTimePickerTabs').useStyles>;
MuiDateTimePickerToolbar: Classes<
MuiPickersDateTimePickerTabs: Classes<
typeof import('../DateTimePicker/DateTimePickerTabs').useStyles
>;
MuiPickersDateTimePickerToolbar: Classes<
typeof import('../DateTimePicker/DateTimePickerToolbar').useStyles
>;
// consider using inline import type notation
Expand All @@ -66,3 +68,10 @@ export interface MuiPickersComponentsToClassName {
MuiPickersCalendarSkeleton: Classes<typeof import('../CalendarSkeleton').useStyles>;
MuiPickersPopper: Classes<typeof import('../_shared/PickersPopper').useStyles>;
}

declare module '@material-ui/core/styles/overrides' {
interface ComponentNameToClassKey extends MuiPickersComponentsToClassName {}
}

// disable automatic export
export {};
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,10 @@ export interface MuiPickersComponentsPropsList {
MuiPickersDateRangeDelimiter: DateRangeDelimiterProps;
MuiPickersCalendarSkeleton: CalendarSkeletonProps;
}

declare module '@material-ui/core/styles/props' {
interface ComponentsPropsList extends MuiPickersComponentsPropsList {}
}

// disable automatic export
export {};