您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ModalHeader.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. * @export
  72. * @class ModalHeader
  73. * @extends {React.Component<Props>}
  74. */
  75. class ModalHeader extends React.Component<Props> {
  76. static defaultProps = {
  77. isHeadingMultiline: true
  78. };
  79. /**
  80. * Initializes a new {@code ModalHeader} instance.
  81. *
  82. * @param {*} props - The read-only properties with which the new instance
  83. * is to be initialized.
  84. */
  85. constructor(props) {
  86. super(props);
  87. // Bind event handler so it is only bound once for every instance.
  88. this._onKeyPress = this._onKeyPress.bind(this);
  89. }
  90. _onKeyPress: (Object) => void;
  91. /**
  92. * KeyPress handler for accessibility.
  93. *
  94. * @param {Object} e - The key event to handle.
  95. *
  96. * @returns {void}
  97. */
  98. _onKeyPress(e) {
  99. if (this.props.onClose && (e.key === ' ' || e.key === 'Enter')) {
  100. e.preventDefault();
  101. this.props.onClose();
  102. }
  103. }
  104. /**
  105. * Implements React's {@link Component#render()}.
  106. *
  107. * @inheritdoc
  108. * @returns {ReactElement}
  109. */
  110. render() {
  111. const {
  112. id,
  113. appearance,
  114. classes,
  115. heading,
  116. hideCloseIconButton,
  117. onClose,
  118. showKeyline,
  119. isHeadingMultiline,
  120. testId,
  121. t
  122. } = this.props;
  123. if (!heading) {
  124. return null;
  125. }
  126. return (
  127. <Header
  128. className = { classes.header }
  129. showKeyline = { showKeyline }>
  130. <Title>
  131. <TitleIcon appearance = { appearance } />
  132. <TitleText
  133. data-testid = { testId && `${testId}-heading` }
  134. id = { id }
  135. isHeadingMultiline = { isHeadingMultiline }>
  136. {heading}
  137. </TitleText>
  138. </Title>
  139. {
  140. !hideCloseIconButton
  141. && <div
  142. className = { classes.closeButton }
  143. id = 'modal-header-close-button'
  144. onClick = { onClose }>
  145. <Icon
  146. ariaLabel = { t('dialog.close') }
  147. onKeyPress = { this._onKeyPress }
  148. role = 'button'
  149. src = { IconClose }
  150. tabIndex = { 0 } />
  151. </div>
  152. }
  153. </Header>
  154. );
  155. }
  156. }
  157. export default translate(withStyles(styles)(ModalHeader));