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.4KB

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