forked from mui/material-ui
/
mergeSlotProps.ts
176 lines (158 loc) · 5.12 KB
/
mergeSlotProps.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import clsx from 'clsx';
import { Simplify } from '@mui/types';
import { EventHandlers } from './types';
import extractEventHandlers from './extractEventHandlers';
import omitEventHandlers, { OmitEventHandlers } from './omitEventHandlers';
export type WithCommonProps<T> = T & {
className?: string;
style?: React.CSSProperties;
ref?: React.Ref<any>;
};
export interface MergeSlotPropsParameters<
SlotProps,
ExternalForwardedProps,
ExternalSlotProps,
AdditionalProps,
> {
/**
* A function that returns the internal props of the component.
* It accepts the event handlers passed into the component by the user
* and is responsible for calling them where appropriate.
*/
getSlotProps?: (other: EventHandlers) => WithCommonProps<SlotProps>;
/**
* Props provided to the `componentsProps.*` of the unstyled component.
*/
externalSlotProps?: WithCommonProps<ExternalSlotProps>;
/**
* Extra props placed on the unstyled component that should be forwarded to the slot.
* This should usually be used only for the root slot.
*/
externalForwardedProps?: WithCommonProps<ExternalForwardedProps>;
/**
* Additional props to be placed on the slot.
*/
additionalProps?: WithCommonProps<AdditionalProps>;
/**
* Extra class name(s) to be placed on the slot.
*/
className?: string | (string | undefined)[] | undefined;
}
export type MergeSlotPropsResult<
SlotProps,
ExternalForwardedProps,
ExternalSlotProps,
AdditionalProps,
> = {
props: Simplify<
SlotProps &
OmitEventHandlers<ExternalForwardedProps> &
OmitEventHandlers<ExternalSlotProps> &
AdditionalProps & { className?: string; style?: React.CSSProperties }
>;
internalRef: React.Ref<any> | undefined;
};
/**
* Merges the slot component internal props (usually coming from a hook)
* with the externally provided ones.
*
* The merge order is (the latter overrides the former):
* 1. The internal props (specified as a getter function to work with get*Props hook result)
* 2. Additional props (specified internally on an unstyled component)
* 3. External props specified on the owner component. These should only be used on a root slot.
* 4. External props specified in the `componentsProps.*` prop.
* 5. The `className` prop - combined from all the above.
* @param parameters
* @returns
*/
export default function mergeSlotProps<
SlotProps,
ExternalForwardedProps extends Record<string, unknown>,
ExternalSlotProps extends Record<string, unknown>,
AdditionalProps,
>(
parameters: MergeSlotPropsParameters<
SlotProps,
ExternalForwardedProps,
ExternalSlotProps,
AdditionalProps
>,
): MergeSlotPropsResult<SlotProps, ExternalForwardedProps, ExternalSlotProps, AdditionalProps> {
const { getSlotProps, additionalProps, externalSlotProps, externalForwardedProps, className } =
parameters;
if (!getSlotProps) {
// The simpler case - getSlotProps is not defined, so no internal event handlers are defined,
// so we can simply merge all the props without having to worry about extracting event handlers.
const joinedClasses = clsx(
externalForwardedProps?.className,
externalSlotProps?.className,
className,
additionalProps?.className,
);
const mergedStyle = {
...additionalProps?.style,
...externalForwardedProps?.style,
...externalSlotProps?.style,
};
const props = {
...additionalProps,
...externalForwardedProps,
...externalSlotProps,
} as MergeSlotPropsResult<
SlotProps,
ExternalForwardedProps,
ExternalSlotProps,
AdditionalProps
>['props'];
if (joinedClasses.length > 0) {
props.className = joinedClasses;
}
if (Object.keys(mergedStyle).length > 0) {
props.style = mergedStyle;
}
return {
props,
internalRef: undefined,
};
}
// In this case, getSlotProps is responsible for calling the external event handlers.
// We don't need to include them in the merged props because of this.
const eventHandlers = extractEventHandlers({ ...externalForwardedProps, ...externalSlotProps });
const componentsPropsWithoutEventHandlers = omitEventHandlers(externalSlotProps);
const otherPropsWithoutEventHandlers = omitEventHandlers(externalForwardedProps);
const internalSlotProps = getSlotProps(eventHandlers);
const joinedClasses = clsx(
externalForwardedProps?.className,
externalSlotProps?.className,
className,
additionalProps?.className,
internalSlotProps?.className,
);
const mergedStyle = {
...internalSlotProps?.style,
...additionalProps?.style,
...externalForwardedProps?.style,
...externalSlotProps?.style,
};
const props = {
...internalSlotProps,
...additionalProps,
...otherPropsWithoutEventHandlers,
...componentsPropsWithoutEventHandlers,
} as MergeSlotPropsResult<
SlotProps,
ExternalForwardedProps,
ExternalSlotProps,
AdditionalProps
>['props'];
if (joinedClasses.length > 0) {
props.className = joinedClasses;
}
if (Object.keys(mergedStyle).length > 0) {
props.style = mergedStyle;
}
return {
props,
internalRef: internalSlotProps.ref,
};
}