選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

SmileysPanel.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // @flow
  2. import React, { PureComponent } from 'react';
  3. import Emoji from 'react-emoji-render';
  4. import { smileys } from '../../smileys';
  5. /**
  6. * The type of the React {@code Component} props of {@link SmileysPanel}.
  7. */
  8. type Props = {
  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. * @extends Component
  19. */
  20. class SmileysPanel extends PureComponent<Props> {
  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: Props) {
  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. _onEscKey: (Object) => void;
  35. /**
  36. * KeyPress handler for accessibility.
  37. *
  38. * @param {Object} e - The key event to handle.
  39. *
  40. * @returns {void}
  41. */
  42. _onEscKey(e) {
  43. // Escape handling does not work in onKeyPress
  44. if (e.key === 'Escape') {
  45. e.preventDefault();
  46. e.stopPropagation();
  47. this.props.onSmileySelect();
  48. }
  49. }
  50. _onKeyPress: (Object) => void;
  51. /**
  52. * KeyPress handler for accessibility.
  53. *
  54. * @param {Object} e - The key event to handle.
  55. *
  56. * @returns {void}
  57. */
  58. _onKeyPress(e) {
  59. if (e.key === ' ') {
  60. e.preventDefault();
  61. this.props.onSmileySelect(e.target.id && smileys[e.target.id]);
  62. }
  63. }
  64. _onClick: (Object) => void;
  65. /**
  66. * Click handler for to select emoji.
  67. *
  68. * @param {Object} e - The key event to handle.
  69. *
  70. * @returns {void}
  71. */
  72. _onClick(e) {
  73. e.preventDefault();
  74. this.props.onSmileySelect(e.currentTarget.id && smileys[e.currentTarget.id]);
  75. }
  76. /**
  77. * Implements React's {@link Component#render()}.
  78. *
  79. * @inheritdoc
  80. * @returns {ReactElement}
  81. */
  82. render() {
  83. const smileyItems = Object.keys(smileys).map(smileyKey => (
  84. <div
  85. className = 'smileyContainer'
  86. id = { smileyKey }
  87. key = { smileyKey }
  88. onClick = { this._onClick }
  89. onKeyDown = { this._onEscKey }
  90. onKeyPress = { this._onKeyPress }
  91. role = 'option'
  92. tabIndex = { 0 }>
  93. <Emoji
  94. onlyEmojiClassName = 'smiley'
  95. text = { smileys[smileyKey] } />
  96. </div>
  97. ));
  98. return (
  99. <div
  100. aria-orientation = 'horizontal'
  101. id = 'smileysContainer'
  102. onKeyDown = { this._onEscKey }
  103. role = 'listbox'
  104. tabIndex = { -1 }>
  105. { smileyItems }
  106. </div>
  107. );
  108. }
  109. }
  110. export default SmileysPanel;