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.web.js 6.4KB

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