您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

DialogWithTabs.web.js 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. * Gets the props to pass into the tab component.
  90. *
  91. * @param {number} tabId - The index of the tab configuration within
  92. * {@link this.state.tabStates}.
  93. * @returns {Object}
  94. */
  95. _getTabProps(tabId) {
  96. const { tabs } = this.props;
  97. const { tabStates } = this.state;
  98. const tabConfiguration = tabs[tabId];
  99. const currentTabState = tabStates[tabId];
  100. if (tabConfiguration.propsUpdateFunction) {
  101. return tabConfiguration.propsUpdateFunction(
  102. currentTabState,
  103. tabConfiguration.props);
  104. }
  105. return { ...currentTabState };
  106. }
  107. /**
  108. * Renders the tabs from the tab information passed on props.
  109. *
  110. * @returns {void}
  111. */
  112. _renderTabs() {
  113. const { defaultTab = 0, t, tabs } = this.props;
  114. if (tabs.length === 1) {
  115. return this._renderTab({
  116. ...tabs[0],
  117. tabId: 0
  118. });
  119. }
  120. if (tabs.length > 1) {
  121. return (
  122. <Tabs
  123. tabs = {
  124. tabs.map(({ component, label, styles }, idx) => {
  125. return {
  126. content: this._renderTab({
  127. component,
  128. styles,
  129. tabId: idx
  130. }),
  131. defaultSelected: defaultTab === idx,
  132. label: t(label)
  133. };
  134. })
  135. } />);
  136. }
  137. logger.warn('No settings tabs configured to display.');
  138. return null;
  139. }
  140. /**
  141. * Renders a tab from the tab information passed as parameters.
  142. *
  143. * @param {Object} tabInfo - Information about the tab.
  144. * @returns {Component} - The tab.
  145. */
  146. _renderTab({ component, styles, tabId }) {
  147. const { closeDialog } = this.props;
  148. const TabComponent = component;
  149. return (
  150. <div className = { styles }>
  151. <TabComponent
  152. closeDialog = { closeDialog }
  153. mountCallback = { this.props.tabs[tabId].onMount }
  154. onTabStateChange
  155. = { this._onTabStateChange }
  156. tabId = { tabId }
  157. { ...this._getTabProps(tabId) } />
  158. </div>);
  159. }
  160. _onTabStateChange: (number, Object) => void;
  161. /**
  162. * Changes the state for a tab.
  163. *
  164. * @param {number} tabId - The id of the tab which state will be changed.
  165. * @param {Object} state - The new state.
  166. * @returns {void}
  167. */
  168. _onTabStateChange(tabId, state) {
  169. const tabStates = [ ...this.state.tabStates ];
  170. tabStates[tabId] = state;
  171. this.setState({ tabStates });
  172. }
  173. _onSubmit: () => void;
  174. /**
  175. * Submits the information filled in the dialog.
  176. *
  177. * @returns {void}
  178. */
  179. _onSubmit() {
  180. const { onSubmit, tabs } = this.props;
  181. tabs.forEach(({ submit }, idx) => {
  182. submit && submit(this.state.tabStates[idx]);
  183. });
  184. onSubmit();
  185. }
  186. }
  187. export default translate(DialogWithTabs);