-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathMessageStrip.js
141 lines (127 loc) · 4.43 KB
/
MessageStrip.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
import Button from '../Button/Button';
import classnamesBind from 'classnames/bind';
import CustomPropTypes from '../utils/CustomPropTypes/CustomPropTypes';
import Link from '../Link/Link';
import { MESSAGESTRIP_TYPES } from '../utils/constants';
import PropTypes from 'prop-types';
import useUniqueId from '../utils/useUniqueId';
import withStyles from '../utils/withStyles';
import React, { useState } from 'react';
import iconStyles from 'fundamental-styles/dist/icon.css';
import messageStripStyles from 'fundamental-styles/dist/message-strip.css';
const classnames = classnamesBind.bind({
...iconStyles,
...messageStripStyles
});
/** A **MessageStrip** provides a message within the application that is
* color-coded to emphasize the level of urgency. */
const MessageStrip = (props) => {
let [active, setActive] = useState(true);
const {
onCloseClicked,
buttonProps,
type,
link,
linkProps,
linkText,
localizedText,
noGlyph,
dismissible,
children,
className,
cssNamespace,
...otherProps
} = props;
const closeMessageStripHandler = (e) => {
setActive(false);
onCloseClicked(e);
};
const MessageStripClasses = classnames(
`${cssNamespace}-message-strip`,
{
[`${cssNamespace}-message-strip--dismissible`]: dismissible,
[`${cssNamespace}-message-strip--no-icon`]: noGlyph,
[`${cssNamespace}-message-strip--${type}`]: !!type
},
className
);
const generatedAlertId = useUniqueId();
const alertId = otherProps?.id || generatedAlertId;
return (
<>
{active && (
<div
{...otherProps}
className={MessageStripClasses}
id={alertId}
role='alert'>
{dismissible && (
<Button
{...buttonProps}
aria-controls={alertId}
aria-label={localizedText.close}
className={classnames(`${cssNamespace}-message-strip__close`)}
compact
glyph='decline'
onClick={closeMessageStripHandler}
option='transparent' />
)}
<p className={classnames(`${cssNamespace}-message-strip__text`)}>
{children}
{link && (
<Link
{...linkProps}
href={link}>
{linkText}{' '}
</Link>
)}
</p>
</div>
)}
</>
);
};
MessageStrip.displayName = 'MessageStrip';
MessageStrip.propTypes = {
/** Additional props to be spread to the `<button>` element */
buttonProps: PropTypes.object,
/** Node(s) to render within the component */
children: PropTypes.node,
/** CSS class(es) to add to the element */
className: PropTypes.string,
/** Set to **true** to show a dismiss button */
dismissible: PropTypes.bool,
/** Value to be applied to the anchor\'s `href` attribute */
link: PropTypes.string,
/** Additional props to be spread to the link\'s `<a>` element */
linkProps: PropTypes.object,
/** Localized display text of the link */
linkText: PropTypes.string,
/** Localized text to be updated based on location/language */
localizedText: CustomPropTypes.i18n({
/** Value for aria-label on the close <button> element */
close: PropTypes.string
}),
/** Set to **true** to disable the state icon */
noGlyph: PropTypes.bool,
/** Sets the variation of the component. Primarily used for styling:
'warning',
'error',
'success',
'information'*/
type: PropTypes.oneOf(MESSAGESTRIP_TYPES),
/**
* Callback function; triggered when a dismissible MessageStrip's close button is clicked.
*
* @param {SyntheticEvent} event - React's original SyntheticEvent. See https://reactjs.org/docs/events.html.
* @returns {void}
*/
onCloseClicked: PropTypes.func
};
MessageStrip.defaultProps = {
localizedText: {
close: 'Close'
},
onCloseClicked: () => { }
};
export default withStyles(MessageStrip);