bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/audio/device-selector/component.jsx

96 lines
2.3 KiB
React
Raw Normal View History

import React, { Component } from 'react';
import PropTypes from 'prop-types';
2017-09-19 00:43:21 +08:00
import styles from '../audio-modal/styles';
const propTypes = {
kind: PropTypes.oneOf(['audioinput', 'audiooutput', 'videoinput']),
onChange: PropTypes.func.isRequired,
2017-02-16 02:49:40 +08:00
value: PropTypes.string,
};
const defaultProps = {
kind: 'audioinput',
2017-02-16 02:49:40 +08:00
value: undefined,
};
class DeviceSelector extends Component {
constructor(props) {
super(props);
this.handleEnumerateDevicesSuccess = this.handleEnumerateDevicesSuccess.bind(this);
this.handleEnumerateDevicesError = this.handleEnumerateDevicesError.bind(this);
this.handleSelectChange = this.handleSelectChange.bind(this);
this.state = {
2017-02-16 02:49:40 +08:00
value: props.value,
devices: [],
options: [],
};
}
componentDidMount() {
navigator.mediaDevices
.enumerateDevices()
.then(this.handleEnumerateDevicesSuccess)
.catch(this.handleEnumerateDevicesError);
}
handleEnumerateDevicesSuccess(deviceInfos) {
const devices = deviceInfos.filter(d => d.kind === this.props.kind);
this.setState({
devices,
options: devices.map((d, i) => ({
label: d.label || `${this.props.kind} - ${i}`,
value: d.deviceId,
2016-12-20 01:11:43 +08:00
})),
});
}
handleEnumerateDevicesError(error) {
2017-10-24 20:58:46 +08:00
log('error', error);
}
handleSelectChange(event) {
const value = event.target.value;
const { onChange } = this.props;
this.setState({ value }, () => {
const selectedDevice = this.state.devices.find(d => d.deviceId === value);
onChange(selectedDevice.deviceId, selectedDevice, event);
});
}
render() {
const { kind, handleDeviceChange, ...props } = this.props;
const { options, value } = this.state;
return (
<select
{...props}
value={value}
2016-12-20 01:11:43 +08:00
onChange={this.handleSelectChange}
2017-06-03 03:25:02 +08:00
disabled={!options.length}
2017-09-19 00:43:21 +08:00
className={styles.select}
2017-06-03 03:25:02 +08:00
>
2016-12-20 01:11:43 +08:00
{
options.length ?
options.map((option, i) => (
<option
key={i}
2017-06-03 03:25:02 +08:00
value={option.value}
>
2016-12-20 01:11:43 +08:00
{option.label}
</option>
)) :
<option value="not-found">{`no ${kind} found`}</option>
}
</select>
);
}
2017-06-03 03:25:02 +08:00
}
DeviceSelector.propTypes = propTypes;
DeviceSelector.defaultProps = defaultProps;
export default DeviceSelector;