您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

VolumeSlider.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import React, { Component } from 'react';
  2. /**
  3. * Used to modify initialValue, which is expected to be a decimal value between
  4. * 0 and 1, and converts it to a number representable by an input slider, which
  5. * recognizes whole numbers.
  6. */
  7. const VOLUME_SLIDER_SCALE = 100;
  8. /**
  9. * Implements a React {@link Component} which displays an input slider for
  10. * adjusting the local volume of a remote participant.
  11. *
  12. * @extends Component
  13. */
  14. class VolumeSlider extends Component {
  15. /**
  16. * {@code VolumeSlider} component's property types.
  17. *
  18. * @static
  19. */
  20. static propTypes = {
  21. /**
  22. * The value of the audio slider should display at when the component
  23. * first mounts. Changes will be stored in state. The value should be a
  24. * number between 0 and 1.
  25. */
  26. initialValue: React.PropTypes.number,
  27. /**
  28. * The callback to invoke when the audio slider value changes.
  29. */
  30. onChange: React.PropTypes.func
  31. };
  32. /**
  33. * Initializes a new {@code VolumeSlider} instance.
  34. *
  35. * @param {Object} props - The read-only properties with which the new
  36. * instance is to be initialized.
  37. */
  38. constructor(props) {
  39. super(props);
  40. this.state = {
  41. /**
  42. * The volume of the participant's audio element. The value will
  43. * be represented by a slider.
  44. *
  45. * @type {Number}
  46. */
  47. volumeLevel: (props.initialValue || 0) * VOLUME_SLIDER_SCALE
  48. };
  49. // Bind event handlers so they are only bound once for every instance.
  50. this._onVolumeChange = this._onVolumeChange.bind(this);
  51. }
  52. /**
  53. * Implements React's {@link Component#render()}.
  54. *
  55. * @inheritdoc
  56. * @returns {ReactElement}
  57. */
  58. render() {
  59. return (
  60. <li className = 'popupmenu__item'>
  61. <div className = 'popupmenu__contents'>
  62. <span className = 'popupmenu__icon'>
  63. <i className = 'icon-volume' />
  64. </span>
  65. <div className = 'popupmenu__slider_container'>
  66. <input
  67. className = 'popupmenu__slider'
  68. max = { VOLUME_SLIDER_SCALE }
  69. min = { 0 }
  70. onChange = { this._onVolumeChange }
  71. type = 'range'
  72. value = { this.state.volumeLevel } />
  73. </div>
  74. </div>
  75. </li>
  76. );
  77. }
  78. /**
  79. * Sets the internal state of the volume level for the volume slider.
  80. * Invokes the prop onVolumeChange to notify of volume changes.
  81. *
  82. * @param {Object} event - DOM Event for slider change.
  83. * @private
  84. * @returns {void}
  85. */
  86. _onVolumeChange(event) {
  87. const volumeLevel = event.currentTarget.value;
  88. this.props.onChange(volumeLevel / VOLUME_SLIDER_SCALE);
  89. this.setState({ volumeLevel });
  90. }
  91. }
  92. export default VolumeSlider;