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.

DialogWithTabs.tsx 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. import React, { ComponentType, useCallback, useEffect, useMemo, useState } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { useDispatch, useSelector } from 'react-redux';
  4. import { makeStyles } from 'tss-react/mui';
  5. import { IReduxState } from '../../../../app/types';
  6. import { hideDialog } from '../../../dialog/actions';
  7. import { IconArrowBack, IconCloseLarge } from '../../../icons/svg';
  8. import { withPixelLineHeight } from '../../../styles/functions.web';
  9. import BaseDialog, { IProps as IBaseProps } from './BaseDialog';
  10. import Button from './Button';
  11. import ClickableIcon from './ClickableIcon';
  12. import ContextMenuItem from './ContextMenuItem';
  13. const MOBILE_BREAKPOINT = 607;
  14. const useStyles = makeStyles()(theme => {
  15. return {
  16. dialog: {
  17. flexDirection: 'row',
  18. height: '560px',
  19. '@media (min-width: 608px) and (max-width: 712px)': {
  20. width: '560px'
  21. },
  22. [`@media (max-width: ${MOBILE_BREAKPOINT}px)`]: {
  23. width: '100%',
  24. position: 'absolute',
  25. top: 0,
  26. left: 0,
  27. bottom: 0
  28. },
  29. '@media (max-width: 448px)': {
  30. height: '100%'
  31. }
  32. },
  33. sidebar: {
  34. display: 'flex',
  35. flexDirection: 'column',
  36. minWidth: '211px',
  37. maxWidth: '100%',
  38. borderRight: `1px solid ${theme.palette.ui03}`,
  39. [`@media (max-width: ${MOBILE_BREAKPOINT}px)`]: {
  40. width: '100%',
  41. borderRight: 'none'
  42. }
  43. },
  44. menuItemMobile: {
  45. paddingLeft: '24px'
  46. },
  47. titleContainer: {
  48. margin: 0,
  49. padding: '24px',
  50. paddingRight: 0,
  51. display: 'flex',
  52. flexDirection: 'row',
  53. alignItems: 'center',
  54. justifyContent: 'space-between',
  55. [`@media (max-width: ${MOBILE_BREAKPOINT}px)`]: {
  56. padding: '16px 24px'
  57. }
  58. },
  59. title: {
  60. ...withPixelLineHeight(theme.typography.heading5),
  61. color: `${theme.palette.text01} !important`,
  62. margin: 0,
  63. padding: 0
  64. },
  65. contentContainer: {
  66. position: 'relative',
  67. display: 'flex',
  68. padding: '24px',
  69. flexDirection: 'column',
  70. overflow: 'hidden',
  71. width: '100%',
  72. [`@media (max-width: ${MOBILE_BREAKPOINT}px)`]: {
  73. padding: '0'
  74. }
  75. },
  76. buttonContainer: {
  77. width: '100%',
  78. boxSizing: 'border-box',
  79. display: 'flex',
  80. alignItems: 'center',
  81. justifyContent: 'flex-end',
  82. flexGrow: 0,
  83. [`@media (max-width: ${MOBILE_BREAKPOINT}px)`]: {
  84. justifyContent: 'space-between',
  85. padding: '16px 24px'
  86. }
  87. },
  88. backContainer: {
  89. display: 'flex',
  90. alignItems: 'center',
  91. '& > button': {
  92. marginRight: '24px'
  93. }
  94. },
  95. closeIcon: {
  96. '&:focus': {
  97. boxShadow: 'none'
  98. }
  99. },
  100. content: {
  101. flexGrow: 1,
  102. overflowY: 'auto',
  103. width: '100%',
  104. boxSizing: 'border-box',
  105. [`@media (max-width: ${MOBILE_BREAKPOINT}px)`]: {
  106. padding: '0 24px'
  107. }
  108. },
  109. footer: {
  110. justifyContent: 'flex-end',
  111. paddingTop: theme.spacing(4),
  112. '& button:last-child': {
  113. marginLeft: '16px'
  114. }
  115. }
  116. };
  117. });
  118. interface IObject {
  119. [key: string]: string | string[] | boolean | number | number[] | {} | undefined;
  120. }
  121. export interface IDialogTab {
  122. className?: string;
  123. component: ComponentType<any>;
  124. icon: Function;
  125. labelKey: string;
  126. name: string;
  127. props?: IObject;
  128. propsUpdateFunction?: (tabState: IObject, newProps: IObject) => IObject;
  129. submit?: Function;
  130. }
  131. interface IProps extends IBaseProps {
  132. defaultTab?: string;
  133. tabs: IDialogTab[];
  134. }
  135. const DialogWithTabs = ({
  136. className,
  137. defaultTab,
  138. titleKey,
  139. tabs
  140. }: IProps) => {
  141. const { classes, cx } = useStyles();
  142. const dispatch = useDispatch();
  143. const { t } = useTranslation();
  144. const [ selectedTab, setSelectedTab ] = useState<string | undefined>(defaultTab ?? tabs[0].name);
  145. const [ tabStates, setTabStates ] = useState(tabs.map(tab => tab.props));
  146. const clientWidth = useSelector((state: IReduxState) => state['features/base/responsive-ui'].clientWidth);
  147. const [ isMobile, setIsMobile ] = useState(false);
  148. useEffect(() => {
  149. if (clientWidth <= MOBILE_BREAKPOINT) {
  150. !isMobile && setIsMobile(true);
  151. } else {
  152. isMobile && setIsMobile(false);
  153. }
  154. }, [ clientWidth, isMobile ]);
  155. useEffect(() => {
  156. if (isMobile) {
  157. setSelectedTab(undefined);
  158. } else {
  159. setSelectedTab(defaultTab ?? tabs[0].name);
  160. }
  161. }, [ isMobile ]);
  162. const back = useCallback(() => {
  163. setSelectedTab(undefined);
  164. }, []);
  165. const onClose = useCallback(() => {
  166. dispatch(hideDialog());
  167. }, []);
  168. const onClick = useCallback((tabName: string) => () => {
  169. setSelectedTab(tabName);
  170. }, []);
  171. const getTabProps = (tabId: number) => {
  172. const tabConfiguration = tabs[tabId];
  173. const currentTabState = tabStates[tabId];
  174. if (tabConfiguration.propsUpdateFunction) {
  175. return tabConfiguration.propsUpdateFunction(
  176. currentTabState ?? {},
  177. tabConfiguration.props ?? {});
  178. }
  179. return { ...currentTabState };
  180. };
  181. const onTabStateChange = useCallback((tabId: number, state: IObject) => {
  182. const newTabStates = [ ...tabStates ];
  183. newTabStates[tabId] = state;
  184. setTabStates(newTabStates);
  185. }, [ tabStates ]);
  186. const onSubmit = useCallback(() => {
  187. tabs.forEach(({ submit }, idx) => {
  188. submit?.(tabStates[idx]);
  189. });
  190. onClose();
  191. }, [ tabs, tabStates ]);
  192. const selectedTabIndex = useMemo(() => {
  193. if (selectedTab) {
  194. return tabs.findIndex(tab => tab.name === selectedTab);
  195. }
  196. return null;
  197. }, [ selectedTab ]);
  198. const selectedTabComponent = useMemo(() => {
  199. if (selectedTabIndex !== null) {
  200. const TabComponent = tabs[selectedTabIndex].component;
  201. return (
  202. <div
  203. className = { tabs[selectedTabIndex].className }
  204. key = { tabs[selectedTabIndex].name }>
  205. <TabComponent
  206. onTabStateChange = { onTabStateChange }
  207. tabId = { selectedTabIndex }
  208. { ...getTabProps(selectedTabIndex) } />
  209. </div>
  210. );
  211. }
  212. return null;
  213. }, [ selectedTabIndex, tabStates ]);
  214. const closeIcon = useMemo(() => (
  215. <ClickableIcon
  216. accessibilityLabel = { t('dialog.close') }
  217. className = { classes.closeIcon }
  218. icon = { IconCloseLarge }
  219. id = 'modal-header-close-button'
  220. onClick = { onClose } />
  221. ), [ onClose ]);
  222. return (
  223. <BaseDialog
  224. className = { cx(classes.dialog, className) }
  225. onClose = { onClose }
  226. size = 'large'>
  227. {(!isMobile || !selectedTab) && (
  228. <div className = { classes.sidebar }>
  229. <div className = { classes.titleContainer }>
  230. <h2 className = { classes.title }>{t(titleKey ?? '')}</h2>
  231. {isMobile && closeIcon}
  232. </div>
  233. {tabs.map(tab => {
  234. const label = t(tab.labelKey);
  235. return (
  236. <ContextMenuItem
  237. accessibilityLabel = { label }
  238. className = { cx(isMobile && classes.menuItemMobile) }
  239. icon = { tab.icon }
  240. key = { tab.name }
  241. onClick = { onClick(tab.name) }
  242. selected = { tab.name === selectedTab }
  243. text = { label } />
  244. );
  245. })}
  246. </div>
  247. )}
  248. {(!isMobile || selectedTab) && (
  249. <div className = { classes.contentContainer }>
  250. <div className = { classes.buttonContainer }>
  251. {isMobile && (
  252. <span className = { classes.backContainer }>
  253. <ClickableIcon
  254. accessibilityLabel = { t('dialog.Back') }
  255. className = { classes.closeIcon }
  256. icon = { IconArrowBack }
  257. id = 'modal-header-back-button'
  258. onClick = { back } />
  259. <h2 className = { classes.title }>
  260. {(selectedTabIndex !== null) && t(tabs[selectedTabIndex].labelKey)}
  261. </h2>
  262. </span>
  263. )}
  264. {closeIcon}
  265. </div>
  266. <div className = { classes.content }>
  267. {selectedTabComponent}
  268. </div>
  269. <div
  270. className = { cx(classes.buttonContainer, classes.footer) }>
  271. <Button
  272. accessibilityLabel = { t('dialog.Cancel') }
  273. id = 'modal-dialog-cancel-button'
  274. labelKey = { 'dialog.Cancel' }
  275. onClick = { onClose }
  276. type = 'tertiary' />
  277. <Button
  278. accessibilityLabel = { t('dialog.Ok') }
  279. id = 'modal-dialog-ok-button'
  280. labelKey = { 'dialog.Ok' }
  281. onClick = { onSubmit } />
  282. </div>
  283. </div>
  284. )}
  285. </BaseDialog>
  286. );
  287. };
  288. export default DialogWithTabs;