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 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. * An array of the states of the tabs.
  44. */
  45. tabStates: Array<Object>
  46. };
  47. /**
  48. * A React {@code Component} for displaying a dialog with tabs.
  49. *
  50. * @extends Component
  51. */
  52. class DialogWithTabs extends Component<Props, State> {
  53. /**
  54. * Initializes a new {@code DialogWithTabs} instance.
  55. *
  56. * @param {Object} props - The read-only React {@code Component} props with
  57. * which the new instance is to be initialized.
  58. */
  59. constructor(props: Props) {
  60. super(props);
  61. this.state = {
  62. tabStates: this.props.tabs.map(tab => tab.props)
  63. };
  64. this._onSubmit = this._onSubmit.bind(this);
  65. this._onTabStateChange = this._onTabStateChange.bind(this);
  66. }
  67. /**
  68. * Implements React's {@link Component#render()}.
  69. *
  70. * @inheritdoc
  71. * @returns {ReactElement}
  72. */
  73. render() {
  74. const onCancel = this.props.closeDialog;
  75. return (
  76. <StatelessDialog
  77. disableBlanketClickDismiss
  78. = { this.props.disableBlanketClickDismiss }
  79. onCancel = { onCancel }
  80. onSubmit = { this._onSubmit }
  81. titleKey = 'settings.title'>
  82. <div className = 'settings-dialog'>
  83. { this._renderTabs() }
  84. </div>
  85. </StatelessDialog>
  86. );
  87. }
  88. /**
  89. * Renders the tabs from the tab information passed on props.
  90. *
  91. * @returns {void}
  92. */
  93. _renderTabs() {
  94. const { defaultTab = 0, t, tabs } = this.props;
  95. if (tabs.length === 1) {
  96. return this._renderTab({
  97. ...tabs[0],
  98. tabId: 0
  99. });
  100. }
  101. if (tabs.length > 1) {
  102. return (
  103. <Tabs
  104. tabs = {
  105. tabs.map(({ component, label, styles }, idx) => {
  106. return {
  107. content: this._renderTab({
  108. component,
  109. styles,
  110. tabId: idx
  111. }),
  112. defaultSelected: defaultTab === idx,
  113. label: t(label)
  114. };
  115. })
  116. } />);
  117. }
  118. logger.warn('No settings tabs configured to display.');
  119. return null;
  120. }
  121. /**
  122. * Renders a tab from the tab information passed as parameters.
  123. *
  124. * @param {Object} tabInfo - Information about the tab.
  125. * @returns {Component} - The tab.
  126. */
  127. _renderTab({ component, styles, tabId }) {
  128. const { closeDialog } = this.props;
  129. const TabComponent = component;
  130. return (
  131. <div className = { styles }>
  132. <TabComponent
  133. closeDialog = { closeDialog }
  134. onTabStateChange
  135. = { this._onTabStateChange }
  136. tabId = { tabId }
  137. { ...this.state.tabStates[tabId] } />
  138. </div>);
  139. }
  140. _onTabStateChange: (number, Object) => void;
  141. /**
  142. * Changes the state for a tab.
  143. *
  144. * @param {number} tabId - The id of the tab which state will be changed.
  145. * @param {Object} state - The new state.
  146. * @returns {void}
  147. */
  148. _onTabStateChange(tabId, state) {
  149. const tabStates = [ ...this.state.tabStates ];
  150. tabStates[tabId] = state;
  151. this.setState({ tabStates });
  152. }
  153. _onSubmit: () => void;
  154. /**
  155. * Submits the information filled in the dialog.
  156. *
  157. * @returns {void}
  158. */
  159. _onSubmit() {
  160. const { onSubmit, tabs } = this.props;
  161. tabs.forEach(({ submit }, idx) => {
  162. submit(this.state.tabStates[idx]);
  163. });
  164. onSubmit();
  165. }
  166. }
  167. export default translate(DialogWithTabs);