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.

VolumeSlider.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 { volumeLevel } = this.state;
  66. const { palette } = this.props.theme;
  67. return (
  68. <View style = { styles.volumeSliderContainer }>
  69. <Icon
  70. size = { 24 }
  71. src = { IconVolumeEmpty }
  72. style = { styles.volumeSliderIcon } />
  73. <View style = { styles.sliderContainer }>
  74. <Slider
  75. maximumTrackTintColor = { palette.field02 }
  76. maximumValue = { VOLUME_SLIDER_SCALE }
  77. minimumTrackTintColor = { palette.action01 }
  78. minimumValue = { 0 }
  79. onValueChange = { this._onVolumeChange }
  80. /* eslint-disable-next-line react-native/no-inline-styles */
  81. value = { volumeLevel } />
  82. </View>
  83. </View>
  84. );
  85. }
  86. _onVolumeChange: (Object) => void;
  87. /**
  88. * Sets the internal state of the volume level for the volume slider.
  89. * Invokes the prop onVolumeChange to notify of volume changes.
  90. *
  91. * @param {number} volumeLevel - Selected volume on slider.
  92. * @private
  93. * @returns {void}
  94. */
  95. _onVolumeChange(volumeLevel) {
  96. this.setState({ volumeLevel });
  97. }
  98. }
  99. export default withTheme(VolumeSlider);