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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. id,
  61. isDisabled,
  62. label,
  63. onSelect,
  64. selectedDeviceId
  65. }: IProps) => {
  66. const { classes } = useStyles();
  67. const { t } = useTranslation();
  68. const _onSelect = useCallback((e: React.ChangeEvent<HTMLSelectElement>) => {
  69. const deviceId = e.target.value;
  70. if (selectedDeviceId !== deviceId) {
  71. onSelect(deviceId);
  72. }
  73. }, [ selectedDeviceId, onSelect ]);
  74. const _createDropdown = (options: {
  75. defaultSelected?: MediaDeviceInfo; isDisabled: boolean;
  76. items?: Array<{ label: string; value: string; }>; placeholder: string;
  77. }) => {
  78. const triggerText
  79. = (options.defaultSelected && (options.defaultSelected.label || options.defaultSelected.deviceId))
  80. || options.placeholder;
  81. if (options.isDisabled || !options.items?.length) {
  82. return (
  83. <div className = { classes.textSelector }>
  84. {triggerText}
  85. </div>
  86. );
  87. }
  88. return (
  89. <Select
  90. id = { id }
  91. label = { t(label) }
  92. onChange = { _onSelect }
  93. options = { options.items }
  94. value = { selectedDeviceId } />
  95. );
  96. };
  97. const _renderNoDevices = () => _createDropdown({
  98. isDisabled: true,
  99. placeholder: t('settings.noDevice')
  100. });
  101. const _renderNoPermission = () => _createDropdown({
  102. isDisabled: true,
  103. placeholder: t('deviceSelection.noPermission')
  104. });
  105. if (hasPermission === undefined) {
  106. return null;
  107. }
  108. if (!hasPermission) {
  109. return _renderNoPermission();
  110. }
  111. if (!devices?.length) {
  112. return _renderNoDevices();
  113. }
  114. const items = devices.map(device => {
  115. return {
  116. value: device.deviceId,
  117. label: device.label || device.deviceId
  118. };
  119. });
  120. const defaultSelected = devices.find(item =>
  121. item.deviceId === selectedDeviceId
  122. );
  123. return _createDropdown({
  124. defaultSelected,
  125. isDisabled,
  126. items,
  127. placeholder: t('deviceSelection.selectADevice')
  128. });
  129. };
  130. export default DeviceSelector;