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.js 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // @flow
  2. import Tabs from '@atlaskit/tabs';
  3. import React, { Component } from 'react';
  4. import { translate } from '../../../i18n/functions';
  5. import logger from '../../logger';
  6. import StatelessDialog from './StatelessDialog';
  7. /**
  8. * The type of the React {@code Component} props of {@link DialogWithTabs}.
  9. */
  10. export type Props = {
  11. /**
  12. * Function that closes the dialog.
  13. */
  14. closeDialog: Function,
  15. /**
  16. * Css class name that will be added to the dialog.
  17. */
  18. cssClassName: string,
  19. /**
  20. * Which settings tab should be initially displayed. If not defined then
  21. * the first tab will be displayed.
  22. */
  23. defaultTab: number,
  24. /**
  25. * Disables dismissing the dialog when the blanket is clicked. Enabled
  26. * by default.
  27. */
  28. disableBlanketClickDismiss: boolean,
  29. /**
  30. * Callback invoked when the Save button has been pressed.
  31. */
  32. onSubmit: Function,
  33. /**
  34. * Invoked to obtain translated strings.
  35. */
  36. t: Function,
  37. /**
  38. * Information about the tabs that will be rendered.
  39. */
  40. tabs: Array<Object>,
  41. /**
  42. * Key to use for showing a title.
  43. */
  44. titleKey: string
  45. };
  46. /**
  47. * The type of the React {@code Component} state of {@link DialogWithTabs}.
  48. */
  49. type State = {
  50. /**
  51. * The index of the tab that should be displayed.
  52. */
  53. selectedTab: number,
  54. /**
  55. * An array of the states of the tabs.
  56. */
  57. tabStates: Array<Object>
  58. };
  59. /**
  60. * A React {@code Component} for displaying a dialog with tabs.
  61. *
  62. * @extends Component
  63. */
  64. class DialogWithTabs extends Component<Props, State> {
  65. /**
  66. * Initializes a new {@code DialogWithTabs} instance.
  67. *
  68. * @param {Object} props - The read-only React {@code Component} props with
  69. * which the new instance is to be initialized.
  70. */
  71. constructor(props: Props) {
  72. super(props);
  73. this.state = {
  74. selectedTab: this.props.defaultTab || 0,
  75. tabStates: this.props.tabs.map(tab => tab.props)
  76. };
  77. this._onSubmit = this._onSubmit.bind(this);
  78. this._onTabSelected = this._onTabSelected.bind(this);
  79. this._onTabStateChange = this._onTabStateChange.bind(this);
  80. }
  81. /**
  82. * Implements React's {@link Component#render()}.
  83. *
  84. * @inheritdoc
  85. * @returns {ReactElement}
  86. */
  87. render() {
  88. const onCancel = this.props.closeDialog;
  89. return (
  90. <StatelessDialog
  91. disableBlanketClickDismiss
  92. = { this.props.disableBlanketClickDismiss }
  93. onCancel = { onCancel }
  94. onSubmit = { this._onSubmit }
  95. titleKey = { this.props.titleKey } >
  96. <div className = { this.props.cssClassName } >
  97. { this._renderTabs() }
  98. </div>
  99. </StatelessDialog>
  100. );
  101. }
  102. /**
  103. * Gets the props to pass into the tab component.
  104. *
  105. * @param {number} tabId - The index of the tab configuration within
  106. * {@link this.state.tabStates}.
  107. * @returns {Object}
  108. */
  109. _getTabProps(tabId) {
  110. const { tabs } = this.props;
  111. const { tabStates } = this.state;
  112. const tabConfiguration = tabs[tabId];
  113. const currentTabState = tabStates[tabId];
  114. if (tabConfiguration.propsUpdateFunction) {
  115. return tabConfiguration.propsUpdateFunction(
  116. currentTabState,
  117. tabConfiguration.props);
  118. }
  119. return { ...currentTabState };
  120. }
  121. _onTabSelected: (Object, number) => void;
  122. /**
  123. * Callback invoked when the desired tab to display should be changed.
  124. *
  125. * @param {Object} tab - The configuration passed into atlaskit tabs to
  126. * describe how to display the selected tab.
  127. * @param {number} tabIndex - The index of the tab within the array of
  128. * displayed tabs.
  129. * @private
  130. * @returns {void}
  131. */
  132. _onTabSelected(tab, tabIndex) { // eslint-disable-line no-unused-vars
  133. this.setState({ selectedTab: tabIndex });
  134. }
  135. /**
  136. * Renders the tabs from the tab information passed on props.
  137. *
  138. * @returns {void}
  139. */
  140. _renderTabs() {
  141. const { t, tabs } = this.props;
  142. if (tabs.length === 1) {
  143. return this._renderTab({
  144. ...tabs[0],
  145. tabId: 0
  146. });
  147. }
  148. if (tabs.length > 1) {
  149. return (
  150. <Tabs
  151. onSelect = { this._onTabSelected }
  152. selected = { this.state.selectedTab }
  153. tabs = {
  154. tabs.map(({ component, label, styles }, idx) => {
  155. return {
  156. content: this._renderTab({
  157. component,
  158. styles,
  159. tabId: idx
  160. }),
  161. label: t(label)
  162. };
  163. })
  164. } />);
  165. }
  166. logger.warn('No settings tabs configured to display.');
  167. return null;
  168. }
  169. /**
  170. * Renders a tab from the tab information passed as parameters.
  171. *
  172. * @param {Object} tabInfo - Information about the tab.
  173. * @returns {Component} - The tab.
  174. */
  175. _renderTab({ component, styles, tabId }) {
  176. const { closeDialog } = this.props;
  177. const TabComponent = component;
  178. return (
  179. <div className = { styles }>
  180. <TabComponent
  181. closeDialog = { closeDialog }
  182. mountCallback = { this.props.tabs[tabId].onMount }
  183. onTabStateChange
  184. = { this._onTabStateChange }
  185. tabId = { tabId }
  186. { ...this._getTabProps(tabId) } />
  187. </div>);
  188. }
  189. _onTabStateChange: (number, Object) => void;
  190. /**
  191. * Changes the state for a tab.
  192. *
  193. * @param {number} tabId - The id of the tab which state will be changed.
  194. * @param {Object} state - The new state.
  195. * @returns {void}
  196. */
  197. _onTabStateChange(tabId, state) {
  198. const tabStates = [ ...this.state.tabStates ];
  199. tabStates[tabId] = state;
  200. this.setState({ tabStates });
  201. }
  202. _onSubmit: () => void;
  203. /**
  204. * Submits the information filled in the dialog.
  205. *
  206. * @returns {void}
  207. */
  208. _onSubmit() {
  209. const { onSubmit, tabs } = this.props;
  210. tabs.forEach(({ submit }, idx) => {
  211. submit && submit(this.state.tabStates[idx]);
  212. });
  213. onSubmit();
  214. }
  215. }
  216. export default translate(DialogWithTabs);