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

VolumeSlider.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // @flow
  2. import Slider from '@react-native-community/slider';
  3. import React, { Component } from 'react';
  4. import { View } from 'react-native';
  5. import { withTheme } from 'react-native-paper';
  6. import { Icon, IconVolumeEmpty } from '../../../base/icons';
  7. import { VOLUME_SLIDER_SCALE } from '../../constants';
  8. import styles from './styles';
  9. /**
  10. * The type of the React {@code Component} props of {@link VolumeSlider}.
  11. */
  12. type Props = {
  13. /**
  14. * The value of the audio slider should display at when the component first
  15. * mounts. Changes will be stored in state. The value should be a number
  16. * between 0 and 1.
  17. */
  18. initialValue: number,
  19. /**
  20. * The callback to invoke when the audio slider value changes.
  21. */
  22. onChange: Function,
  23. /**
  24. * Theme used for styles.
  25. */
  26. theme: Object
  27. };
  28. /**
  29. * The type of the React {@code Component} state of {@link VolumeSlider}.
  30. */
  31. type State = {
  32. /**
  33. * The volume of the participant's audio element. The value will
  34. * be represented by a slider.
  35. */
  36. volumeLevel: number
  37. };
  38. /**
  39. * Component that renders the volume slider.
  40. *
  41. * @returns {React$Element<any>}
  42. */
  43. class VolumeSlider extends Component<Props, State> {
  44. /**
  45. * Initializes a new {@code VolumeSlider} instance.
  46. *
  47. * @param {Object} props - The read-only properties with which the new
  48. * instance is to be initialized.
  49. */
  50. constructor(props: Props) {
  51. super(props);
  52. this.state = {
  53. volumeLevel: (props.initialValue || 0) * VOLUME_SLIDER_SCALE
  54. };
  55. // Bind event handlers so they are only bound once for every instance.
  56. this._onVolumeChange = this._onVolumeChange.bind(this);
  57. }
  58. /**
  59. * Implements React's {@link Component#render()}.
  60. *
  61. * @inheritdoc
  62. * @returns {ReactElement}
  63. */
  64. render() {
  65. const { theme } = this.props;
  66. const { volumeLevel } = this.state;
  67. const { palette } = theme;
  68. return (
  69. <View style = { styles.volumeSliderContainer }>
  70. <Icon
  71. size = { 24 }
  72. src = { IconVolumeEmpty }
  73. style = { styles.volumeIcon } />
  74. <Slider
  75. maximumTrackTintColor = { palette.field02 }
  76. maximumValue = { VOLUME_SLIDER_SCALE }
  77. minimumTrackTintColor = { palette.action01 }
  78. minimumValue = { 0 }
  79. onValueChange = { this._onVolumeChange }
  80. style = { styles.sliderContainer }
  81. value = { volumeLevel } />
  82. </View>
  83. );
  84. }
  85. _onVolumeChange: (Object) => void;
  86. /**
  87. * Sets the internal state of the volume level for the volume slider.
  88. * Invokes the prop onVolumeChange to notify of volume changes.
  89. *
  90. * @param {number} volumeLevel - Selected volume on slider.
  91. * @private
  92. * @returns {void}
  93. */
  94. _onVolumeChange(volumeLevel) {
  95. const { onChange } = this.props;
  96. onChange(volumeLevel / VOLUME_SLIDER_SCALE);
  97. this.setState({ volumeLevel });
  98. }
  99. }
  100. export default withTheme(VolumeSlider);