Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import React, { PureComponent } from 'react';
  2. import Emoji from 'react-emoji-render';
  3. import { smileys } from '../../smileys';
  4. /**
  5. * The type of the React {@code Component} props of {@link SmileysPanel}.
  6. */
  7. interface IProps {
  8. /**
  9. * Callback to invoke when a smiley is selected. The smiley will be passed
  10. * back.
  11. */
  12. onSmileySelect: Function;
  13. }
  14. /**
  15. * Implements a React Component showing smileys that can be be shown in chat.
  16. *
  17. * @augments Component
  18. */
  19. class SmileysPanel extends PureComponent<IProps> {
  20. /**
  21. * Initializes a new {@code SmileysPanel} instance.
  22. *
  23. * @param {*} props - The read-only properties with which the new instance
  24. * is to be initialized.
  25. */
  26. constructor(props: IProps) {
  27. super(props);
  28. // Bind event handler so it is only bound once for every instance.
  29. this._onClick = this._onClick.bind(this);
  30. this._onKeyPress = this._onKeyPress.bind(this);
  31. this._onEscKey = this._onEscKey.bind(this);
  32. }
  33. /**
  34. * KeyPress handler for accessibility.
  35. *
  36. * @param {Object} e - The key event to handle.
  37. *
  38. * @returns {void}
  39. */
  40. _onEscKey(e: React.KeyboardEvent) {
  41. // Escape handling does not work in onKeyPress
  42. if (e.key === 'Escape') {
  43. e.preventDefault();
  44. e.stopPropagation();
  45. this.props.onSmileySelect();
  46. }
  47. }
  48. /**
  49. * KeyPress handler for accessibility.
  50. *
  51. * @param {Object} e - The key event to handle.
  52. *
  53. * @returns {void}
  54. */
  55. _onKeyPress(e: React.KeyboardEvent<HTMLDivElement>) {
  56. if (e.key === ' ') {
  57. e.preventDefault(); // @ts-ignore
  58. this.props.onSmileySelect(e.target.id && smileys[e.target.id]);
  59. }
  60. }
  61. /**
  62. * Click handler for to select emoji.
  63. *
  64. * @param {Object} e - The key event to handle.
  65. *
  66. * @returns {void}
  67. */
  68. _onClick(e: React.MouseEvent) {
  69. e.preventDefault();
  70. this.props.onSmileySelect(e.currentTarget.id && smileys[e.currentTarget.id as keyof typeof smileys]);
  71. }
  72. /**
  73. * Implements React's {@link Component#render()}.
  74. *
  75. * @inheritdoc
  76. * @returns {ReactElement}
  77. */
  78. render() {
  79. const smileyItems = Object.keys(smileys).map(smileyKey => (
  80. <div
  81. className = 'smileyContainer'
  82. id = { smileyKey }
  83. key = { smileyKey }
  84. onClick = { this._onClick }
  85. onKeyDown = { this._onEscKey }
  86. onKeyPress = { this._onKeyPress }
  87. role = 'option'
  88. tabIndex = { 0 }>
  89. <Emoji
  90. onlyEmojiClassName = 'smiley'
  91. text = { smileys[smileyKey as keyof typeof smileys] } />
  92. </div>
  93. ));
  94. return (
  95. <div
  96. aria-orientation = 'horizontal'
  97. id = 'smileysContainer'
  98. onKeyDown = { this._onEscKey }
  99. role = 'listbox'
  100. tabIndex = { -1 }>
  101. { smileyItems }
  102. </div>
  103. );
  104. }
  105. }
  106. export default SmileysPanel;