UNPKG

2.14 kBTypeScriptView Raw
1import * as React from 'react';
2import { DistributiveOmit } from '@mui/types';
3import { StyledComponentProps } from './styles';
4
5/**
6 * A component whose root component can be controlled via a `component` prop.
7 *
8 * Adjusts valid props based on the type of `component`.
9 */
10export interface OverridableComponent<TypeMap extends OverridableTypeMap> {
11 // If you make any changes to this interface, please make sure to update the
12 // `OverridableComponent` type in `mui-types/index.d.ts` as well.
13 // Also, there are types in Base UI that have a similar shape to this interface
14 // (for example SelectType, OptionType, etc.).
15 <RootComponent extends React.ElementType>(
16 props: {
17 /**
18 * The component used for the root node.
19 * Either a string to use a HTML element or a component.
20 */
21 component: RootComponent;
22 } & OverrideProps<TypeMap, RootComponent>,
23 ): React.JSX.Element | null;
24 (props: DefaultComponentProps<TypeMap>): React.JSX.Element | null;
25}
26
27/**
28 * Props of the component if `component={Component}` is used.
29 */
30// prettier-ignore
31export type OverrideProps<
32 TypeMap extends OverridableTypeMap,
33 RootComponent extends React.ElementType
34> = (
35 & BaseProps<TypeMap>
36 & DistributiveOmit<React.ComponentPropsWithRef<RootComponent>, keyof BaseProps<TypeMap>>
37);
38
39/**
40 * Props if `component={Component}` is NOT used.
41 */
42// prettier-ignore
43export type DefaultComponentProps<TypeMap extends OverridableTypeMap> =
44 & BaseProps<TypeMap>
45 & DistributiveOmit<React.ComponentPropsWithRef<TypeMap['defaultComponent']>, keyof BaseProps<TypeMap>>;
46
47/**
48 * Props defined on the component (+ common material-ui props).
49 */
50// prettier-ignore
51export type BaseProps<TypeMap extends OverridableTypeMap> =
52 & TypeMap['props']
53 & CommonProps;
54
55/**
56 * Props that are valid for material-ui components.
57 */
58// each component declares it's classes in a separate interface for proper JSDoc.
59export interface CommonProps extends StyledComponentProps<never> {
60 className?: string;
61 style?: React.CSSProperties;
62}
63
64export interface OverridableTypeMap {
65 props: {};
66 defaultComponent: React.ElementType;
67}