|
1 | 1 | import PropTypes from 'prop-types'; |
2 | 2 | import React from 'react'; |
| 3 | +import { withTranslationContext } from '../../../translation'; |
3 | 4 | import styles from './Alert.scss'; |
4 | 5 |
|
5 | 6 | const Alert = (props) => { |
6 | | - let rootTypeClass = styles.note; |
7 | | - |
8 | | - if (props.type) { |
9 | | - if (props.type === 'success') { |
10 | | - rootTypeClass = styles.success; |
11 | | - } else if (props.type === 'error') { |
12 | | - rootTypeClass = styles.error; |
13 | | - } else if (props.type === 'warning') { |
14 | | - rootTypeClass = styles.warning; |
15 | | - } else if (props.type === 'info') { |
16 | | - rootTypeClass = styles.info; |
17 | | - } else if (props.type === 'help') { |
18 | | - rootTypeClass = styles.help; |
| 7 | + const { |
| 8 | + children, |
| 9 | + closeHandler, |
| 10 | + icon, |
| 11 | + id, |
| 12 | + translations, |
| 13 | + type, |
| 14 | + } = props; |
| 15 | + |
| 16 | + const rootTypeClass = (variant) => { |
| 17 | + if (variant === 'success') { |
| 18 | + return styles.isRootSuccess; |
| 19 | + } |
| 20 | + |
| 21 | + if (variant === 'error') { |
| 22 | + return styles.isRootError; |
19 | 23 | } |
20 | | - } |
| 24 | + |
| 25 | + if (variant === 'warning') { |
| 26 | + return styles.isRootWarning; |
| 27 | + } |
| 28 | + |
| 29 | + if (variant === 'info') { |
| 30 | + return styles.isRootInfo; |
| 31 | + } |
| 32 | + |
| 33 | + if (variant === 'help') { |
| 34 | + return styles.isRootHelp; |
| 35 | + } |
| 36 | + |
| 37 | + return styles.isRootNote; |
| 38 | + }; |
21 | 39 |
|
22 | 40 | return ( |
23 | 41 | <div |
24 | | - className={(` |
25 | | - ${styles.root} |
26 | | - ${rootTypeClass} |
27 | | - `).trim()} |
28 | | - id={props.id} |
| 42 | + className={[ |
| 43 | + styles.root, |
| 44 | + rootTypeClass(type), |
| 45 | + ].join(' ')} |
| 46 | + id={id} |
29 | 47 | role="alert" |
30 | 48 | > |
31 | | - {props.icon && ( |
| 49 | + {icon && ( |
32 | 50 | <div className={styles.icon}> |
33 | | - {props.icon} |
| 51 | + {icon} |
34 | 52 | </div> |
35 | 53 | )} |
36 | 54 | <div |
37 | 55 | className={styles.message} |
38 | | - {...(props.id && { id: `${props.id}__content` })} |
| 56 | + {...(id && { id: `${id}__content` })} |
39 | 57 | > |
40 | | - {props.children} |
| 58 | + {children} |
41 | 59 | </div> |
| 60 | + {closeHandler && ( |
| 61 | + <button |
| 62 | + type="button" |
| 63 | + {...(id && { id: `${id}__close` })} |
| 64 | + className={styles.close} |
| 65 | + onClick={() => closeHandler()} |
| 66 | + onKeyPress={() => closeHandler()} |
| 67 | + tabIndex="0" |
| 68 | + title={translations.close} |
| 69 | + > |
| 70 | + <span className={styles.closeSign}>×</span> |
| 71 | + </button> |
| 72 | + )} |
42 | 73 | </div> |
43 | 74 | ); |
44 | 75 | }; |
45 | 76 |
|
46 | 77 | Alert.defaultProps = { |
| 78 | + closeHandler: null, |
47 | 79 | icon: null, |
48 | 80 | id: undefined, |
49 | 81 | type: 'note', |
50 | 82 | }; |
51 | 83 |
|
52 | 84 | Alert.propTypes = { |
53 | 85 | children: PropTypes.node.isRequired, |
| 86 | + closeHandler: PropTypes.func, |
54 | 87 | icon: PropTypes.node, |
55 | 88 | id: PropTypes.string, |
| 89 | + translations: PropTypes.shape({ |
| 90 | + close: PropTypes.string.isRequired, |
| 91 | + }).isRequired, |
56 | 92 | type: PropTypes.oneOf(['error', 'help', 'info', 'note', 'success', 'warning']), |
57 | 93 | }; |
58 | 94 |
|
59 | | -export default Alert; |
| 95 | +export default withTranslationContext(Alert, 'Alert'); |
0 commit comments