Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ModalHeader.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // @flow
  2. /* eslint-disable react/no-multi-comp */
  3. import ErrorIcon from '@atlaskit/icon/glyph/error';
  4. import WarningIcon from '@atlaskit/icon/glyph/warning';
  5. import {
  6. Header,
  7. Title,
  8. titleIconWrapperStyles,
  9. TitleText
  10. } from '@atlaskit/modal-dialog/dist/es2019/styled/Content';
  11. import { withStyles } from '@material-ui/core/styles';
  12. import React from 'react';
  13. import { translate } from '../../../i18n';
  14. import { Icon, IconClose } from '../../../icons';
  15. import { withPixelLineHeight } from '../../../styles/functions';
  16. const TitleIcon = ({ appearance }: { appearance?: 'danger' | 'warning' }) => {
  17. if (!appearance) {
  18. return null;
  19. }
  20. const IconSymbol = appearance === 'danger' ? ErrorIcon : WarningIcon;
  21. return (
  22. <span css = { titleIconWrapperStyles(appearance) }>
  23. <IconSymbol label = { `${appearance} icon` } />
  24. </span>
  25. );
  26. };
  27. type Props = {
  28. id: string,
  29. appearance?: 'danger' | 'warning',
  30. classes: Object,
  31. heading: string,
  32. hideCloseIconButton: boolean,
  33. onClose: Function,
  34. showKeyline: boolean,
  35. isHeadingMultiline: boolean,
  36. testId: string,
  37. t: Function
  38. }
  39. /**
  40. * Creates the styles for the component.
  41. *
  42. * @param {Object} theme - The current UI theme.
  43. *
  44. * @returns {Object}
  45. */
  46. const styles = theme => {
  47. return {
  48. closeButton: {
  49. borderRadius: theme.shape.borderRadius,
  50. cursor: 'pointer',
  51. padding: 13,
  52. [theme.breakpoints.down('480')]: {
  53. background: theme.palette.action02
  54. },
  55. '&:hover': {
  56. background: theme.palette.action02
  57. }
  58. },
  59. header: {
  60. boxShadow: 'none',
  61. '& h4': {
  62. ...withPixelLineHeight(theme.typography.heading5),
  63. color: theme.palette.text01
  64. }
  65. }
  66. };
  67. };
  68. /**
  69. * A default header for modal-dialog components.
  70. *
  71. * @class ModalHeader
  72. * @augments {React.Component<Props>}
  73. */
  74. class ModalHeader extends React.Component<Props> {
  75. static defaultProps = {
  76. isHeadingMultiline: true
  77. };
  78. /**
  79. * Initializes a new {@code ModalHeader} instance.
  80. *
  81. * @param {*} props - The read-only properties with which the new instance
  82. * is to be initialized.
  83. */
  84. constructor(props) {
  85. super(props);
  86. // Bind event handler so it is only bound once for every instance.
  87. this._onKeyPress = this._onKeyPress.bind(this);
  88. }
  89. _onKeyPress: (Object) => void;
  90. /**
  91. * KeyPress handler for accessibility.
  92. *
  93. * @param {Object} e - The key event to handle.
  94. *
  95. * @returns {void}
  96. */
  97. _onKeyPress(e) {
  98. if (this.props.onClose && (e.key === ' ' || e.key === 'Enter')) {
  99. e.preventDefault();
  100. this.props.onClose();
  101. }
  102. }
  103. /**
  104. * Implements React's {@link Component#render()}.
  105. *
  106. * @inheritdoc
  107. * @returns {ReactElement}
  108. */
  109. render() {
  110. const {
  111. id,
  112. appearance,
  113. classes,
  114. heading,
  115. hideCloseIconButton,
  116. onClose,
  117. showKeyline,
  118. isHeadingMultiline,
  119. testId,
  120. t
  121. } = this.props;
  122. if (!heading) {
  123. return null;
  124. }
  125. return (
  126. <Header
  127. className = { classes.header }
  128. showKeyline = { showKeyline }>
  129. <Title>
  130. <TitleIcon appearance = { appearance } />
  131. <TitleText
  132. data-testid = { testId && `${testId}-heading` }
  133. id = { id }
  134. isHeadingMultiline = { isHeadingMultiline }>
  135. {heading}
  136. </TitleText>
  137. </Title>
  138. {
  139. !hideCloseIconButton
  140. && <div
  141. className = { classes.closeButton }
  142. id = 'modal-header-close-button'
  143. onClick = { onClose }>
  144. <Icon
  145. ariaLabel = { t('dialog.close') }
  146. onKeyPress = { this._onKeyPress }
  147. role = 'button'
  148. src = { IconClose }
  149. tabIndex = { 0 } />
  150. </div>
  151. }
  152. </Header>
  153. );
  154. }
  155. }
  156. export default translate(withStyles(styles)(ModalHeader));