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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. closeButtonContainer: {
  77. paddingBottom: theme.spacing(4)
  78. },
  79. buttonContainer: {
  80. width: '100%',
  81. boxSizing: 'border-box',
  82. display: 'flex',
  83. alignItems: 'center',
  84. justifyContent: 'flex-end',
  85. flexGrow: 0,
  86. [`@media (max-width: ${MOBILE_BREAKPOINT}px)`]: {
  87. justifyContent: 'space-between',
  88. padding: '16px 24px'
  89. }
  90. },
  91. backContainer: {
  92. display: 'flex',
  93. alignItems: 'center',
  94. '& > button': {
  95. marginRight: '24px'
  96. }
  97. },
  98. content: {
  99. flexGrow: 1,
  100. overflowY: 'auto',
  101. width: '100%',
  102. boxSizing: 'border-box',
  103. [`@media (max-width: ${MOBILE_BREAKPOINT}px)`]: {
  104. padding: '0 24px'
  105. }
  106. },
  107. footer: {
  108. justifyContent: 'flex-end',
  109. paddingTop: theme.spacing(4),
  110. '& button:last-child': {
  111. marginLeft: '16px'
  112. }
  113. }
  114. };
  115. });
  116. interface IObject {
  117. [key: string]: string | string[] | boolean | number | number[] | {} | undefined;
  118. }
  119. export interface IDialogTab<P> {
  120. className?: string;
  121. component: ComponentType<any>;
  122. icon: Function;
  123. labelKey: string;
  124. name: string;
  125. props?: IObject;
  126. propsUpdateFunction?: (tabState: IObject, newProps: P) => P;
  127. submit?: Function;
  128. }
  129. interface IProps extends IBaseProps {
  130. defaultTab?: string;
  131. tabs: IDialogTab<any>[];
  132. }
  133. const DialogWithTabs = ({
  134. className,
  135. defaultTab,
  136. titleKey,
  137. tabs
  138. }: IProps) => {
  139. const { classes, cx } = useStyles();
  140. const dispatch = useDispatch();
  141. const { t } = useTranslation();
  142. const [ selectedTab, setSelectedTab ] = useState<string | undefined>(defaultTab ?? tabs[0].name);
  143. const [ tabStates, setTabStates ] = useState(tabs.map(tab => tab.props));
  144. const clientWidth = useSelector((state: IReduxState) => state['features/base/responsive-ui'].clientWidth);
  145. const [ isMobile, setIsMobile ] = useState(false);
  146. useEffect(() => {
  147. if (clientWidth <= MOBILE_BREAKPOINT) {
  148. !isMobile && setIsMobile(true);
  149. } else {
  150. isMobile && setIsMobile(false);
  151. }
  152. }, [ clientWidth, isMobile ]);
  153. useEffect(() => {
  154. if (isMobile) {
  155. setSelectedTab(undefined);
  156. } else {
  157. setSelectedTab(defaultTab ?? tabs[0].name);
  158. }
  159. }, [ isMobile ]);
  160. const back = useCallback(() => {
  161. setSelectedTab(undefined);
  162. }, []);
  163. const onClose = useCallback(() => {
  164. dispatch(hideDialog());
  165. }, []);
  166. const onClick = useCallback((tabName: string) => () => {
  167. setSelectedTab(tabName);
  168. }, []);
  169. const getTabProps = (tabId: number) => {
  170. const tabConfiguration = tabs[tabId];
  171. const currentTabState = tabStates[tabId];
  172. if (tabConfiguration.propsUpdateFunction) {
  173. return tabConfiguration.propsUpdateFunction(
  174. currentTabState ?? {},
  175. tabConfiguration.props ?? {});
  176. }
  177. return { ...currentTabState };
  178. };
  179. const onTabStateChange = useCallback((tabId: number, state: IObject) => {
  180. const newTabStates = [ ...tabStates ];
  181. newTabStates[tabId] = state;
  182. setTabStates(newTabStates);
  183. }, [ tabStates ]);
  184. const onSubmit = useCallback(() => {
  185. tabs.forEach(({ submit }, idx) => {
  186. submit?.(tabStates[idx]);
  187. });
  188. onClose();
  189. }, [ tabs, tabStates ]);
  190. const selectedTabIndex = useMemo(() => {
  191. if (selectedTab) {
  192. return tabs.findIndex(tab => tab.name === selectedTab);
  193. }
  194. return null;
  195. }, [ selectedTab ]);
  196. const selectedTabComponent = useMemo(() => {
  197. if (selectedTabIndex !== null) {
  198. const TabComponent = tabs[selectedTabIndex].component;
  199. return (
  200. <div
  201. className = { tabs[selectedTabIndex].className }
  202. key = { tabs[selectedTabIndex].name }>
  203. <TabComponent
  204. onTabStateChange = { onTabStateChange }
  205. tabId = { selectedTabIndex }
  206. { ...getTabProps(selectedTabIndex) } />
  207. </div>
  208. );
  209. }
  210. return null;
  211. }, [ selectedTabIndex, tabStates ]);
  212. const closeIcon = useMemo(() => (
  213. <ClickableIcon
  214. accessibilityLabel = { t('dialog.close') }
  215. icon = { IconCloseLarge }
  216. id = 'modal-header-close-button'
  217. onClick = { onClose } />
  218. ), [ onClose ]);
  219. return (
  220. <BaseDialog
  221. className = { cx(classes.dialog, className) }
  222. onClose = { onClose }
  223. size = 'large'>
  224. {(!isMobile || !selectedTab) && (
  225. <div className = { classes.sidebar }>
  226. <div className = { classes.titleContainer }>
  227. <h2 className = { classes.title }>{t(titleKey ?? '')}</h2>
  228. {isMobile && closeIcon}
  229. </div>
  230. {tabs.map(tab => {
  231. const label = t(tab.labelKey);
  232. return (
  233. <ContextMenuItem
  234. accessibilityLabel = { label }
  235. className = { cx(isMobile && classes.menuItemMobile) }
  236. icon = { tab.icon }
  237. key = { tab.name }
  238. onClick = { onClick(tab.name) }
  239. selected = { tab.name === selectedTab }
  240. text = { label } />
  241. );
  242. })}
  243. </div>
  244. )}
  245. {(!isMobile || selectedTab) && (
  246. <div className = { classes.contentContainer }>
  247. <div className = { cx(classes.buttonContainer, classes.closeButtonContainer) }>
  248. {isMobile && (
  249. <span className = { classes.backContainer }>
  250. <ClickableIcon
  251. accessibilityLabel = { t('dialog.Back') }
  252. icon = { IconArrowBack }
  253. id = 'modal-header-back-button'
  254. onClick = { back } />
  255. <h2 className = { classes.title }>
  256. {(selectedTabIndex !== null) && t(tabs[selectedTabIndex].labelKey)}
  257. </h2>
  258. </span>
  259. )}
  260. {closeIcon}
  261. </div>
  262. <div className = { classes.content }>
  263. {selectedTabComponent}
  264. </div>
  265. <div
  266. className = { cx(classes.buttonContainer, classes.footer) }>
  267. <Button
  268. accessibilityLabel = { t('dialog.Cancel') }
  269. id = 'modal-dialog-cancel-button'
  270. labelKey = { 'dialog.Cancel' }
  271. onClick = { onClose }
  272. type = 'tertiary' />
  273. <Button
  274. accessibilityLabel = { t('dialog.Ok') }
  275. id = 'modal-dialog-ok-button'
  276. labelKey = { 'dialog.Ok' }
  277. onClick = { onSubmit } />
  278. </div>
  279. </div>
  280. )}
  281. </BaseDialog>
  282. );
  283. };
  284. export default DialogWithTabs;