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

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