import React, { useCallback } from 'react'; import { useTranslation } from 'react-i18next'; import { makeStyles } from 'tss-react/mui'; import Select from '../../base/ui/components/web/Select'; /** * The type of the React {@code Component} props of {@link DeviceSelector}. */ interface IProps { /** * MediaDeviceInfos used for display in the select element. */ devices: Array | undefined; /** * If false, will return a selector with no selection options. */ hasPermission: boolean; /** * CSS class for the icon to the left of the dropdown trigger. */ icon: string; /** * The id of the dropdown element. */ id: string; /** * If true, will render the selector disabled with a default selection. */ isDisabled: boolean; /** * The translation key to display as a menu label. */ label: string; /** * The callback to invoke when a selection is made. */ onSelect: Function; /** * The default device to display as selected. */ selectedDeviceId: string; } const useStyles = makeStyles()(theme => { return { textSelector: { width: '100%', boxSizing: 'border-box', borderRadius: theme.shape.borderRadius, backgroundColor: theme.palette.uiBackground, padding: '10px 16px', textAlign: 'center', ...theme.typography.bodyShortRegular, border: `1px solid ${theme.palette.ui03}` } }; }); const DeviceSelector = ({ devices, hasPermission, id, isDisabled, label, onSelect, selectedDeviceId }: IProps) => { const { classes } = useStyles(); const { t } = useTranslation(); const _onSelect = useCallback((e: React.ChangeEvent) => { const deviceId = e.target.value; if (selectedDeviceId !== deviceId) { onSelect(deviceId); } }, [ selectedDeviceId, onSelect ]); const _createDropdown = (options: { defaultSelected?: MediaDeviceInfo; isDisabled: boolean; items?: Array<{ label: string; value: string; }>; placeholder: string; }) => { const triggerText = (options.defaultSelected && (options.defaultSelected.label || options.defaultSelected.deviceId)) || options.placeholder; if (options.isDisabled || !options.items?.length) { return (
{triggerText}
); } return (