Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

DeviceSelector.web.tsx 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import React, { useCallback } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { makeStyles } from 'tss-react/mui';
  4. import { withPixelLineHeight } from '../../base/styles/functions.web';
  5. import Select from '../../base/ui/components/web/Select';
  6. /**
  7. * The type of the React {@code Component} props of {@link DeviceSelector}.
  8. */
  9. interface IProps {
  10. /**
  11. * MediaDeviceInfos used for display in the select element.
  12. */
  13. devices: Array<MediaDeviceInfo> | undefined;
  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. * The id of the dropdown element.
  24. */
  25. id: string;
  26. /**
  27. * If true, will render the selector disabled with a default selection.
  28. */
  29. isDisabled: boolean;
  30. /**
  31. * The translation key to display as a menu label.
  32. */
  33. label: string;
  34. /**
  35. * The callback to invoke when a selection is made.
  36. */
  37. onSelect: Function;
  38. /**
  39. * The default device to display as selected.
  40. */
  41. selectedDeviceId: string;
  42. }
  43. const useStyles = makeStyles()(theme => {
  44. return {
  45. textSelector: {
  46. width: '100%',
  47. boxSizing: 'border-box',
  48. borderRadius: theme.shape.borderRadius,
  49. backgroundColor: theme.palette.uiBackground,
  50. padding: '10px 16px',
  51. textAlign: 'center',
  52. ...withPixelLineHeight(theme.typography.bodyShortRegular),
  53. border: `1px solid ${theme.palette.ui03}`
  54. }
  55. };
  56. });
  57. const DeviceSelector = ({
  58. devices,
  59. hasPermission,
  60. isDisabled,
  61. label,
  62. onSelect,
  63. selectedDeviceId
  64. }: IProps) => {
  65. const { classes } = useStyles();
  66. const { t } = useTranslation();
  67. const _onSelect = useCallback((e: React.ChangeEvent<HTMLSelectElement>) => {
  68. const deviceId = e.target.value;
  69. if (selectedDeviceId !== deviceId) {
  70. onSelect(deviceId);
  71. }
  72. }, [ selectedDeviceId, onSelect ]);
  73. const _createDropdown = (options: {
  74. defaultSelected?: MediaDeviceInfo; isDisabled: boolean;
  75. items?: Array<{ label: string; value: string; }>; placeholder: string;
  76. }) => {
  77. const triggerText
  78. = (options.defaultSelected && (options.defaultSelected.label || options.defaultSelected.deviceId))
  79. || options.placeholder;
  80. if (options.isDisabled || !options.items?.length) {
  81. return (
  82. <div className = { classes.textSelector }>
  83. {triggerText}
  84. </div>
  85. );
  86. }
  87. return (
  88. <Select
  89. label = { t(label) }
  90. onChange = { _onSelect }
  91. options = { options.items }
  92. value = { selectedDeviceId } />
  93. );
  94. };
  95. const _renderNoDevices = () => _createDropdown({
  96. isDisabled: true,
  97. placeholder: t('settings.noDevice')
  98. });
  99. const _renderNoPermission = () => _createDropdown({
  100. isDisabled: true,
  101. placeholder: t('deviceSelection.noPermission')
  102. });
  103. if (hasPermission === undefined) {
  104. return null;
  105. }
  106. if (!hasPermission) {
  107. return _renderNoPermission();
  108. }
  109. if (!devices || !devices.length) {
  110. return _renderNoDevices();
  111. }
  112. const items = devices.map(device => {
  113. return {
  114. value: device.deviceId,
  115. label: device.label || device.deviceId
  116. };
  117. });
  118. const defaultSelected = devices.find(item =>
  119. item.deviceId === selectedDeviceId
  120. );
  121. return _createDropdown({
  122. defaultSelected,
  123. isDisabled,
  124. items,
  125. placeholder: t('deviceSelection.selectADevice')
  126. });
  127. };
  128. export default DeviceSelector;