Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

SmileysPanel.tsx 3.3KB

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