You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Dialog.tsx 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import React, { useCallback } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { useDispatch } from 'react-redux';
  4. import { makeStyles } from 'tss-react/mui';
  5. import { hideDialog } from '../../../dialog/actions';
  6. import { IconCloseLarge } from '../../../icons/svg';
  7. import { withPixelLineHeight } from '../../../styles/functions.web';
  8. import { operatesWithEnterKey } from '../../functions.web';
  9. import BaseDialog, { IProps as IBaseDialogProps } from './BaseDialog';
  10. import Button from './Button';
  11. import ClickableIcon from './ClickableIcon';
  12. const useStyles = makeStyles()(theme => {
  13. return {
  14. header: {
  15. width: '100%',
  16. padding: '24px',
  17. boxSizing: 'border-box',
  18. display: 'flex',
  19. alignItems: 'flex-start',
  20. justifyContent: 'space-between'
  21. },
  22. title: {
  23. color: theme.palette.text01,
  24. ...withPixelLineHeight(theme.typography.heading5),
  25. margin: 0,
  26. padding: 0
  27. },
  28. content: {
  29. height: 'auto',
  30. overflowY: 'auto',
  31. width: '100%',
  32. boxSizing: 'border-box',
  33. padding: '0 24px',
  34. overflowX: 'hidden',
  35. minHeight: '40px',
  36. '@media (max-width: 448px)': {
  37. height: '100%'
  38. }
  39. },
  40. footer: {
  41. width: '100%',
  42. boxSizing: 'border-box',
  43. display: 'flex',
  44. alignItems: 'center',
  45. justifyContent: 'flex-end',
  46. padding: '24px',
  47. '& button:last-child': {
  48. marginLeft: '16px'
  49. }
  50. }
  51. };
  52. });
  53. interface IDialogProps extends IBaseDialogProps {
  54. back?: {
  55. hidden?: boolean;
  56. onClick?: () => void;
  57. translationKey?: string;
  58. };
  59. cancel?: {
  60. hidden?: boolean;
  61. translationKey?: string;
  62. };
  63. children?: React.ReactNode;
  64. disableAutoHideOnSubmit?: boolean;
  65. hideCloseButton?: boolean;
  66. ok?: {
  67. disabled?: boolean;
  68. hidden?: boolean;
  69. translationKey?: string;
  70. };
  71. onCancel?: () => void;
  72. onSubmit?: () => void;
  73. }
  74. const Dialog = ({
  75. back = { hidden: true },
  76. cancel = { translationKey: 'dialog.Cancel' },
  77. children,
  78. className,
  79. description,
  80. disableAutoHideOnSubmit = false,
  81. disableBackdropClose,
  82. hideCloseButton,
  83. disableEnter,
  84. ok = { translationKey: 'dialog.Ok' },
  85. onCancel,
  86. onSubmit,
  87. size,
  88. title,
  89. titleKey
  90. }: IDialogProps) => {
  91. const { classes } = useStyles();
  92. const { t } = useTranslation();
  93. const dispatch = useDispatch();
  94. const onClose = useCallback(() => {
  95. dispatch(hideDialog());
  96. onCancel?.();
  97. }, [ onCancel ]);
  98. const submit = useCallback(() => {
  99. if (onSubmit && (
  100. (document.activeElement && !operatesWithEnterKey(document.activeElement))
  101. || !document.activeElement
  102. )) {
  103. !disableAutoHideOnSubmit && dispatch(hideDialog());
  104. onSubmit();
  105. }
  106. }, [ onSubmit ]);
  107. return (
  108. <BaseDialog
  109. className = { className }
  110. description = { description }
  111. disableBackdropClose = { disableBackdropClose }
  112. disableEnter = { disableEnter }
  113. onClose = { onClose }
  114. size = { size }
  115. submit = { submit }
  116. title = { title }
  117. titleKey = { titleKey }>
  118. <div className = { classes.header }>
  119. <h1
  120. className = { classes.title }
  121. id = 'dialog-title'>
  122. {title ?? t(titleKey ?? '')}
  123. </h1>
  124. {!hideCloseButton && (
  125. <ClickableIcon
  126. accessibilityLabel = { t('dialog.accessibilityLabel.close') }
  127. icon = { IconCloseLarge }
  128. id = 'modal-header-close-button'
  129. onClick = { onClose } />
  130. )}
  131. </div>
  132. <div
  133. className = { classes.content }
  134. data-autofocus-inside = 'true'>
  135. {children}
  136. </div>
  137. <div
  138. className = { classes.footer }
  139. data-autofocus-inside = 'true'>
  140. {!back.hidden && <Button
  141. accessibilityLabel = { t(back.translationKey ?? '') }
  142. labelKey = { back.translationKey }
  143. // eslint-disable-next-line react/jsx-handler-names
  144. onClick = { back.onClick }
  145. type = 'secondary' />}
  146. {!cancel.hidden && <Button
  147. accessibilityLabel = { t(cancel.translationKey ?? '') }
  148. labelKey = { cancel.translationKey }
  149. onClick = { onClose }
  150. type = 'tertiary' />}
  151. {!ok.hidden && <Button
  152. accessibilityLabel = { t(ok.translationKey ?? '') }
  153. disabled = { ok.disabled }
  154. id = 'modal-dialog-ok-button'
  155. isSubmit = { true }
  156. labelKey = { ok.translationKey }
  157. onClick = { submit } />}
  158. </div>
  159. </BaseDialog>
  160. );
  161. };
  162. export default Dialog;