1 | import React from 'react';
|
2 | import PropTypes from 'prop-types';
|
3 | import classNames from 'classnames';
|
4 | import { mapToCssModules, tagPropType } from './utils';
|
5 |
|
6 | const propTypes = {
|
7 | tag: tagPropType,
|
8 | icon: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
|
9 | wrapTag: tagPropType,
|
10 | toggle: PropTypes.func,
|
11 | className: PropTypes.string,
|
12 | cssModule: PropTypes.object,
|
13 | children: PropTypes.node,
|
14 | closeAriaLabel: PropTypes.string,
|
15 | charCode: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
16 | close: PropTypes.object,
|
17 | tagClassName: PropTypes.string,
|
18 | };
|
19 |
|
20 | function ToastHeader(props) {
|
21 | let closeButton;
|
22 | let icon;
|
23 | const {
|
24 | className,
|
25 | cssModule,
|
26 | children,
|
27 | toggle,
|
28 | tag: Tag = 'strong',
|
29 | wrapTag: WrapTag = 'div',
|
30 | closeAriaLabel = 'Close',
|
31 | close,
|
32 | tagClassName = 'me-auto',
|
33 | icon: iconProp,
|
34 | ...attributes
|
35 | } = props;
|
36 |
|
37 | const classes = mapToCssModules(
|
38 | classNames(className, 'toast-header'),
|
39 | cssModule,
|
40 | );
|
41 |
|
42 | if (!close && toggle) {
|
43 | closeButton = (
|
44 | <button
|
45 | type="button"
|
46 | onClick={toggle}
|
47 | className={mapToCssModules('btn-close', cssModule)}
|
48 | aria-label={closeAriaLabel}
|
49 | />
|
50 | );
|
51 | }
|
52 |
|
53 | if (typeof iconProp === 'string') {
|
54 | icon = (
|
55 | <svg
|
56 | className={mapToCssModules(`rounded text-${iconProp}`)}
|
57 | width="20"
|
58 | height="20"
|
59 | xmlns="http://www.w3.org/2000/svg"
|
60 | preserveAspectRatio="xMidYMid slice"
|
61 | focusable="false"
|
62 | role="img"
|
63 | >
|
64 | <rect fill="currentColor" width="100%" height="100%" />
|
65 | </svg>
|
66 | );
|
67 | } else if (iconProp) {
|
68 | icon = iconProp;
|
69 | }
|
70 |
|
71 | return (
|
72 | <WrapTag {...attributes} className={classes}>
|
73 | {icon}
|
74 | <Tag
|
75 | className={mapToCssModules(
|
76 | classNames(tagClassName, { 'ms-2': icon != null }),
|
77 | cssModule,
|
78 | )}
|
79 | >
|
80 | {children}
|
81 | </Tag>
|
82 | {close || closeButton}
|
83 | </WrapTag>
|
84 | );
|
85 | }
|
86 |
|
87 | ToastHeader.propTypes = propTypes;
|
88 |
|
89 | export default ToastHeader;
|