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.

DeviceSelector.web.js 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* @flow */
  2. import React, { Component } from 'react';
  3. import { translate } from '../../base/i18n/functions';
  4. import Select from '../../base/ui/components/web/Select';
  5. /**
  6. * The type of the React {@code Component} props of {@link DeviceSelector}.
  7. */
  8. type Props = {
  9. /**
  10. * MediaDeviceInfos used for display in the select element.
  11. */
  12. devices: Array<Object>,
  13. /**
  14. * If false, will return a selector with no selection options.
  15. */
  16. hasPermission: boolean,
  17. /**
  18. * CSS class for the icon to the left of the dropdown trigger.
  19. */
  20. icon: string,
  21. /**
  22. * If true, will render the selector disabled with a default selection.
  23. */
  24. isDisabled: boolean,
  25. /**
  26. * The translation key to display as a menu label.
  27. */
  28. label: string,
  29. /**
  30. * The callback to invoke when a selection is made.
  31. */
  32. onSelect: Function,
  33. /**
  34. * The default device to display as selected.
  35. */
  36. selectedDeviceId: string,
  37. /**
  38. * Invoked to obtain translated strings.
  39. */
  40. t: Function,
  41. /**
  42. * The id of the dropdown element.
  43. */
  44. id: string
  45. };
  46. /**
  47. * React component for selecting a device from a select element. Wraps
  48. * AKDropdownMenu with device selection specific logic.
  49. *
  50. * @augments Component
  51. */
  52. class DeviceSelector extends Component<Props> {
  53. /**
  54. * Initializes a new DeviceSelector instance.
  55. *
  56. * @param {Object} props - The read-only React Component props with which
  57. * the new instance is to be initialized.
  58. */
  59. constructor(props) {
  60. super(props);
  61. this._onSelect = this._onSelect.bind(this);
  62. }
  63. /**
  64. * Implements React's {@link Component#render()}.
  65. *
  66. * @inheritdoc
  67. * @returns {ReactElement}
  68. */
  69. render() {
  70. if (this.props.hasPermission === undefined) {
  71. return null;
  72. }
  73. if (!this.props.hasPermission) {
  74. return this._renderNoPermission();
  75. }
  76. if (!this.props.devices || !this.props.devices.length) {
  77. return this._renderNoDevices();
  78. }
  79. const items = this.props.devices.map(device => {
  80. return {
  81. value: device.deviceId,
  82. label: device.label || device.deviceId
  83. };
  84. });
  85. const defaultSelected = this.props.devices.find(item =>
  86. item.deviceId === this.props.selectedDeviceId
  87. );
  88. return this._createDropdown({
  89. defaultSelected,
  90. isDisabled: this.props.isDisabled,
  91. items,
  92. placeholder: this.props.t('deviceSelection.selectADevice')
  93. });
  94. }
  95. /**
  96. * Creates a React Element for displaying the passed in text surrounded by
  97. * two icons. The left icon is the icon class passed in through props and
  98. * the right icon is AtlasKit ExpandIcon.
  99. *
  100. * @param {string} triggerText - The text to display within the element.
  101. * @private
  102. * @returns {ReactElement}
  103. */
  104. _createDropdownTrigger(triggerText) {
  105. return (
  106. <div className = 'device-selector-trigger'>
  107. <span className = 'device-selector-trigger-text'>
  108. { triggerText }
  109. </span>
  110. </div>
  111. );
  112. }
  113. /**
  114. * Creates a AKDropdownMenu Component using passed in props and options. If
  115. * the dropdown needs to be disabled, then only the AKDropdownMenu trigger
  116. * element is returned to simulate a disabled state.
  117. *
  118. * @param {Object} options - Additional configuration for display.
  119. * @param {Object} options.defaultSelected - The option that should be set
  120. * as currently chosen.
  121. * @param {boolean} options.isDisabled - If true, only the AKDropdownMenu
  122. * trigger component will be returned to simulate a disabled dropdown.
  123. * @param {Array} options.items - All the selectable options to display.
  124. * @param {string} options.placeholder - The translation key to display when
  125. * no selection has been made.
  126. * @private
  127. * @returns {ReactElement}
  128. */
  129. _createDropdown(options) {
  130. const triggerText
  131. = (options.defaultSelected && (options.defaultSelected.label || options.defaultSelected.deviceId))
  132. || options.placeholder;
  133. const trigger = this._createDropdownTrigger(triggerText);
  134. if (options.isDisabled || !options.items.length) {
  135. return (
  136. <div className = 'device-selector-trigger-disabled'>
  137. { trigger }
  138. </div>
  139. );
  140. }
  141. return (
  142. <div className = 'dropdown-menu'>
  143. <Select
  144. onChange = { this._onSelect }
  145. options = { options.items }
  146. value = { this.props.selectedDeviceId } />
  147. </div>
  148. );
  149. }
  150. _onSelect: (Object) => void;
  151. /**
  152. * Invokes the passed in callback to notify of selection changes.
  153. *
  154. * @param {Object} e - The key event to handle.
  155. *
  156. * @private
  157. * @returns {void}
  158. */
  159. _onSelect(e) {
  160. const deviceId = e.target.value;
  161. if (this.props.selectedDeviceId !== deviceId) {
  162. this.props.onSelect(deviceId);
  163. }
  164. }
  165. /**
  166. * Creates a Select Component that is disabled and has a placeholder
  167. * indicating there are no devices to select.
  168. *
  169. * @private
  170. * @returns {ReactElement}
  171. */
  172. _renderNoDevices() {
  173. return this._createDropdown({
  174. isDisabled: true,
  175. placeholder: this.props.t('settings.noDevice')
  176. });
  177. }
  178. /**
  179. * Creates a AKDropdownMenu Component that is disabled and has a placeholder
  180. * stating there is no permission to display the devices.
  181. *
  182. * @private
  183. * @returns {ReactElement}
  184. */
  185. _renderNoPermission() {
  186. return this._createDropdown({
  187. isDisabled: true,
  188. placeholder: this.props.t('deviceSelection.noPermission')
  189. });
  190. }
  191. }
  192. export default translate(DeviceSelector);