-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
/
Copy pathCardHeader.js
275 lines (255 loc) · 7.93 KB
/
CardHeader.js
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import composeClasses from '@mui/utils/composeClasses';
import Typography, { typographyClasses } from '../Typography';
import { styled } from '../zero-styled';
import { useDefaultProps } from '../DefaultPropsProvider';
import cardHeaderClasses, { getCardHeaderUtilityClass } from './cardHeaderClasses';
import useSlot from '../utils/useSlot';
const useUtilityClasses = (ownerState) => {
const { classes } = ownerState;
const slots = {
root: ['root'],
avatar: ['avatar'],
action: ['action'],
content: ['content'],
title: ['title'],
subheader: ['subheader'],
};
return composeClasses(slots, getCardHeaderUtilityClass, classes);
};
const CardHeaderRoot = styled('div', {
name: 'MuiCardHeader',
slot: 'Root',
overridesResolver: (props, styles) => {
return [
{ [`& .${cardHeaderClasses.title}`]: styles.title },
{ [`& .${cardHeaderClasses.subheader}`]: styles.subheader },
styles.root,
];
},
})({
display: 'flex',
alignItems: 'center',
padding: 16,
});
const CardHeaderAvatar = styled('div', {
name: 'MuiCardHeader',
slot: 'Avatar',
overridesResolver: (props, styles) => styles.avatar,
})({
display: 'flex',
flex: '0 0 auto',
marginRight: 16,
});
const CardHeaderAction = styled('div', {
name: 'MuiCardHeader',
slot: 'Action',
overridesResolver: (props, styles) => styles.action,
})({
flex: '0 0 auto',
alignSelf: 'flex-start',
marginTop: -4,
marginRight: -8,
marginBottom: -4,
});
const CardHeaderContent = styled('div', {
name: 'MuiCardHeader',
slot: 'Content',
overridesResolver: (props, styles) => styles.content,
})({
flex: '1 1 auto',
[`.${typographyClasses.root}:where(& .${cardHeaderClasses.title})`]: {
display: 'block',
},
[`.${typographyClasses.root}:where(& .${cardHeaderClasses.subheader})`]: {
display: 'block',
},
});
const CardHeader = React.forwardRef(function CardHeader(inProps, ref) {
const props = useDefaultProps({ props: inProps, name: 'MuiCardHeader' });
const {
action,
avatar,
component = 'div',
disableTypography = false,
subheader: subheaderProp,
subheaderTypographyProps,
title: titleProp,
titleTypographyProps,
slots = {},
slotProps = {},
...other
} = props;
const ownerState = {
...props,
component,
disableTypography,
};
const classes = useUtilityClasses(ownerState);
const externalForwardedProps = {
slots,
slotProps: {
title: titleTypographyProps,
subheader: subheaderTypographyProps,
...slotProps,
},
};
let title = titleProp;
const [TitleSlot, titleSlotProps] = useSlot('title', {
className: classes.title,
elementType: Typography,
externalForwardedProps,
ownerState,
additionalProps: {
variant: avatar ? 'body2' : 'h5',
component: 'span',
},
});
if (title != null && title.type !== Typography && !disableTypography) {
title = <TitleSlot {...titleSlotProps}>{title}</TitleSlot>;
}
let subheader = subheaderProp;
const [SubheaderSlot, subheaderSlotProps] = useSlot('subheader', {
className: classes.subheader,
elementType: Typography,
externalForwardedProps,
ownerState,
additionalProps: {
variant: avatar ? 'body2' : 'body1',
color: 'textSecondary',
component: 'span',
},
});
if (subheader != null && subheader.type !== Typography && !disableTypography) {
subheader = <SubheaderSlot {...subheaderSlotProps}>{subheader}</SubheaderSlot>;
}
const [RootSlot, rootSlotProps] = useSlot('root', {
ref,
className: classes.root,
elementType: CardHeaderRoot,
externalForwardedProps: {
...externalForwardedProps,
...other,
component,
},
ownerState,
});
const [AvatarSlot, avatarSlotProps] = useSlot('avatar', {
className: classes.avatar,
elementType: CardHeaderAvatar,
externalForwardedProps,
ownerState,
});
const [ContentSlot, contentSlotProps] = useSlot('content', {
className: classes.content,
elementType: CardHeaderContent,
externalForwardedProps,
ownerState,
});
const [ActionSlot, actionSlotProps] = useSlot('action', {
className: classes.action,
elementType: CardHeaderAction,
externalForwardedProps,
ownerState,
});
return (
<RootSlot {...rootSlotProps}>
{avatar && <AvatarSlot {...avatarSlotProps}>{avatar}</AvatarSlot>}
<ContentSlot {...contentSlotProps}>
{title}
{subheader}
</ContentSlot>
{action && <ActionSlot {...actionSlotProps}>{action}</ActionSlot>}
</RootSlot>
);
});
CardHeader.propTypes /* remove-proptypes */ = {
// ┌────────────────────────────── Warning ──────────────────────────────┐
// │ These PropTypes are generated from the TypeScript type definitions. │
// │ To update them, edit the d.ts file and run `pnpm proptypes`. │
// └─────────────────────────────────────────────────────────────────────┘
/**
* The action to display in the card header.
*/
action: PropTypes.node,
/**
* The Avatar element to display.
*/
avatar: PropTypes.node,
/**
* @ignore
*/
children: PropTypes.node,
/**
* Override or extend the styles applied to the component.
*/
classes: PropTypes.object,
/**
* The component used for the root node.
* Either a string to use a HTML element or a component.
*/
component: PropTypes.elementType,
/**
* If `true`, `subheader` and `title` won't be wrapped by a Typography component.
* This can be useful to render an alternative Typography variant by wrapping
* the `title` text, and optional `subheader` text
* with the Typography component.
* @default false
*/
disableTypography: PropTypes.bool,
/**
* The props used for each slot inside.
* @default {}
*/
slotProps: PropTypes.shape({
action: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
avatar: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
content: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
root: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
subheader: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
title: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
}),
/**
* The components used for each slot inside.
* @default {}
*/
slots: PropTypes.shape({
action: PropTypes.elementType,
avatar: PropTypes.elementType,
content: PropTypes.elementType,
root: PropTypes.elementType,
subheader: PropTypes.elementType,
title: PropTypes.elementType,
}),
/**
* The content of the component.
*/
subheader: PropTypes.node,
/**
* These props will be forwarded to the subheader
* (as long as disableTypography is not `true`).
* @deprecated Use `slotProps.subheader` instead. This prop will be removed in v7. See [Migrating from deprecated APIs](/material-ui/migration/migrating-from-deprecated-apis/) for more details.
*/
subheaderTypographyProps: PropTypes.object,
/**
* The system prop that allows defining system overrides as well as additional CSS styles.
*/
sx: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])),
PropTypes.func,
PropTypes.object,
]),
/**
* The content of the component.
*/
title: PropTypes.node,
/**
* These props will be forwarded to the title
* (as long as disableTypography is not `true`).
* @deprecated Use `slotProps.title` instead. This prop will be removed in v7. See [Migrating from deprecated APIs](/material-ui/migration/migrating-from-deprecated-apis/) for more details.
*/
titleTypographyProps: PropTypes.object,
};
export default CardHeader;